Skip to content

Commit a9c6ec6

Browse files
ログインページから、アクセスされたページにリダイレクトする #16
1 parent d4442ec commit a9c6ec6

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/routes/login/+page.server.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import type { Actions, PageServerLoad } from '.svelte-kit/types/src/routes/regis
44
import { invalid, redirect } from '@sveltejs/kit'
55
import bcrypt from 'bcrypt'
66

7-
export const load: PageServerLoad = async ({ locals }) => {
8-
if (locals.user) throw redirect(302, '/')
7+
export const load: PageServerLoad = async ({ locals, url }) => {
8+
if (locals.user) {
9+
const redirect_url = url.searchParams.get('redirect') || '/'
10+
throw redirect(302, redirect_url)
11+
}
912
}
1013

1114
export const actions: Actions = {
@@ -27,7 +30,7 @@ export const actions: Actions = {
2730
const auth_token = await db.authToken.upsert({
2831
where: { user_id: user.id },
2932
update: { token: crypto.randomUUID() },
30-
create: { user_id: user.id, token: crypto.randomUUID() }
33+
create: { user_id: user.id, token: crypto.randomUUID() },
3134
})
3235

3336
cookies.set('session_id', auth_token.token, {

src/routes/main/+page.server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { redirect } from "@sveltejs/kit";
22
import type { PageServerLoad } from "./$types";
33

4-
export const load: PageServerLoad = async ({ locals }) => {
5-
if (!locals.user) throw redirect(302, '/')
4+
// NOTE: https://github.com/sveltejs/kit/issues/3912
5+
6+
export const load: PageServerLoad = async ({ locals, url }) => {
7+
if (!locals.user) throw redirect(302, `/login?redirect=${url.pathname}`)
68
}

0 commit comments

Comments
 (0)