added redirect from login if sessionKey present

This commit is contained in:
Jonas_Jones 2025-03-07 20:41:12 +01:00
parent e5c9c351e7
commit f0eedba287
2 changed files with 14 additions and 1 deletions

View file

@ -19,6 +19,15 @@ export function resetSession() {
window.location.href = '/login';
}
export function redirectToHome() {
if (getSessionKey()) {
window.location.href = new URLSearchParams(window.location.search).get('returnUrl') || '/';
console.log(new URLSearchParams(window.location.search).get('returnUrl') || '/')
} else {
console.log(getSessionKey())
}
}
export function ensureAuthenticated() {
if (!getSessionKey()) {
redirectToLogin();

View file

@ -1,13 +1,17 @@
<script>
import { navigate } from 'svelte-routing';
import { onMount } from 'svelte';
import { setSessionKey, ACCOUNTS_WORKER_URL } from '$lib/session.js';
import { setSessionKey, redirectToHome, ACCOUNTS_WORKER_URL } from '$lib/session.js';
import { page } from '$app/state';
let email = '';
let password = '';
let errorMessage = '';
let showPassword = false;
onMount(() => {
redirectToHome(); // This will check the session key and redirect if necessary
});
const handleLogin = async () => {
try {
const response = await fetch(ACCOUNTS_WORKER_URL + '/login', {