jwt 작동되도록 수정..
This commit is contained in:
parent
bdf43e1906
commit
63e3831951
@ -1,26 +1,43 @@
|
||||
// D:/gitea/Jwt/src/routes/demo/lucia/+page.server.ts
|
||||
|
||||
import * as auth from '$lib/server/auth';
|
||||
import { fail, redirect } from '@sveltejs/kit';
|
||||
import { getRequestEvent } from '$app/server';
|
||||
import { redirect } from '@sveltejs/kit'; // `fail`은 사용되지 않으므로 제거합니다.
|
||||
import type { Actions, PageServerLoad } from './$types';
|
||||
|
||||
export const load: PageServerLoad = async () => {
|
||||
const user = requireLogin()
|
||||
return { user };
|
||||
// `load` 함수는 event 객체에서 locals와 cookies를 직접 받아옵니다.
|
||||
export const load: PageServerLoad = async ({ locals, cookies }) => {
|
||||
// 1. `locals`에서 직접 사용자 정보를 확인합니다. 이것이 표준 방식입니다.
|
||||
if (!locals.user) {
|
||||
// 2. 사용자가 없으면 여기서 바로 리다이렉트합니다.
|
||||
throw redirect(302, '/demo/lucia/login');
|
||||
}
|
||||
|
||||
// 이 코드는 사용자가 로그인한 경우에만 실행됩니다.
|
||||
// 이전에 정의한 쿠키 이름으로 수정했습니다.
|
||||
const accessToken = cookies.get(auth.jwtCookieName);
|
||||
const refreshToken = cookies.get(auth.refreshCookieName);
|
||||
|
||||
// 3. `locals`의 사용자와 쿠키에서 읽은 토큰을 반환합니다.
|
||||
return {
|
||||
user: locals.user,
|
||||
accessToken: accessToken ?? 'N/A', // 토큰이 없을 경우를 대비해 기본값 설정
|
||||
refreshToken: refreshToken ?? 'N/A'
|
||||
};
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
logout: async (event) => {
|
||||
// 더 안전한 로그아웃을 위해 DB의 리프레시 토큰을 무효화합니다.
|
||||
const refreshTokenFromCookie = event.cookies.get(auth.refreshCookieName);
|
||||
// if (refreshTokenFromCookie) {
|
||||
// // auth.ts에 구현된 invalidateRefreshToken 함수를 호출합니다.
|
||||
// await auth.invalidateRefreshToken(refreshTokenFromCookie);
|
||||
// }
|
||||
|
||||
// 그 다음 쿠키를 삭제합니다.
|
||||
auth.deleteAuthCookies(event);
|
||||
return redirect(302, '/demo/lucia/login');
|
||||
},
|
||||
};
|
||||
|
||||
function requireLogin() {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
if (!locals.user) {
|
||||
return redirect(302, "/demo/lucia/login");
|
||||
}
|
||||
|
||||
return locals.user;
|
||||
}
|
||||
// `actions`에서는 throw redirect(...)를 사용하는 것이 표준입니다.
|
||||
throw redirect(302, '/demo/lucia/login');
|
||||
}
|
||||
};
|
||||
@ -3,10 +3,13 @@
|
||||
import type { PageServerData } from './$types';
|
||||
|
||||
let { data }: { data: PageServerData } = $props();
|
||||
|
||||
</script>
|
||||
|
||||
<h1>Hi, {data.user.username}!</h1>
|
||||
<h1>Hi!</h1>
|
||||
<p>Your user ID is {data.user.id}.</p>
|
||||
<p>Your JWT is "{data.accessToken}"</p>
|
||||
<p>Your REFRESH_JWT is "{data.refreshToken}"</p>
|
||||
<form method='post' action='?/logout' use:enhance>
|
||||
<button>Sign out</button>
|
||||
</form>
|
||||
|
||||
@ -54,7 +54,7 @@ export const actions: Actions = {
|
||||
auth.setAccessTokenCookie(event, accessToken);
|
||||
auth.setRefreshTokenCookie(event, refreshToken);
|
||||
|
||||
return redirect(302, '/dashboard');
|
||||
return redirect(302, '/demo/lucia');
|
||||
},
|
||||
register: async (event) => {
|
||||
const formData = await event.request.formData();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user