feat(auth): preserve redirect URL when redirecting to /login
Appends ?redirect= with the original pathname so the login page can redirect back after successful authentication. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,11 @@ function isPublicRoute(pathname: string): boolean {
|
||||
return PUBLIC_ROUTES.some((route) => pathname === route || pathname.startsWith(route + '/'));
|
||||
}
|
||||
|
||||
function loginRedirect(pathname: string): never {
|
||||
const target = '/login?redirect=' + encodeURIComponent(pathname);
|
||||
redirect(302, target);
|
||||
}
|
||||
|
||||
export const handle: Handle = async ({ event, resolve }) => {
|
||||
if (isPublicRoute(event.url.pathname)) {
|
||||
return resolve(event);
|
||||
@@ -20,14 +25,14 @@ export const handle: Handle = async ({ event, resolve }) => {
|
||||
|
||||
const sessionCookie = event.cookies.get('session');
|
||||
if (!sessionCookie) {
|
||||
redirect(302, '/login');
|
||||
loginRedirect(event.url.pathname);
|
||||
}
|
||||
|
||||
const api = apiClient(event.fetch);
|
||||
const { data, error } = await api.GET('/v1/auth/me');
|
||||
|
||||
if (error || !data?.data) {
|
||||
redirect(302, '/login');
|
||||
loginRedirect(event.url.pathname);
|
||||
}
|
||||
|
||||
const user = data.data;
|
||||
|
||||
Reference in New Issue
Block a user