diff --git a/backend/apps/user/models.py b/backend/apps/user/models.py index 537f11a..0eec1d2 100644 --- a/backend/apps/user/models.py +++ b/backend/apps/user/models.py @@ -1,4 +1,4 @@ -from django.contrib.auth.models import AbstractBaseUser, AbstractUser, PermissionsMixin +from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin from django.db import models from django.utils.translation import gettext_lazy as _ diff --git a/frontend/src/lib/components/modals/auth/forms/login.svelte b/frontend/src/lib/components/modals/auth/forms/login.svelte index 9b62bb7..e939415 100644 --- a/frontend/src/lib/components/modals/auth/forms/login.svelte +++ b/frontend/src/lib/components/modals/auth/forms/login.svelte @@ -103,6 +103,10 @@ {/if} +
+ Not a member yet? + +

By continuing, you agree to the { - try { - pending = true; - status_text = 'Fetching profiles...'; - profiles = await apiFetch('v1/user/me/profiles/', { - headers: { - Authorization: `Bearer ${(forms_state.login as { token: string }).token}` - } - }); - } catch (err) { - console.error(err); - } finally { - pending = false; - status_text = null; + pending = true; + status_text = 'Fetching profiles...'; + + const { data, error, response } = await client.GET('/api/v1/u/me/profiles/', { + headers: { + Authorization: `Bearer ${(forms_state.login as { token: string }).token}` + } + }); + + if (!response.ok && error) { + console.log(error); + } else if (data) { + profiles = data; } + + pending = false; + status_text = null; }); diff --git a/frontend/src/routes/(actions)/auth/+page.server.ts b/frontend/src/routes/(actions)/auth/+page.server.ts index 578d46b..483f729 100644 --- a/frontend/src/routes/(actions)/auth/+page.server.ts +++ b/frontend/src/routes/(actions)/auth/+page.server.ts @@ -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; diff --git a/frontend/src/routes/(app)/settings/profile/+page.server.ts b/frontend/src/routes/(app)/settings/profile/+page.server.ts new file mode 100644 index 0000000..53b50de --- /dev/null +++ b/frontend/src/routes/(app)/settings/profile/+page.server.ts @@ -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; diff --git a/frontend/src/routes/settings/profile/+page.server.ts b/frontend/src/routes/settings/profile/+page.server.ts deleted file mode 100644 index ea93a60..0000000 --- a/frontend/src/routes/settings/profile/+page.server.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { isAuthError } from '$lib/utils/errors'; -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(); - - try { - 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 - } - }); - - return { success: true }; - } catch (err) { - let message = 'Oops! something went wrong.'; - let code = 500; - - if (isAuthError(err)) { - message = err.message; - code = err.code; - } else if (err instanceof Error) { - message = err.message; - code = 400; - } - return fail(code, { detail: message }); - } - }, - select: async ({ request, cookies }) => { - const form_data = await request.formData(); - - cookies.set('auth_user_profile_id', form_data.get('profile_id') as string, { - httpOnly: true, - secure: !dev, - path: '/', - sameSite: 'lax' - }); - - return { success: true }; - } -} satisfies Actions;