-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabaseClient.ts
More file actions
58 lines (51 loc) · 1.54 KB
/
supabaseClient.ts
File metadata and controls
58 lines (51 loc) · 1.54 KB
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
52
53
54
55
56
57
58
import { createClient, SupabaseClient } from "@supabase/supabase-js";
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL as string;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY as string;
export const supabase: SupabaseClient = createClient(
supabaseUrl,
supabaseAnonKey,
);
export const supabaseAuth = {
login: async ({ email, password }: { email: string; password: string }) => {
try {
const { error, data } = await supabase.auth.signInWithPassword({
email,
password,
});
return error ? [error, null] : [null, data];
} catch (error) {
return [error as Error, null];
}
},
signUp: async ({ email, password }: { email: string; password: string }) => {
try {
const { error, data } = await supabase.auth.signUp({
email,
password,
});
return error ? [error, null] : [null, data];
} catch (error) {
return [error as Error, null];
}
},
getSession: async () => {
try {
const { data, error } = await supabase.auth.getSession();
return error ? [error, null] : [null, data];
} catch (error) {
return [error as Error, null];
}
},
signOut: async () => {
try {
const { error } = await supabase.auth.signOut();
return error ? [error, null] : [null, {}];
} catch (error) {
return [error as Error, null];
}
},
onAuthStateChange: (callback: (event: string, session: any) => void) => {
const { data } = supabase.auth.onAuthStateChange(callback);
return data;
},
};