From 63f8f813212ab82f56d84d01d0b543aad7f62331 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sat, 18 May 2024 17:00:49 +0200 Subject: [PATCH] Redirect to previous page in login --- src/lib/components/NavBar.svelte | 7 ++++++- src/routes/(auth)/login/+page.server.ts | 9 +++++++-- src/routes/(auth)/login/+page.svelte | 6 +++++- src/routes/(auth)/logout/+page.server.ts | 2 +- src/routes/(protected)/profile/+page.server.ts | 1 - src/routes/timer/+page.server.ts | 2 -- src/routes/timer/+page.svelte | 3 --- 7 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/lib/components/NavBar.svelte b/src/lib/components/NavBar.svelte index be618a4..98f8c70 100644 --- a/src/lib/components/NavBar.svelte +++ b/src/lib/components/NavBar.svelte @@ -2,6 +2,7 @@ import { enhance } from "$app/forms" import type { Theme, User } from "$lib" import ThemePicker from "./ThemePicker.svelte" + import { page } from '$app/stores'; let { theme, user } = $props<{ theme: Theme; user: User }>() @@ -35,7 +36,11 @@ {/each} {#if !user} - Login + {#if $page.url.pathname !== "/login" && $page.url.pathname !== "/register" && $page.url.pathname !== "/"} + Login + {:else} + Login + {/if} Register {/if} diff --git a/src/routes/(auth)/login/+page.server.ts b/src/routes/(auth)/login/+page.server.ts index 20648ce..92a55a4 100644 --- a/src/routes/(auth)/login/+page.server.ts +++ b/src/routes/(auth)/login/+page.server.ts @@ -3,10 +3,14 @@ import bcrypt from "bcrypt" import { db } from "$lib/server/database" -export const load = async ({ locals }) => { +export const load = async ({ locals, url }) => { if (locals.user) { throw redirect(302, "/") } + + let from = url.searchParams.get('from') + + return { from } } export const actions = { @@ -14,6 +18,7 @@ export const actions = { const data = await request.formData() const username = data.get("username") const password = data.get("password") + let from = data.get("from") if ( typeof username !== "string" || @@ -49,6 +54,6 @@ export const actions = { maxAge: 60 * 60 * 24 * 30, }) - throw redirect(302, "/") + throw redirect(302, from) }, } diff --git a/src/routes/(auth)/login/+page.svelte b/src/routes/(auth)/login/+page.svelte index bf32663..ec5e34b 100644 --- a/src/routes/(auth)/login/+page.svelte +++ b/src/routes/(auth)/login/+page.svelte @@ -1,7 +1,9 @@
@@ -10,6 +12,8 @@ + +
diff --git a/src/routes/(auth)/logout/+page.server.ts b/src/routes/(auth)/logout/+page.server.ts index 2170255..f7036cb 100644 --- a/src/routes/(auth)/logout/+page.server.ts +++ b/src/routes/(auth)/logout/+page.server.ts @@ -11,6 +11,6 @@ export const actions = { expires: new Date(0), }) - throw redirect(302, "/login") + throw redirect(302, "/") }, } diff --git a/src/routes/(protected)/profile/+page.server.ts b/src/routes/(protected)/profile/+page.server.ts index c932e60..6fef3fe 100644 --- a/src/routes/(protected)/profile/+page.server.ts +++ b/src/routes/(protected)/profile/+page.server.ts @@ -19,7 +19,6 @@ async function fetchGuides(event: ServerLoadEvent) { if (res.ok) { let num = Math.floor(Math.random() * data.length) - console.log(data[num]["lines"]) return data[num]["lines"] } } diff --git a/src/routes/timer/+page.server.ts b/src/routes/timer/+page.server.ts index 8007cec..69c4998 100644 --- a/src/routes/timer/+page.server.ts +++ b/src/routes/timer/+page.server.ts @@ -1,4 +1,3 @@ -import { redirect, type ServerLoadEvent } from "@sveltejs/kit" import type { PageServerLoad } from "./$types" import { db } from "$lib/server/database" import type { User } from "$lib/index" @@ -31,7 +30,6 @@ export const actions = { updateTimer: async ({ request, locals }) => { const user = locals.user const data = await request.formData() - console.log(data) const breakInterval = parseInt(data.get("break-interval")) const miniBreakDuration = parseInt(data.get("mini-break-duration")) const longBreakDuration = parseInt(data.get("long-break-duration")) diff --git a/src/routes/timer/+page.svelte b/src/routes/timer/+page.svelte index 2c68d7c..1ad7f0e 100644 --- a/src/routes/timer/+page.svelte +++ b/src/routes/timer/+page.svelte @@ -74,7 +74,6 @@ }) $effect(() => { - console.log(MINI_BREAK_DURATION_STORE) MINI_BREAK_DURATION = MINI_BREAK_DURATION_STORE * 1000 localStorage.setItem("miniBreakDuration", MINI_BREAK_DURATION_STORE) updateTimer() @@ -129,7 +128,6 @@ timer = setInterval(() => { const newTimeLeft = timeLeft - 1000 if (newTimeLeft <= 0) { - console.log(timerState) if (timerState !== "Ready") { timerState = "Ready" miniBreakCount++ @@ -162,7 +160,6 @@ } timeLeftDisplay = formatTime(newTimeLeft) timeLeft = newTimeLeft - console.log(timeLeft) }, 1000) }