Working
This commit is contained in:
11
src/routes/auth/+layout.svelte
Normal file
11
src/routes/auth/+layout.svelte
Normal file
@@ -0,0 +1,11 @@
|
||||
<script>
|
||||
let { children } = $props()
|
||||
</script>
|
||||
|
||||
<header>
|
||||
<nav>
|
||||
<a href="/">Home</a>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
{@render children()}
|
||||
32
src/routes/auth/+page.server.ts
Normal file
32
src/routes/auth/+page.server.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { redirect } from '@sveltejs/kit'
|
||||
|
||||
import type { Actions } from './$types'
|
||||
|
||||
export const actions: Actions = {
|
||||
signup: async ({ request, locals: { supabase } }) => {
|
||||
const formData = await request.formData()
|
||||
const email = formData.get('email') as string
|
||||
const password = formData.get('password') as string
|
||||
|
||||
const { error } = await supabase.auth.signUp({ email, password })
|
||||
if (error) {
|
||||
console.error(error)
|
||||
redirect(303, '/auth/error')
|
||||
} else {
|
||||
redirect(303, '/')
|
||||
}
|
||||
},
|
||||
login: async ({ request, locals: { supabase } }) => {
|
||||
const formData = await request.formData()
|
||||
const email = formData.get('email') as string
|
||||
const password = formData.get('password') as string
|
||||
|
||||
const { error } = await supabase.auth.signInWithPassword({ email, password })
|
||||
if (error) {
|
||||
console.error(error)
|
||||
redirect(303, '/auth/error')
|
||||
} else {
|
||||
redirect(303, '/private/home')
|
||||
}
|
||||
},
|
||||
}
|
||||
12
src/routes/auth/+page.svelte
Normal file
12
src/routes/auth/+page.svelte
Normal file
@@ -0,0 +1,12 @@
|
||||
<form method="POST" action="?/login">
|
||||
<label>
|
||||
Email
|
||||
<input name="email" type="email" />
|
||||
</label>
|
||||
<label>
|
||||
Password
|
||||
<input name="password" type="password" />
|
||||
</label>
|
||||
<button>Login</button>
|
||||
<button formaction="?/signup">Sign up</button>
|
||||
</form>
|
||||
31
src/routes/auth/confirm/+server.ts
Normal file
31
src/routes/auth/confirm/+server.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { EmailOtpType } from '@supabase/supabase-js'
|
||||
import { redirect } from '@sveltejs/kit'
|
||||
|
||||
import type { RequestHandler } from './$types'
|
||||
|
||||
export const GET: RequestHandler = async ({ url, locals: { supabase } }) => {
|
||||
const token_hash = url.searchParams.get('token_hash')
|
||||
const type = url.searchParams.get('type') as EmailOtpType | null
|
||||
const next = url.searchParams.get('next') ?? '/'
|
||||
|
||||
/**
|
||||
* Clean up the redirect URL by deleting the Auth flow parameters.
|
||||
*
|
||||
* `next` is preserved for now, because it's needed in the error case.
|
||||
*/
|
||||
const redirectTo = new URL(url)
|
||||
redirectTo.pathname = next
|
||||
redirectTo.searchParams.delete('token_hash')
|
||||
redirectTo.searchParams.delete('type')
|
||||
|
||||
if (token_hash && type) {
|
||||
const { error } = await supabase.auth.verifyOtp({ type, token_hash })
|
||||
if (!error) {
|
||||
redirectTo.searchParams.delete('next')
|
||||
redirect(303, redirectTo)
|
||||
}
|
||||
}
|
||||
|
||||
redirectTo.pathname = '/auth/error'
|
||||
redirect(303, redirectTo)
|
||||
}
|
||||
1
src/routes/auth/error/+page.svelte
Normal file
1
src/routes/auth/error/+page.svelte
Normal file
@@ -0,0 +1 @@
|
||||
<p>Login error</p>
|
||||
Reference in New Issue
Block a user