-
Notifications
You must be signed in to change notification settings - Fork 0
/
supabase.ts
98 lines (84 loc) · 2.88 KB
/
supabase.ts
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
import { createClient } from "@supabase/supabase-js";
const config = useRuntimeConfig()
const url: any = config.SUPABASE_URL
//const anon_key: any = config.SUPABASE_ANON_KEY
const service_role_key: any = config.SERVICE_ROLE_KEY
export const supabase = createClient(url, service_role_key, {
auth: {
autoRefreshToken: false,
persistSession: false,
detectSessionInUrl: false
}
})
async function returnUserSession() {
const { data: { user } } = await supabase.auth.getUser()
return { user }
}
export async function checkSession(event?: any) {
const { user } = await returnUserSession()
if (!user) {
// dont know if its scuffed but with this we can run on the client and server
if (process.server) {
return { user: 'No User Was Found Try Again!' }
}
}
return { user }
}
export async function isUserSession(method: any) {
const { user } = await returnUserSession()
if (user) {
method()
console.log('there is an active user session')
return { user }
}
console.log('there was no active users')
}
export async function updateUserInfo(attributesObj: any) {
const { data, error } = await supabase.auth.updateUser(attributesObj)
if (error) throw error
console.log(`Updated User ${data.user.id}`)
const user = data.user
return { user }
}
export async function updatePicture(fileName: string, picture: any, type: any) {
console.log(fileName, picture)
console.log(type)
const { data, error } = await supabase.storage.from('profile').upload(`public/${fileName}`, picture, {
cacheControl: '3600',
upsert: true,
contentType: type
})
await signPictureUrl(fileName)
if (error) throw error
console.log(`Uploaded ${fileName}`)
}
export async function signPictureUrl(fileName: string) {
const oneYear = 60 * 60 * 24 * 365;
const { data, error } = await supabase.storage.from('profile').createSignedUrl(`public/${fileName}`, oneYear)
if (error) throw error
console.log(`Signed Url ${data.signedUrl}`)
return data
}
export async function getSignedUrl(userId: string) {
let picture;
const { data, error } = await supabase.storage.from("profile").list("public");
if (error) {
console.error("Error retrieving files:", error.message);
} else {
// `data` contains an array of file objects
// You can use this array to find the file with the desired user ID and extension
const file = data.find((file) => {
const [name, ext] = file.name.split(".");
return name === userId && ["png", "svg", "jpg", "gif"].includes(ext);
});
if (file) {
// `file` contains the file object for the desired file
// You can use `file.name` to get the name of the file
const { data } = supabase.storage.from("profile").getPublicUrl(`public/${file.name}`);
picture = data.publicUrl;
} else {
console.error("File not found");
}
}
return picture
}