forked from rphlmr/supa-fly-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.server.ts
76 lines (56 loc) · 1.92 KB
/
service.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
67
68
69
70
71
72
73
74
75
76
import { getSupabaseAdmin } from "~/integrations/supabase";
import { SERVER_URL } from "~/utils/env";
import { mapAuthSession } from "./mappers";
import type { AuthSession } from "./types";
export async function createEmailAuthAccount(email: string, password: string) {
const { data, error } = await getSupabaseAdmin().auth.admin.createUser({
email,
password,
email_confirm: true, // FIXME: demo purpose, assert that email is confirmed. For production, check email confirmation
});
if (!data.user || error) return null;
return data.user;
}
export async function signInWithEmail(email: string, password: string) {
const { data, error } = await getSupabaseAdmin().auth.signInWithPassword({
email,
password,
});
if (!data.session || error) return null;
return mapAuthSession(data.session);
}
export async function sendMagicLink(email: string) {
return getSupabaseAdmin().auth.signInWithOtp({
email,
options: {
emailRedirectTo: `${SERVER_URL}/oauth/callback`,
},
});
}
export async function deleteAuthAccount(userId: string) {
const { error } = await getSupabaseAdmin().auth.admin.deleteUser(userId);
if (error) return null;
return true;
}
export async function getAuthAccountByAccessToken(accessToken: string) {
const { data, error } = await getSupabaseAdmin().auth.getUser(accessToken);
if (!data.user || error) return null;
return data.user;
}
export async function refreshAccessToken(
refreshToken?: string
): Promise<AuthSession | null> {
if (!refreshToken) return null;
const { data, error } = await getSupabaseAdmin().auth.setSession({
access_token: "",
refresh_token: refreshToken,
});
if (!data.session || error) return null;
return mapAuthSession(data.session);
}
export async function verifyAuthSession(authSession: AuthSession) {
const authAccount = await getAuthAccountByAccessToken(
authSession.accessToken
);
return Boolean(authAccount);
}