feat: auth fully implemented

This commit is contained in:
Bart van der Braak 2024-02-06 21:00:38 +01:00
parent 7c408bd19c
commit af5267d97a
11 changed files with 177 additions and 230 deletions

View file

@ -32,6 +32,80 @@ export const actions = {
throw redirect(303, '/');
},
register: async ({ request, locals }: { request: Request; locals: App.Locals }) => {
if (locals.pocketBase.authStore.isValid) {
return;
}
const formData = await request.formData();
const name = formData.get('name');
const email = formData.get('email');
const password = formData.get('password');
const passwordConfirm = formData.get('passwordConfirm');
try {
if (typeof name !== 'string') {
throw new Error('Name must be a string');
}
if (name.length === 0) {
throw new Error('Please enter a valid name');
}
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');
}
if (password !== passwordConfirm) {
throw new Error('Passwords do not match');
}
await locals.pocketBase.collection('users').create({
name,
email,
password,
passwordConfirm
});
await locals.pocketBase.collection('users').authWithPassword(email, password);
if (!locals.pocketBase?.authStore?.model?.verified) {
locals.pocketBase.authStore.clear();
return {
showLogin: true,
isLoading: false,
notVerified: true
};
}
} catch (error) {
console.error(error);
if (!(error instanceof Error)) {
return {
name,
email,
password,
error: 'Unknown error occured when signing up user'
};
}
return { error: error.message, name, email, password };
}
throw redirect(303, '/');
},
oauth2: async ({ request, cookies }) => {
const form = await request.formData();
const token = form.get('token');

View file

@ -44,7 +44,7 @@
<div class="lg:p-8">
<Tabs.Root
value="login"
value={form?.showLogin ? 'login' : undefined}
class="mx-auto flex w-full flex-col justify-center space-y-6 sm:w-[350px]"
>
<Tabs.List class="grid w-full grid-cols-2">
@ -54,7 +54,7 @@
<Tabs.Content value="login">
<div class="flex flex-col space-y-2 text-center">
<h1 class="text-2xl font-semibold tracking-tight">Log into your account</h1>
<p class="text-muted-foreground text-sm pb-4">
<p class="pb-6 text-sm text-muted-foreground">
Enter your credentials below to log into your account
</p>
</div>
@ -64,6 +64,10 @@
action="?/login"
use:enhance={() => {
isLoading = true;
return async ({ update }) => {
isLoading = false;
update();
};
}}
>
<div class="grid gap-2">
@ -91,7 +95,7 @@
</Button>
</div>
{#if form?.notVerified}
<Alert.Root>
<Alert.Root class="mt-4">
<Alert.Title></Alert.Title>
<Alert.Description>You must verify your email before you can login.</Alert.Description
>
@ -101,7 +105,65 @@
</div>
</Tabs.Content>
<Tabs.Content value="register">
<div class="flex flex-col space-y-2 text-center">
<h1 class="text-2xl font-semibold tracking-tight">Create your account</h1>
<p class="pb-6 text-sm text-muted-foreground">
Enter your details below to create a new account
</p>
</div>
<div class={cn('grid gap-6')} {...$$restProps}>
<form
method="POST"
action="?/register"
use:enhance={() => {
isLoading = true;
return async ({ update }) => {
isLoading = false;
update();
};
}}
>
<div class="grid gap-2">
<div class="grid gap-2">
<Label for="email">Name</Label>
<Input id="name" name="name" type="name" disabled={isLoading} />
</div>
<div class="grid gap-2">
<Label for="email">Email</Label>
<Input
id="email"
name="email"
type="email"
autocapitalize="none"
autocomplete="email"
autocorrect="off"
disabled={isLoading}
/>
</div>
<div class="grid gap-2">
<Label for="password">Password</Label>
<Input id="password" name="password" type="password" disabled={isLoading} />
</div>
<div class="grid gap-2">
<Label for="password">Confirm password</Label>
<Input id="password" name="passwordConfirm" type="password" disabled={isLoading} />
</div>
<Button type="submit" disabled={isLoading}>
{#if isLoading}
<Icons.spinner class="mr-2 h-4 w-4 animate-spin" />
{/if}
Register
</Button>
</div>
{#if form?.notVerified}
<Alert.Root class="mt-4">
<Alert.Title></Alert.Title>
<Alert.Description>You must verify your email before you can login.</Alert.Description
>
</Alert.Root>
{/if}
</form>
</div>
</Tabs.Content>
{#if providers.length}
<form
@ -110,6 +172,10 @@
bind:this={oauth2Form}
use:enhance={() => {
isLoading = true;
return async ({ update }) => {
isLoading = false;
update();
};
}}
>
<div class="relative">
@ -117,11 +183,11 @@
<span class="w-full border-t" />
</div>
<div class="relative flex justify-center text-xs uppercase">
<span class="bg-background text-muted-foreground px-2 py-6"> Or continue with </span>
<span class="bg-background px-2 py-4 text-muted-foreground"> Or continue with </span>
</div>
</div>
<div
class="border-input hover:bg-accent hover:text-accent-foreground focus-visible:ring-ring flex items-center justify-between whitespace-nowrap rounded-md border bg-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-1 disabled:pointer-events-none disabled:opacity-50"
class="flex items-center justify-between whitespace-nowrap rounded-md border border-input bg-transparent shadow-sm transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50"
>
<input type="hidden" name="provider" bind:value={currentProvider.name} />
<div class="flex w-full items-center justify-center space-x-2">
@ -148,7 +214,7 @@
</div>
{#if providers.length > 1}
<div class="flex items-center space-x-2">
<Separator orientation="vertical" class="bg-secondary h-[20px]" />
<Separator orientation="vertical" class="h-[20px] bg-secondary" />
<div class="flex items-center space-x-2">
<DropdownMenu.Root>
<DropdownMenu.Trigger asChild let:builder>
@ -187,7 +253,7 @@
{/if}
</Tabs.Root>
<p class="text-muted-foreground px-8 py-2 text-center text-xs">
<p class="px-8 py-2 text-center text-xs text-muted-foreground">
Don't have an account? <a class="text-primary underline" href="/register">Sign up.</a> <br />
Forgot password? <a class="text-primary underline" href="/reset-password">Reset password.</a>
</p>

View file

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

View file

@ -1,69 +0,0 @@
import { 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 name = formData.get('name');
const email = formData.get('email');
const password = formData.get('password');
try {
if (typeof name !== 'string') {
throw new Error('Name must be a string');
}
if (name.length === 0) {
throw new Error('Please enter a valid name');
}
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');
}
if (password !== formData.get('passwordConfirm')) {
throw new Error('Passwords do not match');
}
await locals.pocketBase.collection('users').create({
email,
password,
name,
passwordConfirm: password
});
await locals.pocketBase.collection('users').authWithPassword(email, password);
} catch (error) {
console.error(error);
if (!(error instanceof Error)) {
return {
name,
email,
password,
error: 'Unknown error occured when signing up user'
};
}
return { error: error.message, name, email, password };
}
throw redirect(303, '/');
}
};

View file

@ -1,95 +0,0 @@
<script lang="ts">
import { enhance } from '$app/forms';
import { Icons } from '$lib/components/site/icons';
import { Button } from '$lib/components/ui/button';
import { Input } from '$lib/components/ui/input';
import { Label } from '$lib/components/ui/label';
import { cn } from '$lib/utils';
let isLoading = false;
</script>
<div class="lg:p-8">
<div class="mx-auto flex w-full flex-col justify-center space-y-6 sm:w-[350px]">
<div class="flex flex-col space-y-2 text-center">
<h1 class="text-2xl font-semibold tracking-tight">Create your account</h1>
<p class="text-sm text-muted-foreground">Enter your details below to create a new account</p>
</div>
<div class={cn('grid gap-6')} {...$$restProps}>
<form
method="POST"
use:enhance={() => {
isLoading = true;
}}
>
<div class="grid gap-2">
<div class="grid gap-1">
<Label class="sr-only" for="email">Name</Label>
<Input id="name" name="name" placeholder="Name" type="name" disabled={isLoading} />
</div>
<div class="grid gap-1">
<Label class="sr-only" for="email">Email</Label>
<Input
id="email"
name="email"
placeholder="name@example.com"
type="email"
autocapitalize="none"
autocomplete="email"
autocorrect="off"
disabled={isLoading}
/>
</div>
<div class="grid gap-1">
<Label class="sr-only" for="password">Password</Label>
<Input
id="password"
name="password"
type="password"
disabled={isLoading}
placeholder="Password"
/>
</div>
<div class="grid gap-1">
<Label class="sr-only" for="password">Confirm Password</Label>
<Input
id="password"
name="passwordConfirm"
type="password"
disabled={isLoading}
placeholder="Confirm password"
/>
</div>
<Button type="submit" disabled={isLoading} on:click={() => (isLoading = true)}>
{#if isLoading}
<Icons.spinner class="mr-2 h-4 w-4 animate-spin" />
{/if}
Sign In
</Button>
</div>
</form>
<div class="relative">
<div class="absolute inset-0 flex items-center">
<span class="w-full border-t" />
</div>
<div class="relative flex justify-center text-xs uppercase">
<span class="bg-background px-2 text-muted-foreground"> Or continue with </span>
</div>
</div>
<Button variant="outline" type="button" disabled={isLoading}>
{#if isLoading}
<Icons.spinner class="mr-2 h-4 w-4 animate-spin" />
{:else}
<Icons.microsoft class="mr-2 h-4 w-4" />
{/if}
{' '}
Microsoft
</Button>
</div>
<p class="px-8 text-center text-sm text-muted-foreground">
Or <a class="text-primary underline" href="/login">sign in</a> if you already have an account.<br
/>
Forgot password? <a class="text-primary underline" href="/reset-password">Reset password.</a>
</p>
</div>
</div>

View file

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