-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: client api requests and error handling
- Loading branch information
1 parent
a630bfc
commit e00f509
Showing
6 changed files
with
79 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,30 @@ | ||
import { dev } from '$app/environment'; | ||
import { fail } from '@sveltejs/kit'; | ||
import type { Actions } from './$types'; | ||
import { isAuthError } from '$lib/utils/errors'; | ||
import client from '$lib/clients/client'; | ||
|
||
export const actions = { | ||
login: async ({ request, cookies }) => { | ||
const form_data = await request.formData(); | ||
|
||
try { | ||
const { data } = await client.POST('/api/v1/u/auth/login/', { | ||
body: { | ||
email: form_data.get('email') as string, | ||
password: form_data.get('password') as string | ||
} | ||
}); | ||
|
||
if (data !== undefined) { | ||
cookies.set('auth_token', data.token, { | ||
httpOnly: true, | ||
secure: !dev, | ||
path: '/', | ||
sameSite: 'lax' | ||
}); | ||
const { data, error, response } = await client.POST('/api/v1/u/auth/login/', { | ||
body: { | ||
email: form_data.get('email') as string, | ||
password: form_data.get('password') as string | ||
} | ||
}); | ||
|
||
return { token: data?.token }; | ||
} catch (err) { | ||
let message = 'Oops! something went wrong.'; | ||
let code = 500; | ||
if (!response.ok && error) { | ||
return fail(response.status, error.errors[0]); | ||
} else if (data) { | ||
cookies.set('auth_token', data.token, { | ||
httpOnly: true, | ||
secure: !dev, | ||
path: '/', | ||
sameSite: 'lax' | ||
}); | ||
|
||
if (isAuthError(err)) { | ||
message = err.message; | ||
code = err.code; | ||
} else { | ||
console.error(err); | ||
} | ||
return fail(code, { detail: message }); | ||
return { token: data.token }; | ||
} | ||
} | ||
} satisfies Actions; |
39 changes: 39 additions & 0 deletions
39
frontend/src/routes/(app)/settings/profile/+page.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { fail } from '@sveltejs/kit'; | ||
import type { Actions } from './$types'; | ||
import { dev } from '$app/environment'; | ||
import client from '$lib/clients/client'; | ||
|
||
export const actions = { | ||
create: async ({ request, cookies }) => { | ||
const form_data = await request.formData(); | ||
|
||
const { error, response } = await client.POST('/api/v1/u/me/profiles/', { | ||
headers: { | ||
Authorization: `Bearer ${cookies.get('auth_token')}` | ||
}, | ||
// @ts-expect-error: only requires username for POST req | ||
body: { | ||
username: form_data.get('username') as string | ||
} | ||
}); | ||
|
||
if (!response.ok && error) { | ||
return fail(response.status, error.errors[0]); | ||
} else { | ||
return { success: true }; | ||
} | ||
}, | ||
select: async ({ request, cookies }) => { | ||
const form_data = await request.formData(); | ||
const profile_id = form_data.get('profile_id') as string; | ||
|
||
cookies.set('auth_user_profile_id', profile_id, { | ||
httpOnly: true, | ||
secure: !dev, | ||
path: '/', | ||
sameSite: 'lax' | ||
}); | ||
|
||
return { success: true }; | ||
} | ||
} satisfies Actions; |
This file was deleted.
Oops, something went wrong.