-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch-utils.js
134 lines (104 loc) · 3.72 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
const SUPABASE_URL = 'https://lhopwipdeoyzrdkhgnll.supabase.co';
const SUPABASE_KEY =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imxob3B3aXBkZW95enJka2hnbmxsIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NjE0NDUxMDEsImV4cCI6MTk3NzAyMTEwMX0.CqslQ0D0NOtKTdAzI7cJ1mdy_MBewH_F_jVzrMWaNvg';
const client = supabase.createClient(SUPABASE_URL, SUPABASE_KEY);
/* Auth related functions */
export function getUser() {
return client.auth.user();
}
export function checkAuth() {
const user = getUser();
// do we have a user?
if (!user) {
// path is different if we are at home page versus any other page
const authUrl = location.pathname === '/' ? './auth/' : '../auth/';
// include the current url as a "redirectUrl" search param so user can come
// back to this page after they sign in...
location.replace(`${authUrl}?redirectUrl=${encodeURIComponent(location)}`);
}
// return the user so can be used in the page if needed
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();
}
/* Data functions */
export async function newProject(project) {
return await client.from('projects').insert(project).single();
}
export async function updateTrack(track) {
return await client.from('tracks').insert(track);
}
export async function uploadAudio(bucketName, audioName, audioFile) {
const bucket = client.storage.from(bucketName);
const response = await bucket.upload(audioName, audioFile, {
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 getTrack(folderName) {
return await client.storage.from('files-bucket').download(folderName);
}
export async function getTracksByProject(project_id) {
const response = await client.from('tracks').select('*').match({ project_id });
return response.data;
}
export function updateTrackInRealtime(handleInsert, playlist, project_id) {
client
.from(`tracks:project_id=eq.${project_id}`)
.on('INSERT', (e) => {
playlist.load([{ src: e.new.url, name: e.new.instrument }]);
})
.subscribe();
}
export async function getProject(id) {
// from the roster table, select a single player who has the matching id
const response = await client.from('projects').select('*, tracks(*)').match({ id }).single();
// and return the response
if (response.error) {
throw new Error(response.error.message);
}
return response.data;
}
export async function getProjects() {
const response = await client.from('projects').select('*');
return response.data;
}
// PROFILE FETCH FNS
// export async function createProfile(profile) {
// return await client.from('profiles').insert(profile).single();
// }
export async function updateProfile(profile) {
return await client.from('profiles').insert(profile).single();
}
export async function uploadProfilePhoto(bucketName, fileName, imageFile) {
const bucket = client.storage.from(bucketName);
const response = await bucket.upload(fileName, 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;
}