forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
76 lines (64 loc) · 1.71 KB
/
Copy pathauth.js
File metadata and controls
76 lines (64 loc) · 1.71 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { supabase } from "./supabase";
/**
* Map Supabase user → app user shape used by Navbar / AdminGuard.
*/
export function mapAuthUser(supabaseUser) {
if (!supabaseUser) return null;
const meta = supabaseUser.user_metadata || {};
return {
id: supabaseUser.id,
uid: supabaseUser.id,
email: supabaseUser.email || "",
displayName:
meta.full_name ||
meta.name ||
meta.display_name ||
(supabaseUser.email ? supabaseUser.email.split("@")[0] : "User"),
photoURL: meta.avatar_url || meta.picture || null,
};
}
export const loginWithGoogle = async () => {
const { data, error } = await supabase.auth.signInWithOAuth({
provider: "google",
options: {
redirectTo:
typeof window !== "undefined"
? `${window.location.origin}/profile/login`
: undefined,
queryParams: {
access_type: "offline",
prompt: "consent",
},
},
});
if (error) {
throw new Error(`Error logging in with Google: ${error.message}`);
}
return data;
};
export const signup = async (email, password) => {
const { data, error } = await supabase.auth.signUp({
email,
password,
});
if (error) {
throw new Error(`Error signing up: ${error.message}`);
}
return mapAuthUser(data.user);
};
export const login = async (email, password) => {
const { data, error } = await supabase.auth.signInWithPassword({
email,
password,
});
if (error) {
throw new Error(`Error logging in: ${error.message}`);
}
return mapAuthUser(data.user);
};
export const logout = async () => {
const { error } = await supabase.auth.signOut();
if (error) {
throw new Error(`Error logging out: ${error.message}`);
}
};