This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-utils.js
87 lines (65 loc) · 2.01 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
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
77
78
79
80
81
82
83
84
85
86
87
const SUPABASE_URL = 'https://gxwgjhfyrlwiqakdeamc.supabase.co';
const SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlhdCI6MTYzNjQxMTMxMiwiZXhwIjoxOTUxOTg3MzEyfQ.PHekiwfLxT73qQsLklp0QFEfNx9NlmkssJFDnlvNIcA';
const client = supabase.createClient(SUPABASE_URL, SUPABASE_KEY);
export async function createTodo(todo){
const response = await client
.from('todos')
.insert({
todo: todo,
complete: false,
user_id: client.auth.user().id,
})
.single();
return checkError(response);
}
export async function deleteAllTodos() {
await client
.from('todos')
.delete()
.match({ user_id: client.auth.user().id, });
}
export async function getTodos() {
const response = await client
.from('todos')
.select()
.order('complete')
.match({ user_id: client.auth.user().id, });
return checkError(response);
}
export async function completeTodo(id) {
const response = await client
.from('todos')
.update({ complete: true })
.match({
user_id: client.auth.user().id,
id: id,
});
return checkError(response);
}
export async function getUser() {
return client.auth.session();
}
export async function checkAuth() {
const user = await getUser();
if (!user) location.replace('../');
}
export async function redirectIfLoggedIn() {
if (await getUser()) {
location.replace('./todos');
}
}
export async function signupUser(email, password){
const response = await client.auth.signUp({ email, password });
return checkError(response);
}
export async function signInUser(email, password){
const response = await client.auth.signIn({ email, password });
return checkError(response);
}
export async function logout() {
await client.auth.signOut();
return window.location.href = '/';
}
function checkError({ data, error }) {
return error ? console.error(error) : data;
}