/
AuthDataAccess.ts
48 lines (40 loc) · 1.32 KB
/
AuthDataAccess.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
'use server';
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import { createServerActionClient } from '@supabase/auth-helpers-nextjs';
import { Database } from '@/lib/database.types';
import { OAuthProvider } from './OAuthProvider';
const redirectTo = `${process.env.NEXT_PUBLIC_URL}/auth/callback`;
export const signInWithEmail = async (formData: FormData) => {
'use server';
const email = String(formData.get('email'));
const supabase = createServerActionClient<Database>({ cookies });
const { error } = await supabase.auth.signInWithOtp({
email,
options: {
emailRedirectTo: redirectTo,
},
});
if (error) {
console.error(error);
throw new Error('There was an error trying to sign you in.');
} else {
redirect(`/auth/sign-in/check-email-link?email=${email}`);
}
};
export const signInWithOAuth = async (formData: FormData) => {
'use server';
const provider = String(formData.get('provider')) as OAuthProvider;
const supabase = createServerActionClient<Database>({ cookies });
const { data, error } = await supabase.auth.signInWithOAuth({
provider,
options: {
redirectTo,
},
});
if (error || typeof data.url !== 'string') {
throw new Error('There was an error trying to sign you in.');
} else {
redirect(data.url);
}
};