-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbackground.ts
More file actions
45 lines (39 loc) · 1.3 KB
/
background.ts
File metadata and controls
45 lines (39 loc) · 1.3 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
import browser from "webextension-polyfill";
import supabase from './src/supabase_client';
type Message = {
action: 'fetch' | 'getSession' | 'signout',
value: null
} | {
action: 'signup' | 'signin',
value: {
email: string,
password: string,
}
}
type ResponseCallback = (data: any) => void
async function handleMessage({action, value}: Message, response: ResponseCallback) {
if (action === 'fetch') {
const result = await fetch('https://meowfacts.herokuapp.com/');
const { data } = await result.json();
response({ message: 'Successfully signed up!', data });
} else if (action === 'signup') {
const result = await supabase.auth.signUp(value)
response({message: 'Successfully signed up!', data: result});
} else if (action === 'signin') {
console.log('requesting auth');
const {data, error} = await supabase.auth.signInWithPassword(value);
response({data, error});
} else if (action === 'getSession') {
supabase.auth.getSession().then(response)
} else if (action === 'signout') {
const {error} = await supabase.auth.signOut()
response({data: null, error});
} else {
response({data: null, error: 'Unknown action'});
}
}
// @ts-ignore
browser.runtime.onMessage.addListener((msg, sender, response) => {
handleMessage(msg, response);
return true;
})