From be0981f7b7b56af9b5aaba9154f9a918fe7373a7 Mon Sep 17 00:00:00 2001 From: trev Date: Tue, 18 Mar 2025 17:46:54 -0400 Subject: [PATCH] feat: auth redirect --- client/src/routes/auth/+page.svelte | 4 +++- server/internal/interceptors/auth.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/client/src/routes/auth/+page.svelte b/client/src/routes/auth/+page.svelte index 30c6e6d..750d24d 100644 --- a/client/src/routes/auth/+page.svelte +++ b/client/src/routes/auth/+page.svelte @@ -7,6 +7,7 @@ import { toast } from 'svelte-sonner'; import Button from '$lib/ui/Button.svelte'; import Input from '$lib/ui/Input.svelte'; + import { page } from '$app/state'; let tab = $state('login'); @@ -41,6 +42,7 @@ const formData = new FormData(e.target as HTMLFormElement); const username = formData.get('login-username')?.toString(); const password = formData.get('login-password')?.toString(); + const redir = page.url.searchParams.get('redir') || '/'; try { const response = await AuthClient.login({ @@ -49,7 +51,7 @@ }); if (response.token && username) { - goto('/'); + goto(redir); } } catch (err) { const error = ConnectError.from(err); diff --git a/server/internal/interceptors/auth.go b/server/internal/interceptors/auth.go index d56e0b5..fcf4743 100644 --- a/server/internal/interceptors/auth.go +++ b/server/internal/interceptors/auth.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "net/http" + "net/url" "strconv" "strings" @@ -62,7 +63,8 @@ func WithAuthRedirect(next http.Handler, key string) http.Handler { } // Redirect if not authenticated - http.Redirect(w, r, "/auth", http.StatusFound) + pathRedir := url.QueryEscape(r.URL.Path) + http.Redirect(w, r, fmt.Sprintf("/auth?redir=%s", pathRedir), http.StatusFound) } }) }