Skip to content

Commit

Permalink
fix: client api requests and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace committed Dec 12, 2024
1 parent a630bfc commit e00f509
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 92 deletions.
2 changes: 1 addition & 1 deletion backend/apps/user/models.py
Original file line number Diff line number Diff line change
@@ -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 _

Expand Down
4 changes: 4 additions & 0 deletions frontend/src/lib/components/modals/auth/forms/login.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@
{/if}
</button>
</form>
<div class="flex items-center gap-2 text-sm">
<span>Not a member yet?</span>
<button class="font-info font-medium underline">Register now!</button>
</div>
<p class="text-center text-xs">
By continuing, you agree to the <a
href="/support/terms-and-conditions"
Expand Down
35 changes: 20 additions & 15 deletions frontend/src/lib/components/modals/auth/forms/profile_select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import type { FormProps } from '../types';
import Avatar from '$lib/components/ui/avatar.svelte';
import { onMount } from 'svelte';
import { apiFetch } from '$lib/utils/api';
import type { Profile } from '$lib/types/user';
import { enhance } from '$app/forms';
import type { SubmitFunction } from '@sveltejs/kit';
import { close_modal } from '$lib/stores/modals.svelte';
import { invalidateAll } from '$app/navigation';
// @ts-expect-error: too lazy to copy paste it
import daisyuiColorNames from 'daisyui/src/theming/colorNames';
import client from '$lib/clients/client';
import type { components } from '$lib/clients/v1';
type Profile = components['schemas']['Profile'];
let { forms_state, goto_form }: FormProps = $props();
Expand All @@ -38,20 +40,23 @@
};
onMount(async () => {
try {
pending = true;
status_text = 'Fetching profiles...';
profiles = await apiFetch<Profile[]>('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;
});
</script>

Expand Down
42 changes: 15 additions & 27 deletions frontend/src/routes/(actions)/auth/+page.server.ts
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 frontend/src/routes/(app)/settings/profile/+page.server.ts
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;
49 changes: 0 additions & 49 deletions frontend/src/routes/settings/profile/+page.server.ts

This file was deleted.

0 comments on commit e00f509

Please sign in to comment.