-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch-utils.js
170 lines (135 loc) · 3.79 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const SUPABASE_URL = 'https://mtegvpmustvqjcrpqjft.supabase.co';
const SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im10ZWd2cG11c3R2cWpjcnBxamZ0Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2NTk2MzgyMTYsImV4cCI6MTk3NTIxNDIxNn0.1qATbqaxyJY3HmYMZsX0LcLV6_XXcgd_qnE96O4JeR8';
export const client = supabase.createClient(SUPABASE_URL, SUPABASE_KEY);
export function getUser() {
return client.auth.user();
}
export function checkAuth() {
const user = getUser();
if (!user) {
const authUrl = location.pathname === '/' ? './auth/' : '../auth/';
location.replace(`${authUrl}?redirectUrl=${encodeURIComponent(location)}`);
}
return user;
}
export async function signUpUser(email, password) {
return await client.auth.signUp({
email,
password,
});
}
export async function signInUser(email, password) {
return await client.auth.signIn({
email,
password,
});
}
export async function signOutUser() {
return await client.auth.signOut();
}
export async function getAllUsers() {
const response = await client
.from('pawfile')
.select('*');
if (response.error) {
throw new Error(response.error.message);
}
return response.data;
}
export async function getUserById(user_id) {
const { data, error } = await client
.from('pawfile')
.select('*')
.match({ user_id })
.single();
if (error) {
throw error;
}
return data;
}
export async function savePawfile(user_id) {
return await client
.from('pawfile')
.upsert(user_id);
}
export async function addMessage(message) {
return await client
.from('pawfile_chat')
.insert(message)
.single();
}
export async function getAllMessages() {
const response = await client
.from('pawfile_chat')
.select('*')
.order('created_at');
return response.data;
}
export async function getMessageById(user_id) {
const response = await client
.from('pawfile_chat')
.select('*')
.match({ user_id })
.single();
if (response.error) {
throw new Error(response.error.message);
}
return response.data;
}
export async function uploadImage(imageFile, imageName) {
const response = await client.storage
.from('profile-images')
.upload(imageName, imageFile, {
cacheControl: '3600',
upsert: true,
});
if (response.error) {
console.log(response.error);
return null;
}
const url = `${SUPABASE_URL}/storage/v1/object/public/${response.data.Key}`;
return url;
}
export async function getSingleUser(user_id) {
const response = await client
.from('pawfile')
.select()
.match({ user_id })
.single();
return response.data;
}
export async function onMessage(handleNewMessage) {
await client
.from('pawfile_chat')
.on('INSERT', handleNewMessage)
.subscribe();
}
export async function deleteMessage(id) {
const response = await client
.from('pawfile_chat')
.delete()
.match({ id });
return response.data;
}
export async function getAllDetailComments(target_id) {
const response = await client
.from('comments')
.select('*')
.match({ target_id });
return response.data;
}
export async function createDetailComment(comment, target_id) {
const response = await client
.from('comments')
.insert({
message: comment,
target_id: target_id
});
return response.data;
}
export async function onComment(handleNewComment) {
await client
.from('comments')
.on('INSERT', handleNewComment)
.subscribe();
}