Skip to content

Commit

Permalink
refactor: Move logic into the api abstraction
Browse files Browse the repository at this point in the history
bocsi sopa xd
  • Loading branch information
MrExplode committed Nov 28, 2023
1 parent 59c84ac commit 88c9df8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 38 deletions.
3 changes: 1 addition & 2 deletions src/lib/LoginForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { loginSchema, type LoginSchema } from '$lib/schema'
import type { SuperValidated } from 'sveltekit-superforms'
export let form: SuperValidated<LoginSchema>
</script>

<Form.Root method="POST" {form} schema={loginSchema} let:config>
Expand All @@ -18,7 +17,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" type="password"/>
<Form.Input class="bg-slate-950" type="password" />
<Form.Description />
<Form.Validation />
</Form.Item>
Expand Down
6 changes: 5 additions & 1 deletion src/lib/server/apiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export interface LoginRequest {
password: string
}

export interface LoginResponse {
token: string
}

export interface RegisterRequest {
username: string
password: string
Expand All @@ -23,4 +27,4 @@ export interface ApiError {

export type BackendRequest = LoginRequest | RegisterRequest | null

export type BackendResponse = ProfileInfo | UploadInfo | ViewResponse
export type BackendResponse = ProfileInfo | UploadInfo | ViewResponse | LoginResponse
45 changes: 15 additions & 30 deletions src/routes/dash/login/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,9 @@ 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
}
}
import { useApi } from '$lib/server/api'
import type { ApiError, LoginRequest, LoginResponse } from '$lib/server/apiTypes'
import { isError } from '$lib/types'

export const load: PageServerLoad = () => {
return {
Expand All @@ -30,25 +21,19 @@ export const actions: Actions = {
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/')

const req: LoginRequest = {
username: form.data.username,
password: form.data.password
}
const data = await useApi(null, '/auth/login', 'POST', req)
if (isError(data)) {
const error = data as ApiError
return setError(form, 'username', error.message)
} else {
let resp = (await response.json()) as ResponseType
console.log(resp.message)
return setError(form, 'username', resp.message)
const result = data as LoginResponse
cookies.set('token', result.token, {})
throw redirect(303, '/dash/')
}
}
}
8 changes: 3 additions & 5 deletions src/routes/dash/login/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
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;
import type { PageData } from './$types'
export let data: PageData
</script>

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

<Card.Content>
<LoginForm form = {data.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 88c9df8

Please sign in to comment.