forked from rustyzone/Supabase-Chrome-Ext-Auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bg-supa.js
72 lines (65 loc) · 1.98 KB
/
bg-supa.js
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
const configUrl = chrome.runtime.getURL('supa_config.json');
const config = fetch(configUrl)
.then((response) => response.json());
config.then((result) => {
const { createClient } = supabase;
supabase = createClient(result.SUPABASE_URL, result.SUPABASE_KEY);
console.log('supabase?', supabase);
});
const handleMessage = async function(msg, sender, response){
console.log('handle...');
if(msg.command == 'logoutAuth'){
let { error } = await supabase.auth.signOut();
//check for error here.. then choose response..
response({type: "un-auth", status: "success", message: true});
}
if(msg.command == 'checkAuth'){
var user = await supabase.auth.user();
if (user) {
// User is signed in.
response({type: "auth", status: "success", message: user});
} else {
// No user is signed in.
response({type: "auth", status: "no-auth", message: false});
}
}
if(msg.command == 'loginUser'){
console.log(msg.data);
var email = msg.data.e;
var pass = msg.data.p;
let { user, error } = await supabase.auth.signIn({
email: email,
password: pass
});
console.log(error, user);
if (user) {
// User is signed in.
response({type: "auth", status: "success", message: user});
} else {
// No user is signed in.
response({type: "auth", status: "no-auth", message: false});
}
}
if(msg.command == 'signupUser'){
console.log(msg.data);
var email = msg.data.e;
var pass = msg.data.p;
let { user, error } = await supabase.auth.signUp({
email: email,
password: pass
});
console.log(error, user);
if (user) {
// User is signed in.
response({type: "auth", status: "success", message: user});
} else {
// No user is signed in.
response({type: "auth", status: "no-auth", message: false});
}
}
return true;
}
chrome.runtime.onMessage.addListener((msg, sender, response) => {
handleMessage(msg, sender, response);
return true;
});