forked from bible-technology/scribe-scripture-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
supabase.js
executable file
·124 lines (111 loc) · 3.04 KB
/
supabase.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
import { createClient } from '@supabase/supabase-js';
import 'dotenv/config';
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
console.log({ supabaseUrl, supabaseAnonKey });
const IsElectron = process.env.NEXT_PUBLIC_IS_ELECTRON === 'true';
const supabase = !IsElectron ? createClient(supabaseUrl, supabaseAnonKey) : {};
// const supabase = createClient(supabaseUrl, supabaseAnonKey);
const supabaseStorage = !IsElectron ? createClient(supabaseUrl, supabaseAnonKey).storage.from('scribe') : {};
const sbStorageUpload = (file, payload, settings = {}) => {
const data = supabaseStorage.upload(file, payload, settings);
return data;
};
async function createDirectory({ path, data }) {
const { data: folder } = await supabaseStorage.list(path);
if (folder.length === 0) {
if (data) {
await sbStorageUpload(path, data);
} else {
const fileName = '.keep';
const filePath = `${path}/${fileName}`;
const fileContent = new Blob(['testtext'], { type: 'text/plain' });
// eslint-disable-next-line no-unused-vars
const { data: createdDirectory } = await sbStorageUpload(
filePath,
fileContent,
{
cacheControl: '3600',
upsert: false,
},
);
}
}
}
const getSupabaseSession = async () => {
if (supabase.auth) {
const { data } = await supabase.auth.getSession();
return data;
}
};
const getSupabaseUser = async () => {
if (supabase.auth) {
const {
data: { user },
} = await supabase.auth.getUser();
return user;
}
};
const supabaseSignup = async ({ email, password }) => {
if (supabase.auth) {
const response = await supabase.auth.signUp({
email,
password,
});
return response;
}
};
const supabaseSignIn = async ({ email, password }) => {
if (supabase.auth) {
const response = await supabase.auth.signInWithPassword({
email,
password,
});
return response;
}
};
const supabaseSignout = async () => {
if (supabase.auth) {
const response = await supabase.auth.signOut();
return response;
}
};
const sbStorageList = async (file) => {
if (file === undefined) {
const response = await supabaseStorage.list();
return response;
}
const response = await supabaseStorage.list(file);
return response;
};
const sbStorageDownload = async (path) => {
const res = await supabaseStorage.download(path);
return res;
};
const sbStorageUpdate = async ({ path, payload, options = {} }) => {
await supabaseStorage().update(path, payload, options);
};
const sbStorageRemove = async (path, options) => {
if (options) {
await supabaseStorage().remove(path, options);
}
await supabaseStorage().remove(path);
};
const newPath = 'scribe/users';
export {
supabase,
supabaseSignup,
supabaseSignIn,
supabaseSignout,
supabaseStorage,
newPath,
createDirectory,
getSupabaseSession,
getSupabaseUser,
sbStorageList,
sbStorageDownload,
sbStorageUpload,
sbStorageUpdate,
sbStorageRemove,
IsElectron,
};