-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add login page (no passwords, no per-user db yet)
- Loading branch information
1 parent
8e0740d
commit ab7a642
Showing
9 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
// See https://kit.svelte.dev/docs/types#app | ||
// for information about these interfaces | ||
import type { Session } from 'svelte-kit-cookie-session'; | ||
type SessionData = { | ||
username: string; | ||
}; | ||
declare global { | ||
namespace App { | ||
// interface Error {} | ||
// interface Locals {} | ||
interface Locals { | ||
session: Session<SessionData>; | ||
} | ||
interface PageData { | ||
title: string; | ||
session: SessionData; | ||
} | ||
// interface Platform {} | ||
} | ||
} | ||
|
||
export {}; | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,22 @@ | ||
import dayjs from "dayjs"; | ||
import utc from "dayjs/plugin/utc"; | ||
import timezone from "dayjs/plugin/timezone"; | ||
import { handleSession } from "svelte-kit-cookie-session"; | ||
import { redirect } from "@sveltejs/kit"; | ||
|
||
dayjs.extend(utc); | ||
dayjs.extend(timezone); | ||
|
||
export const handle = handleSession( | ||
{ | ||
secret: "SOME_COMPLEX_SECRET_32_CHARSLONG", | ||
rolling: true, | ||
}, | ||
({ event, resolve }) => { | ||
if (event.url.pathname !== "/login" && !event.locals.session.data.username) { | ||
throw redirect(302, "/login"); | ||
} | ||
return resolve(event); | ||
}, | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { z } from "zod"; | ||
import type { Actions, PageServerLoad } from "./$types"; | ||
import { redirect } from "@sveltejs/kit"; | ||
import { zx } from "zodix"; | ||
|
||
const loginRequestSchema = z.object({ | ||
username: z.string(), | ||
}); | ||
|
||
export const load = (async ({ locals }) => { | ||
if (locals.session.data.username) { | ||
throw redirect(302, "/"); | ||
} | ||
return { | ||
title: "Login", | ||
session: locals.session.data, | ||
}; | ||
}) satisfies PageServerLoad; | ||
|
||
export const actions: Actions = { | ||
default: async ({ locals, request }) => { | ||
const formData = await zx.parseForm(request, loginRequestSchema); | ||
await locals.session.set({ username: formData.username }); | ||
return {}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script lang="ts"> | ||
import type { PageData } from "./$types"; | ||
export let data: PageData; | ||
</script> | ||
|
||
<form method="POST"> | ||
<select name="username"> | ||
<option value="dan">Dan</option> | ||
<option value="nit">Nit</option> | ||
</select> | ||
<button type="submit">Login</button> | ||
</form> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { redirect } from "@sveltejs/kit"; | ||
import type { PageServerLoad } from "./$types"; | ||
|
||
export const load: PageServerLoad = async ({ locals }) => { | ||
locals.session.destroy(); | ||
throw redirect(302, "/login"); | ||
}; |