feat: add auth locations

This commit is contained in:
Bart van der Braak 2024-01-29 13:50:14 +01:00
parent b03dcbaf2d
commit 9ef0e46f5f
13 changed files with 200 additions and 95 deletions

View file

@ -16,8 +16,20 @@ export const navConfig: NavConfig = {
href: '/login'
},
{
title: 'Signup',
href: '/signup'
title: 'Register',
href: '/register'
},
{
title: 'Dashboard',
href: '/dashboard'
},
{
title: 'Settings',
href: '/settings'
},
{
title: 'Logout',
href: '/logout'
}
],
sidebarNav: []

View file

@ -1,48 +1,24 @@
import { redirect } from '@sveltejs/kit';
import { error, redirect } from '@sveltejs/kit';
export const actions = {
default: async ({ request, locals }: { request: Request; locals: App.Locals }) => {
if (locals.pocketBase.authStore.isValid) {
return;
}
const formData = await request.formData();
const email = formData.get('email');
const password = formData.get('password');
login: async ({ request, locals }: { request: Request; locals: App.Locals }) => {
const body = Object.fromEntries(await request.formData());
try {
if (typeof email !== 'string') {
throw new Error('Email must be a string');
}
if (email.length < 5) {
throw new Error('Please enter a valid e-mail address');
}
if (typeof password !== 'string') {
throw new Error('Password must be a string');
}
if (password.length < 8) {
throw new Error('Password must be at least 8 characters in length');
}
const email = body.email.toString();
const password = body.password.toString();
await locals.pocketBase.collection('users').authWithPassword(email, password);
} catch (error) {
console.error(error);
if (!(error instanceof Error)) {
if (!locals.pocketBase?.authStore?.model?.verified) {
locals.pocketBase.authStore.clear();
return {
email,
password,
error: 'Unknown error occured when signing up user'
notVerified: true
};
}
return { error: error.message, email, password };
} catch (err) {
console.log('Error: ', err);
throw error(500, 'Something went wrong logging in');
}
throw redirect(303, '/');
}
};
};

View file

@ -1,25 +1,59 @@
<script lang="ts">
import type { ActionData } from './$types';
export let form: ActionData;
export const data = {
title: 'Log in'
};
<script>
export let form;
</script>
<h1>Log in</h1>
<div class="flex flex-col items-center h-full w-full">
<h2 class="mt-2 text-center text-3xl font-bold tracking-tight text-base-content">
Login to your account
</h2>
<p class="text-center mt-1">
Or <a href="/register" class="text-primary font-medium hover:cursor-pointer hover:underline"
>register</a
> if you don't already have an account.
</p>
<form action="?/login" method="POST" class="flex flex-col items-center space-y-2 w-full pt-4">
<div class="form-control w-full max-w-md">
<label for="email" class="label font-medium pb-1">
<span class="label-text">Email</span>
</label>
<input type="email" name="email" class="input input-bordered w-full max-w-md" />
</div>
<div class="form-control w-full max-w-md">
<label for="password" class="label font-medium pb-1">
<span class="label-text">Password</span>
</label>
<input type="password" name="password" class="input input-bordered w-full max-w-md" />
</div>
<div class="w-full max-w-md">
<a
href="/reset-password"
class="font-medium text-primary hover:cursor-pointer hover:underline"
>
Forgot Password?</a
>
</div>
<form method="POST">
<input type="email" name="email" placeholder="E-mail" value={form?.email ?? ''} />
<input type="password" name="password" placeholder="Password" value={form?.password ?? ''} />
<button type="submit">Log in</button>
</form>
{#if form?.error}
<p>{form.error}</p>
{/if}
<p>Don't have an account?</p>
<a href="/signup">Sign up</a>
<div class="w-full max-w-md pt-2">
<button type="submit" class="btn btn-primary w-full">Login</button>
</div>
{#if form?.notVerified}
<div class="alert alert-error shadow-lg w-full max-w-md">
<div>
<svg
xmlns="http://www.w3.org/2000/svg"
class="stroke-current flex-shrink-0 h-6 w-6"
fill="none"
viewBox="0 0 24 24"
><path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"
/></svg
>
<span>You must verify your email before you can login.</span>
</div>
</div>
{/if}
</form>
</div>

View file

@ -0,0 +1,6 @@
import { redirect } from "@sveltejs/kit";
export const GET = ({locals}: {locals: App.Locals}) => {
locals.pocketBase.authStore.clear();
throw redirect(303, '/login');
}

View file

@ -1,7 +1,7 @@
import { redirect } from '@sveltejs/kit';
export const actions = {
default: async ({ request, locals }: { request: Request; locals: App.Locals }) => {
register: async ({ request, locals }: { request: Request; locals: App.Locals }) => {
if (locals.pocketBase.authStore.isValid) {
return;
}

View file

@ -0,0 +1,39 @@
<div class="flex flex-col items-center h-full w-full">
<h2 class="mt-2 text-center text-3xl font-bold tracking-tight text-base-content">
Register for an account
</h2>
<p class="text-center mt-1">
Or <a href="/login" class="text-primary font-medium hover:cursor-pointer hover:underline"
>sign in</a
> if you already have an account.
</p>
<form action="?/register" method="POST" class="flex flex-col items-center space-y-2 w-full pt-4">
<div class="form-control w-full max-w-md">
<label for="name" class="label font-medium pb-1">
<span class="label-text">Name</span>
</label>
<input type="text" name="name" class="input input-bordered w-full max-w-md" />
</div>
<div class="form-control w-full max-w-md">
<label for="email" class="label font-medium pb-1">
<span class="label-text">Email</span>
</label>
<input type="email" name="email" class="input input-bordered w-full max-w-md" />
</div>
<div class="form-control w-full max-w-md">
<label for="password" class="label font-medium pb-1">
<span class="label-text">Password</span>
</label>
<input type="password" name="password" class="input input-bordered w-full max-w-md" />
</div>
<div class="form-control w-full max-w-md">
<label for="passwordConfirm" class="label font-medium pb-1">
<span class="label-text">Confirm Password</span>
</label>
<input type="password" name="passwordConfirm" class="input input-bordered w-full max-w-md" />
</div>
<div class="w-full max-w-md pt-2">
<button type="submit" class="btn btn-primary w-full">Register</button>
</div>
</form>
</div>

View file

@ -0,0 +1,18 @@
import { error } from '@sveltejs/kit';
export const actions = {
resetPassword: async ({ request, locals }: { request: Request; locals: App.Locals }) => {
const body = Object.fromEntries(await request.formData());
try {
const email = body.email.toString();
await locals.pocketBase.collection('users').requestPasswordReset(email);
return {
success: true
};
} catch (err) {
console.log('Error: ', err);
throw error(500, 'Something went wrong');
}
}
};

View file

@ -0,0 +1,44 @@
<script>
export let form;
</script>
<div class="flex flex-col items-center h-full w-full">
<h2 class="mt-2 text-center text-3xl font-bold tracking-tight text-base-content">
Reset Your Password
</h2>
<p class="text-center mt-1">We'll send you an email with a link to reset your password.</p>
<form
action="?/resetPassword"
method="POST"
class="flex flex-col items-center space-y-2 w-full pt-4"
>
<div class="form-control w-full max-w-md">
<label for="email" class="label font-medium pb-1">
<span class="label-text">Email</span>
</label>
<input type="email" name="email" class="input input-bordered w-full max-w-md" />
</div>
<div class="w-full max-w-md pt-2">
<button type="submit" class="btn btn-primary w-full">Request Password Reset</button>
</div>
{#if form?.success}
<div class="alert alert-success shadow-lg w-full max-w-md">
<div>
<svg
xmlns="http://www.w3.org/2000/svg"
class="stroke-current flex-shrink-0 h-6 w-6"
fill="none"
viewBox="0 0 24 24"
><path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
/></svg
>
<span>An email has been sent to reset your password!</span>
</div>
</div>
{/if}
</form>
</div>

View file

@ -1,23 +0,0 @@
<script lang="ts">
import type { ActionData } from './$types';
export let form: ActionData;
</script>
<h1>Sign up</h1>
<form method="POST">
<input type="text" name="name" placeholder="name" value={form?.name ?? ''} />
<input type="email" name="email" placeholder="E-mail" value={form?.email ?? ''} />
<input type="password" name="password" placeholder="Password" value={form?.password ?? ''} />
<button type="submit">Sign up</button>
</form>
{#if form?.error}
<p>{form.error}</p>
{/if}
<p>Already have an account?</p>
<a href="/login">Log in</a>

View file

@ -1,10 +0,0 @@
import { redirect } from '@sveltejs/kit';
import type { LayoutServerLoad } from './$types';
export const load = (async ({ locals }) => {
if (!locals.pocketBase.authStore.isValid) {
throw redirect(303, '/signup');
}
return {};
}) satisfies LayoutServerLoad;

View file

@ -0,0 +1,9 @@
import { redirect } from '@sveltejs/kit';
export const load = (async ({ locals }: { locals: App.Locals }) => {
if (!locals.pocketBase.authStore.isValid) {
throw redirect(303, '/register');
}
return {};
})

View file

@ -4,4 +4,4 @@ export const load = (async ({ locals }) => {
return {
authenticated: locals.pocketBase.authStore.isValid
};
}) satisfies PageServerLoad;
}) satisfies PageServerLoad;