-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy path+page.server.ts
51 lines (45 loc) · 1.12 KB
/
+page.server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { supabaseClient } from '$lib/db';
import { invalid, redirect } from '@sveltejs/kit';
import type { Actions } from './$types';
import { saveSession } from '@supabase/auth-helpers-sveltekit/server';
export const actions: Actions = {
async default({ request, cookies, url }) {
const formData = await request.formData();
const email = formData.get('email') as string;
const password = formData.get('password') as string;
if (!email) {
return invalid(400, {
error: 'Please enter your email'
});
}
if (!password) {
return invalid(400, {
error: 'Please enter your password',
values: {
email
}
});
}
const { data, error } = await supabaseClient.auth.api.signInWithEmail(email, password, {
redirectTo: `${url.origin}/logging-in`
});
if (error || !data) {
if (error?.status === 400) {
return invalid(400, {
error: 'Invalid credentials',
values: {
email
}
});
}
return invalid(500, {
error: 'Server error. Try again later.',
values: {
email
}
});
}
saveSession(cookies, data);
throw redirect(303, '/dashboard');
}
};