mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-15 06:18:48 +00:00
Access Token 유무에 따른 자동 리다이렉트 구현
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import type { ServerInit } from "@sveltejs/kit";
|
import { redirect, type ServerInit, type Handle } from "@sveltejs/kit";
|
||||||
import schedule from "node-schedule";
|
import schedule from "node-schedule";
|
||||||
import { cleanupExpiredRefreshTokens } from "$lib/server/db/token";
|
import { cleanupExpiredRefreshTokens } from "$lib/server/db/token";
|
||||||
|
|
||||||
@@ -7,3 +7,17 @@ export const init: ServerInit = () => {
|
|||||||
cleanupExpiredRefreshTokens();
|
cleanupExpiredRefreshTokens();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const handle: Handle = async ({ event, resolve }) => {
|
||||||
|
const path = event.url.pathname;
|
||||||
|
if (path.startsWith("/api") || path.startsWith("/auth")) {
|
||||||
|
return await resolve(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
const accessToken = event.cookies.get("accessToken");
|
||||||
|
if (accessToken) {
|
||||||
|
return await resolve(event);
|
||||||
|
} else {
|
||||||
|
redirect(302, "/auth/login?redirect=" + encodeURIComponent(path));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
13
src/routes/(fullscreen)/auth/login/+page.server.ts
Normal file
13
src/routes/(fullscreen)/auth/login/+page.server.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { redirect } from "@sveltejs/kit";
|
||||||
|
import type { PageServerLoad } from "./$types";
|
||||||
|
|
||||||
|
export const load: PageServerLoad = async ({ url, cookies }) => {
|
||||||
|
const redirectPath = url.searchParams.get("redirect") || "/";
|
||||||
|
|
||||||
|
const accessToken = cookies.get("accessToken");
|
||||||
|
if (accessToken) {
|
||||||
|
redirect(302, redirectPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { redirectPath };
|
||||||
|
};
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { goto } from "$app/navigation";
|
||||||
import { Button, TextButton } from "$lib/components/buttons";
|
import { Button, TextButton } from "$lib/components/buttons";
|
||||||
import { TitleDiv, BottomDiv } from "$lib/components/divs";
|
import { TitleDiv, BottomDiv } from "$lib/components/divs";
|
||||||
import { TextInput } from "$lib/components/inputs";
|
import { TextInput } from "$lib/components/inputs";
|
||||||
import { requestLogin } from "./service";
|
import { requestLogin } from "./service";
|
||||||
|
|
||||||
|
let { data } = $props();
|
||||||
|
|
||||||
let email = $state("");
|
let email = $state("");
|
||||||
let password = $state("");
|
let password = $state("");
|
||||||
|
|
||||||
@@ -11,8 +14,11 @@
|
|||||||
// TODO: Validation
|
// TODO: Validation
|
||||||
|
|
||||||
const ok = await requestLogin(email, password);
|
const ok = await requestLogin(email, password);
|
||||||
|
if (ok) {
|
||||||
// TODO: Action
|
goto(data.redirectPath);
|
||||||
|
} else {
|
||||||
|
// TODO: Alert
|
||||||
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user