File tree Expand file tree Collapse file tree 7 files changed +54
-9
lines changed Expand file tree Collapse file tree 7 files changed +54
-9
lines changed Original file line number Diff line number Diff line change 1+ import type { LayoutServerLoad } from './$types'
2+
3+ export const load : LayoutServerLoad = async ( { locals } ) => {
4+ return {
5+ user : locals . user ,
6+ }
7+ }
Original file line number Diff line number Diff line change 1+ <script lang =" ts" >
2+ import { page } from ' $app/stores'
3+ </script >
4+
5+ <svelte:head >
6+ <title > SvelteKit Authentication</title >
7+ </svelte:head >
8+
9+ <nav >
10+ {#if $page .data .user }
11+ <!-- <a href="/admin">Admin</a> -->
12+
13+ <form action =" /logout" method =" POST" >
14+ <button type =" submit" >Log out</button >
15+ </form >
16+ {:else }
17+ <a href =" /login" >Log in</a >
18+ <a href =" /register" >Register</a >
19+ {/if }
20+ </nav >
21+
22+ <main >
23+ <slot />
24+ </main >
Original file line number Diff line number Diff line change 11import type { PageServerLoad } from "./$types" ;
22
33export const load : PageServerLoad = async ( event ) => {
4- console . log ( event )
4+ // console.log(event)
55}
Original file line number Diff line number Diff line change 11<h1 >Welcome to SvelteKit Authentication</h1 >
22<p >Visit <a href =" https://github.com/sinProject-Inc/sveltekit_authentication" >GitHub Repository</a > to read the documentation</p >
33
4- <button on:click ={() => window .location .href = ' /logout' }>Log out</button >
Original file line number Diff line number Diff line change 1+ import { goto } from '$app/navigation'
12import { db } from '$lib/database'
23import type { Actions , PageServerLoad } from '.svelte-kit/types/src/routes/register/$types'
34import { invalid , redirect } from '@sveltejs/kit'
45import bcrypt from 'bcrypt'
56
6- export const load : PageServerLoad = async ( ) => {
7- // todo
7+ export const load : PageServerLoad = async ( { locals } ) => {
8+ if ( locals . user ) throw redirect ( 302 , '/' )
89}
910
1011export const actions : Actions = {
@@ -37,7 +38,5 @@ export const actions: Actions = {
3738 // secure: process.env.NODE_ENV === 'production',
3839 httpOnly : true ,
3940 } )
40-
41- throw redirect ( 303 , '/' )
4241 } ,
4342}
Original file line number Diff line number Diff line change 1+ import { redirect , type Actions } from "@sveltejs/kit" ;
2+ import type { PageServerLoad } from "./$types" ;
3+
4+ export const load : PageServerLoad = async ( ) => {
5+ throw redirect ( 302 , "/" )
6+ }
7+
8+ export const actions : Actions = {
9+ default : async ( { cookies } ) => {
10+ cookies . set ( 'session_id' , '' , {
11+ path : '/' ,
12+ expires : new Date ( 0 ) ,
13+ } )
14+
15+ throw redirect ( 302 , '/login' )
16+ }
17+ }
Original file line number Diff line number Diff line change @@ -8,8 +8,8 @@ enum Roles {
88 user = 'user' ,
99}
1010
11- export const load : PageServerLoad = async ( ) => {
12- // todo
11+ export const load : PageServerLoad = async ( { locals } ) => {
12+ if ( locals . user ) throw redirect ( 302 , '/' )
1313}
1414
1515export const actions : Actions = {
@@ -40,4 +40,3 @@ export const actions: Actions = {
4040 throw redirect ( 303 , '/login' )
4141 } ,
4242}
43-
You can’t perform that action at this time.
0 commit comments