-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.server.ts
66 lines (52 loc) · 1.55 KB
/
user.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import bcrypt from "bcryptjs";
import { createClient } from "@supabase/supabase-js";
import invariant from "tiny-invariant";
export type User = { id: string; email: string };
// Abstract this away
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseAnonKey = process.env.SUPABASE_ANON_KEY;
invariant(
supabaseUrl,
"SUPABASE_URL must be set in your environment variables."
);
invariant(
supabaseAnonKey,
"SUPABASE_ANON_KEY must be set in your environment variables."
);
export const supabase = createClient(supabaseUrl, supabaseAnonKey);
export async function createUser(email: string, password: string) {
const { user } = await supabase.auth.signUp({
email,
password,
});
// get the user profile after created
const profile = await getProfileByEmail(user?.email);
return profile;
}
export async function getProfileById(id: string) {
const { data, error } = await supabase
.from("profiles")
.select("email, id")
.eq("id", id)
.single();
if (error) return null;
if (data) return { id: data.id, email: data.email };
}
export async function getProfileByEmail(email?: string) {
const { data, error } = await supabase
.from("profiles")
.select("email, id")
.eq("email", email)
.single();
if (error) return null;
if (data) return data;
}
export async function verifyLogin(email: string, password: string) {
const { user, error } = await supabase.auth.signIn({
email,
password,
});
if (error) return undefined;
const profile = await getProfileByEmail(user?.email);
return profile;
}