Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const supabase = createClient();
- const user = await getProfile();
- if (!user) {
- throw new Error(
- 'You must be authenticated to upload a profile picture'
- );
- }
- const fileName = `${user.id}.jpg`;
- const filePath = fileName;
- if (!user.avatar_url) {
- await supabase.storage.from('profile-pictures').upload(filePath, file);
- } else {
- // Replace (update) the file at the same path
- const { data, error: updateError } = await supabase.storage
- .from('profile-pictures')
- .update(filePath, file, {
- upsert: true
- });
- console.log('data', data);
- if (updateError) {
- console.error('Update error:', updateError);
- throw updateError;
- }
- }
- // Get the public URL
- const {
- data: { publicUrl }
- } = supabase.storage.from('profile-pictures').getPublicUrl(filePath);
- // Update user profile with new avatar URL
- await updateProfile({
- avatarUrl: publicUrl
- });
- return { publicUrl };
Advertisement
Add Comment
Please, Sign In to add comment