mirror of
https://github.com/bartvdbraak/omnidash.git
synced 2025-04-28 07:51:20 +00:00
feat: add auth locations
This commit is contained in:
parent
b03dcbaf2d
commit
9ef0e46f5f
13 changed files with 200 additions and 95 deletions
|
@ -16,8 +16,20 @@ export const navConfig: NavConfig = {
|
||||||
href: '/login'
|
href: '/login'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Signup',
|
title: 'Register',
|
||||||
href: '/signup'
|
href: '/register'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Dashboard',
|
||||||
|
href: '/dashboard'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Settings',
|
||||||
|
href: '/settings'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Logout',
|
||||||
|
href: '/logout'
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
sidebarNav: []
|
sidebarNav: []
|
||||||
|
|
|
@ -1,46 +1,22 @@
|
||||||
import { redirect } from '@sveltejs/kit';
|
import { error, redirect } from '@sveltejs/kit';
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
default: async ({ request, locals }: { request: Request; locals: App.Locals }) => {
|
login: async ({ request, locals }: { request: Request; locals: App.Locals }) => {
|
||||||
if (locals.pocketBase.authStore.isValid) {
|
const body = Object.fromEntries(await request.formData());
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const formData = await request.formData();
|
|
||||||
|
|
||||||
const email = formData.get('email');
|
|
||||||
const password = formData.get('password');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (typeof email !== 'string') {
|
const email = body.email.toString();
|
||||||
throw new Error('Email must be a string');
|
const password = body.password.toString();
|
||||||
}
|
|
||||||
|
|
||||||
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');
|
|
||||||
}
|
|
||||||
|
|
||||||
await locals.pocketBase.collection('users').authWithPassword(email, password);
|
await locals.pocketBase.collection('users').authWithPassword(email, password);
|
||||||
} catch (error) {
|
if (!locals.pocketBase?.authStore?.model?.verified) {
|
||||||
console.error(error);
|
locals.pocketBase.authStore.clear();
|
||||||
|
|
||||||
if (!(error instanceof Error)) {
|
|
||||||
return {
|
return {
|
||||||
email,
|
notVerified: true
|
||||||
password,
|
|
||||||
error: 'Unknown error occured when signing up user'
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
return { error: error.message, email, password };
|
console.log('Error: ', err);
|
||||||
|
throw error(500, 'Something went wrong logging in');
|
||||||
}
|
}
|
||||||
|
|
||||||
throw redirect(303, '/');
|
throw redirect(303, '/');
|
||||||
|
|
|
@ -1,25 +1,59 @@
|
||||||
<script lang="ts">
|
<script>
|
||||||
import type { ActionData } from './$types';
|
export let form;
|
||||||
|
|
||||||
export let form: ActionData;
|
|
||||||
export const data = {
|
|
||||||
title: 'Log in'
|
|
||||||
};
|
|
||||||
</script>
|
</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">
|
<div class="w-full max-w-md pt-2">
|
||||||
<input type="email" name="email" placeholder="E-mail" value={form?.email ?? ''} />
|
<button type="submit" class="btn btn-primary w-full">Login</button>
|
||||||
<input type="password" name="password" placeholder="Password" value={form?.password ?? ''} />
|
</div>
|
||||||
|
{#if form?.notVerified}
|
||||||
<button type="submit">Log in</button>
|
<div class="alert alert-error shadow-lg w-full max-w-md">
|
||||||
</form>
|
<div>
|
||||||
|
<svg
|
||||||
{#if form?.error}
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
<p>{form.error}</p>
|
class="stroke-current flex-shrink-0 h-6 w-6"
|
||||||
{/if}
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
<p>Don't have an account?</p>
|
><path
|
||||||
|
stroke-linecap="round"
|
||||||
<a href="/signup">Sign up</a>
|
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>
|
6
apps/web/src/routes/(auth)/logout/+server.ts
Normal file
6
apps/web/src/routes/(auth)/logout/+server.ts
Normal 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');
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
import { redirect } from '@sveltejs/kit';
|
import { redirect } from '@sveltejs/kit';
|
||||||
|
|
||||||
export const actions = {
|
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) {
|
if (locals.pocketBase.authStore.isValid) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
39
apps/web/src/routes/(auth)/register/+page.svelte
Normal file
39
apps/web/src/routes/(auth)/register/+page.svelte
Normal 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>
|
18
apps/web/src/routes/(auth)/reset-password/+page.server.ts
Normal file
18
apps/web/src/routes/(auth)/reset-password/+page.server.ts
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
44
apps/web/src/routes/(auth)/reset-password/+page.svelte
Normal file
44
apps/web/src/routes/(auth)/reset-password/+page.svelte
Normal 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>
|
|
@ -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>
|
|
|
@ -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;
|
|
9
apps/web/src/routes/(dashboard)/+layout.server.ts
Normal file
9
apps/web/src/routes/(dashboard)/+layout.server.ts
Normal 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 {};
|
||||||
|
})
|
Loading…
Reference in a new issue