Skip to content

Commit

Permalink
Merge branch 'IMG-49-svelte-server-side-functionality'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/routes/dash/login/+page.svelte
  • Loading branch information
MrExplode committed Nov 28, 2023
2 parents da3b192 + c5edf90 commit 59c84ac
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"prettier-plugin-svelte": "^2.10.1",
"svelte": "^4.0.5",
"svelte-check": "^3.4.3",
"sveltekit-superforms": "^1.10.1",
"svelte-meta-tags": "^3.1.0",
"sveltekit-superforms": "^1.10.1",
"tailwind-merge": "^2.0.0",
"tailwind-variants": "^0.1.18",
"tailwindcss": "^3.3.5",
Expand Down
5 changes: 3 additions & 2 deletions src/lib/LoginForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import { loginSchema, type LoginSchema } from '$lib/schema'
import type { SuperValidated } from 'sveltekit-superforms'
export let form: SuperValidated<LoginSchema>
</script>

<Form.Root {form} schema={loginSchema} let:config>
<Form.Root method="POST" {form} schema={loginSchema} let:config>
<Form.Field {config} name="username">
<Form.Item>
<Form.Label class="text-xl">Username</Form.Label>
Expand All @@ -17,7 +18,7 @@
<Form.Field {config} name="password">
<Form.Item class="mb-5 ">
<Form.Label class="text-xl">Password</Form.Label>
<Form.Input class="bg-slate-950" />
<Form.Input class="bg-slate-950" type="password"/>
<Form.Description />
<Form.Validation />
</Form.Item>
Expand Down
54 changes: 54 additions & 0 deletions src/routes/dash/login/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { PageServerLoad, Actions } from './$types'
import { fail } from '@sveltejs/kit'
import { setError, superValidate } from 'sveltekit-superforms/server'
import { loginSchema } from '$lib/schema'
import { redirect } from '@sveltejs/kit'
import { actionResult } from 'sveltekit-superforms/server'
import { error } from '@sveltejs/kit'

interface LoginResponse {
token: string
}
interface ResponseType {
message: string
details: any
error: {
statusCode: number
statusPhrase: string
}
}

export const load: PageServerLoad = () => {
return {
form: superValidate(loginSchema)
}
}

export const actions: Actions = {
default: async ({ request, cookies }) => {
const form = await superValidate(request, loginSchema)
if (!form.valid) {
return actionResult('failure', { form }, 400)
}
const response = await fetch('https://api.unideb.tech/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: form.data.username,
password: form.data.password
})
})
if (response.status === 200) {
let token = ((await response.json()) as LoginResponse).token
console.log('Login successful, token: ' + token)
cookies.set('token', token, {})
throw redirect(303, '/dash/')
} else {
let resp = (await response.json()) as ResponseType
console.log(resp.message)
return setError(form, 'username', resp.message)
}
}
}
7 changes: 6 additions & 1 deletion src/routes/dash/login/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import LoginForm from '$lib/LoginForm.svelte'
import Centered from '$lib/Centered.svelte'
import logo from '$lib/assets/icons/bordered/logo_slate-700.png'
import type { PageData } from './$types';
export let data: PageData;
</script>

<Centered>
Expand All @@ -20,7 +25,7 @@
</Card.Header>

<Card.Content>
<LoginForm {form} />
<LoginForm form = {data.form}/>
<Label class="">Don't have an account yet?</Label>
<Button class="text-slate-400" variant="link" href="/dash/register">Register</Button>
</Card.Content>
Expand Down

0 comments on commit 59c84ac

Please sign in to comment.