fix: improve back button handling on auth pages

This commit is contained in:
Elias Schneider
2025-09-30 14:44:08 +02:00
parent fc9939d1f1
commit d47b20326f
5 changed files with 23 additions and 13 deletions

View File

@@ -33,7 +33,7 @@
alternativeSignInButton.label = m.sign_in_with_login_code();
}
if (page.url.pathname == '/login') {
if (page.url.pathname != '/login') {
alternativeSignInButton.href = `${alternativeSignInButton.href}?redirect=${encodeURIComponent(page.url.pathname + page.url.search)}`;
}
});

View File

@@ -179,7 +179,7 @@
{m.try_again()}
</Button>
{/if}
<Button onclick={() => history.back()} class="flex-1" variant="secondary">
<Button href={document.referrer || '/'} class="flex-1" variant="secondary">
{m.cancel()}
</Button>
</div>

View File

@@ -10,6 +10,9 @@
import { startAuthentication } from '@simplewebauthn/browser';
import { fade } from 'svelte/transition';
import LoginLogoErrorSuccessIndicator from './components/login-logo-error-success-indicator.svelte';
let { data } = $props();
const webauthnService = new WebAuthnService();
let isLoading = $state(false);
@@ -24,7 +27,7 @@
const user = await webauthnService.finishLogin(authResponse);
await userStore.setUser(user);
goto('/settings');
goto(data.redirect || '/settings');
} catch (e) {
error = getWebauthnErrorMessage(e);
}

View File

@@ -0,0 +1,7 @@
import type { PageLoad } from './$types';
export const load: PageLoad = async ({ url }) => {
return {
redirect: url.searchParams.get('redirect') || '/settings'
};
};

View File

@@ -1,12 +1,10 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/state';
import { afterNavigate, goto } from '$app/navigation';
import SignInWrapper from '$lib/components/login-wrapper.svelte';
import { Button } from '$lib/components/ui/button';
import Input from '$lib/components/ui/input/input.svelte';
import { m } from '$lib/paraglide/messages';
import UserService from '$lib/services/user-service';
import appConfigStore from '$lib/stores/application-configuration-store';
import userStore from '$lib/stores/user-store.js';
import { getAxiosErrorMessage } from '$lib/utils/error-util';
import { preventDefault } from '$lib/utils/event-util';
@@ -17,9 +15,17 @@
let code = $state(data.code ?? '');
let isLoading = $state(false);
let error: string | undefined = $state();
let backHref = $state('/login/alternative');
const userService = new UserService();
// If the previous page is a Pocket ID page, go back there instead of the generic alternative login page
afterNavigate((e) => {
if (e.from?.url.pathname) {
backHref = e.from.url.pathname + e.from.url.search;
}
});
async function authenticate() {
isLoading = true;
try {
@@ -64,13 +70,7 @@
<form onsubmit={preventDefault(authenticate)} class="w-full max-w-[450px]">
<Input id="Code" class="mt-7" placeholder={m.code()} bind:value={code} type="text" />
<div class="mt-8 flex justify-between gap-2">
<Button
variant="secondary"
class="flex-1"
href={($appConfigStore.emailOneTimeAccessAsUnauthenticatedEnabled
? '/login/alternative'
: '/login') + page.url.search}>{m.go_back()}</Button
>
<Button variant="secondary" class="flex-1" href={backHref}>{m.go_back()}</Button>
<Button class="flex-1" type="submit" {isLoading}>{m.submit()}</Button>
</div>
</form>