feat: auth redirect
This commit is contained in:
parent
86b74e0ebc
commit
be0981f7b7
@ -7,6 +7,7 @@
|
|||||||
import { toast } from 'svelte-sonner';
|
import { toast } from 'svelte-sonner';
|
||||||
import Button from '$lib/ui/Button.svelte';
|
import Button from '$lib/ui/Button.svelte';
|
||||||
import Input from '$lib/ui/Input.svelte';
|
import Input from '$lib/ui/Input.svelte';
|
||||||
|
import { page } from '$app/state';
|
||||||
|
|
||||||
let tab = $state('login');
|
let tab = $state('login');
|
||||||
</script>
|
</script>
|
||||||
@ -41,6 +42,7 @@
|
|||||||
const formData = new FormData(e.target as HTMLFormElement);
|
const formData = new FormData(e.target as HTMLFormElement);
|
||||||
const username = formData.get('login-username')?.toString();
|
const username = formData.get('login-username')?.toString();
|
||||||
const password = formData.get('login-password')?.toString();
|
const password = formData.get('login-password')?.toString();
|
||||||
|
const redir = page.url.searchParams.get('redir') || '/';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await AuthClient.login({
|
const response = await AuthClient.login({
|
||||||
@ -49,7 +51,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (response.token && username) {
|
if (response.token && username) {
|
||||||
goto('/');
|
goto(redir);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const error = ConnectError.from(err);
|
const error = ConnectError.from(err);
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -62,7 +63,8 @@ func WithAuthRedirect(next http.Handler, key string) http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Redirect if not authenticated
|
// 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)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user