File tree Expand file tree Collapse file tree 3 files changed +22
-21
lines changed Expand file tree Collapse file tree 3 files changed +22
-21
lines changed Original file line number Diff line number Diff line change @@ -4,22 +4,6 @@ import { HEADER_NAMES } from '$lib/constants/headers';
4
4
import type { Handle , HandleFetch } from '@sveltejs/kit' ;
5
5
6
6
export const handle : Handle = async ( { event, resolve } ) => {
7
- const accessTokenFromCookies = event . cookies . get ( AUTH_COOKIE_NAME ) ;
8
-
9
- event . locals . accessToken = accessTokenFromCookies ;
10
-
11
- if ( ! accessTokenFromCookies ) {
12
- if ( ! event . url . pathname . startsWith ( '/signin' ) ) {
13
- return Response . redirect ( `${ event . url . origin } /signin` , 301 ) ;
14
- }
15
- }
16
-
17
- if ( event . url . pathname . startsWith ( '/signin' ) ) {
18
- if ( accessTokenFromCookies ) {
19
- return Response . redirect ( `${ event . url . origin } ` ) ;
20
- }
21
- }
22
-
23
7
// erase token cookie
24
8
if ( event . url . pathname === '/logout' ) {
25
9
event . cookies . set ( AUTH_COOKIE_NAME , String ( ) , AUTH_COOKIE_ERASE_OPTIONS ) ;
Original file line number Diff line number Diff line change 1
1
import type { LayoutServerLoad } from './$types' ;
2
2
import { UserService } from '$lib/services' ;
3
+ import { AUTH_COOKIE_NAME } from '$lib/constants/auth' ;
4
+ import { redirect } from '@sveltejs/kit' ;
3
5
4
- export const load : LayoutServerLoad = async ( { locals, fetch } ) => {
5
- const userService = new UserService ( fetch ) ;
6
- const authenticatedUser = await userService . getAuthenticatedUser ( ) ;
7
- locals . user = authenticatedUser ;
8
- return authenticatedUser ;
6
+ export const load : LayoutServerLoad = async ( { locals, fetch, cookies } ) => {
7
+ const token = cookies . get ( AUTH_COOKIE_NAME ) ;
8
+ if ( token ) {
9
+ const userService = new UserService ( fetch ) ;
10
+ const authenticatedUser = await userService . getAuthenticatedUser ( ) ;
11
+ locals . user = authenticatedUser ;
12
+ return authenticatedUser ;
13
+ } else {
14
+ throw redirect ( 303 , '/signin' ) ;
15
+ }
9
16
} ;
Original file line number Diff line number Diff line change
1
+ import type { LayoutServerLoad } from '../../$types' ;
2
+ import { AUTH_COOKIE_NAME } from '$lib/constants/auth' ;
3
+ import { redirect } from '@sveltejs/kit' ;
4
+
5
+ export const load : LayoutServerLoad = async ( { cookies } ) => {
6
+ const token = cookies . get ( AUTH_COOKIE_NAME ) ;
7
+ if ( token ) {
8
+ throw redirect ( 303 , '/' ) ;
9
+ }
10
+ } ;
You can’t perform that action at this time.
0 commit comments