generated from alchemycodelab/web-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-utils.js
48 lines (38 loc) · 1.32 KB
/
fetch-utils.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
const SUPABASE_URL = 'https://nxkfcrgevcuksvdnaugc.supabase.co';
const SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im54a2ZjcmdldmN1a3N2ZG5hdWdjIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NDQzNDE0NjAsImV4cCI6MTk1OTkxNzQ2MH0.9e3rnFUxMChSkwzJQcBImTDSwY_4VDtfmLDUZ-dY7gc';
const client = supabase.createClient(SUPABASE_URL, SUPABASE_KEY);
function checkError({ data, error }) {
return error ? console.error(error) : data;
}
export function getUser() {
return client.auth.session() && client.auth.session().user;
}
export async function fetchNotes() {
const resp = await client.from('notes').select('*');
return checkError(resp);
}
export async function signupUser(email, password) {
const resp = await client.auth.signUp({ email, password });
return resp.user;
}
export async function signInUser(email, password) {
const resp = await client.auth.signIn({ email, password });
return resp.user;
}
export async function logOut() {
client.auth.signOut();
}
export function redirectIfLoggedIn() {
if (getUser()) {
location.replace('/');
}
}
export function checkAuth() {
if (!getUser()) {
location.replace('/');
}
}
export async function insertRow(newPost) {
const resp = await client.from('notes').insert([newPost]);
return checkError(resp);
}