Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do full page loads for home and logout #409

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/hooks.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ handleFetch(async ({ fetch, args }) => {
const response = await traceFetch(async () => {
const response = await fetch(...args);

validateFetchResponse(response,
await validateFetchResponse(response,
location.pathname === '/login',
location.pathname === '/' || location.pathname === '/home' || location.pathname === '/admin');

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const handleFetch: HandleFetch = async ({ event, request, fetch }) => {
const response = await fetch(request);

const routeId = event.route.id ?? '';
validateFetchResponse(response,
await validateFetchResponse(response,
routeId.endsWith('/login'),
routeId.endsWith(AUTHENTICATED_ROOT) || routeId.endsWith('/home') || routeId.endsWith('/admin'));

Expand Down
5 changes: 3 additions & 2 deletions frontend/src/hooks.shared.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { goHome } from '$lib/user';
import { redirect } from '@sveltejs/kit';

const sayWuuuuuuut = 'We\'re not sure what happened.';
Expand All @@ -20,7 +21,7 @@ export function getErrorMessage(error: unknown): string {
);
}

export function validateFetchResponse(response: Response, isAtLogin: boolean, isHome: boolean): void {
export async function validateFetchResponse(response: Response, isAtLogin: boolean, isHome: boolean): Promise<void> {
if (response.status === 401 && !isAtLogin) {
throw redirect(307, '/logout');
}
Expand All @@ -31,7 +32,7 @@ export function validateFetchResponse(response: Response, isAtLogin: boolean, is
throw redirect(307, '/logout');
} else {
// the user tried to access something they don't have permission for
throw redirect(307, '/home');
await goHome();
}
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/layout/AppMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</header>

<li>
<a href="/logout" data-sveltekit-preload-data="tap">
<a href="/logout" data-sveltekit-reload>
{$t('appmenu.log_out')}
<LogoutIcon />
</a>
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ export function getHomePath(user: LexAuthUser | null): string {
return isAdmin(user) ? '/admin' : '/';
}

export async function goHome(): Promise<void> {
if (browser) {
location.pathname = '/home';
return new Promise(() => {});
} else {
throw redirect(307, '/home');
}
}

export async function login(userId: string, password: string): Promise<boolean> {
const response = await fetch('/api/login', {
method: 'post',
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/routes/(unauthenticated)/login/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { isAuthn } from '$lib/user';
import { redirect } from '@sveltejs/kit';
import { goHome, isAuthn } from '$lib/user';
import type { PageServerLoad } from './$types';

export const load = (({ cookies }) => {
export const load = (async ({ cookies }) => {
if (isAuthn(cookies)) {
throw redirect(307, '/home');
await goHome();
}
}) satisfies PageServerLoad
5 changes: 2 additions & 3 deletions frontend/src/routes/(unauthenticated)/login/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { SubmitButton, Form, FormError, Input, lexSuperForm } from '$lib/forms';
import t from '$lib/i18n';
import { PageHeader } from '$lib/layout';
import { login, logout } from '$lib/user';
import { goHome, login, logout } from '$lib/user';
import { onMount } from 'svelte';
import SvelteMarkdown from 'svelte-markdown';
import flexLogo from '$lib/assets/flex-logo.png';
Expand All @@ -20,7 +19,7 @@
formSchema,
async () => {
if (await login($form.email, $form.password)) {
await goto('/home');
await goHome();
return;
}
$message = $t('login.bad_credentials');
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/routes/(unauthenticated)/register/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { SubmitButton, FormError, Input, ProtectedForm, lexSuperForm } from '$lib/forms';
import { passwordFormRules } from '$lib/forms/utils';
import t from '$lib/i18n';
import { Page } from '$lib/layout';
import { register } from '$lib/user';
import { goHome, register } from '$lib/user';
import { z } from 'zod';

let turnstileToken = '';
Expand All @@ -26,7 +25,7 @@
return;
}
if (user) {
await goto('/home');
await goHome();
return;
}
throw new Error('Unknown error, no error from server, but also no user.');
Expand Down