feat: build the /admin and /admin/login pages
This commit is contained in:
parent
203e7c4b27
commit
1d24bde610
4 changed files with 53 additions and 0 deletions
16
src/routes/admin/+layout.server.ts
Normal file
16
src/routes/admin/+layout.server.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { redirect } from "@sveltejs/kit";
|
||||
import type { LayoutServerLoad } from "./$types.js";
|
||||
import { CookieAuthentication } from "$lib/blog/auth/CookieAuthentication.js";
|
||||
|
||||
export const load: LayoutServerLoad = ({ cookies, route }) => {
|
||||
const auth = new CookieAuthentication(cookies)
|
||||
const isAuthd = auth.isAuthdAsAdmin
|
||||
|
||||
if (route.id === '/admin/login' && isAuthd) {
|
||||
return redirect(307, '/admin')
|
||||
} else if (!isAuthd && route.id !== '/admin/login') {
|
||||
return redirect(307, '/admin/login')
|
||||
}
|
||||
|
||||
return {}
|
||||
}
|
||||
1
src/routes/admin/+page.svelte
Normal file
1
src/routes/admin/+page.svelte
Normal file
|
|
@ -0,0 +1 @@
|
|||
<h1>Welcome home, Wilson</h1>
|
||||
25
src/routes/admin/login/+page.server.ts
Normal file
25
src/routes/admin/login/+page.server.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { PRIVATE_ADMIN_AUTH_TOKEN } from "$env/static/private";
|
||||
import { redirect } from "@sveltejs/kit";
|
||||
import type { Actions} from "./$types.js";
|
||||
import { CookieAuthentication } from "$lib/blog/auth/CookieAuthentication.js";
|
||||
|
||||
export const actions = {
|
||||
default: async ({cookies, request}) => {
|
||||
const formData = await request.formData()
|
||||
const token = formData.get('token')
|
||||
|
||||
const isAuthd = PRIVATE_ADMIN_AUTH_TOKEN === token;
|
||||
const auth = new CookieAuthentication(cookies)
|
||||
|
||||
auth.setAdminAuthentication(isAuthd)
|
||||
|
||||
if (isAuthd) {
|
||||
return redirect(307, '/admin')
|
||||
}
|
||||
|
||||
return {
|
||||
isAuthd
|
||||
}
|
||||
}
|
||||
|
||||
} satisfies Actions
|
||||
11
src/routes/admin/login/+page.svelte
Normal file
11
src/routes/admin/login/+page.svelte
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<h1>Admin login</h1>
|
||||
|
||||
<form method="POST">
|
||||
<div class="field">
|
||||
<label for="token">API Token</label>
|
||||
<input type="password" id="token" name="token" />
|
||||
</div>
|
||||
<div class="field">
|
||||
<input type="submit" value="Login">
|
||||
</div>
|
||||
</form>
|
||||
Loading…
Reference in a new issue