Not even sure but I'm sure it's better

This commit is contained in:
2025-05-20 16:38:40 -05:00
parent 259aa46ef8
commit 0f92f7eb7f
19 changed files with 331 additions and 575 deletions

View File

@ -3,7 +3,7 @@
import 'server-only';
import { encodedRedirect } from '@/utils/utils';
import { createServerClient } from '@/utils/supabase';
import type { User } from '@supabase/supabase-js'
import type { User } from '@supabase/supabase-js';
import { headers } from 'next/headers';
import { redirect } from 'next/navigation';
import type { Result } from './index';
@ -119,11 +119,7 @@ export const resetPassword = async (formData: FormData) => {
}
if (password !== confirmPassword) {
encodedRedirect(
'error',
'/reset-password',
'Passwords do not match',
);
encodedRedirect('error', '/reset-password', 'Passwords do not match');
}
const { error } = await supabase.auth.updateUser({
@ -131,11 +127,7 @@ export const resetPassword = async (formData: FormData) => {
});
if (error) {
encodedRedirect(
'error',
'/reset-password',
'Password update failed',
);
encodedRedirect('error', '/reset-password', 'Password update failed');
}
encodedRedirect('success', '/reset-password', 'Password updated');
@ -152,8 +144,8 @@ export const getUser = async (): Promise<Result<User>> => {
const supabase = await createServerClient();
const { data, error } = await supabase.auth.getUser();
if (error) throw error;
return {success: true, data: data.user};
return { success: true, data: data.user };
} catch (error) {
return {success: false, error: 'Could not get user!'};
return { success: false, error: 'Could not get user!' };
}
};

View File

@ -8,7 +8,8 @@ import type { Result } from './index';
export const getProfile = async (): Promise<Result<Profile>> => {
try {
const user = await getUser();
if (!user.success || user.data === undefined) throw new Error('User not found');
if (!user.success || user.data === undefined)
throw new Error('User not found');
const supabase = await createServerClient();
const { data, error } = await supabase
.from('profiles')
@ -40,7 +41,11 @@ export const updateProfile = async ({
avatar_url,
}: updateProfileProps): Promise<Result<Profile>> => {
try {
if (full_name === undefined && email === undefined && avatar_url === undefined)
if (
full_name === undefined &&
email === undefined &&
avatar_url === undefined
)
throw new Error('No profile data provided');
const userResponse = await getUser();
@ -51,9 +56,9 @@ export const updateProfile = async ({
const { data, error } = await supabase
.from('profiles')
.update({
...(full_name !== undefined && {full_name}),
...(email !== undefined && {email}),
...(avatar_url !== undefined && {avatar_url}),
...(full_name !== undefined && { full_name }),
...(email !== undefined && { email }),
...(avatar_url !== undefined && { avatar_url }),
})
.eq('id', userResponse.data.id)
.select()

View File

@ -17,9 +17,9 @@ export const useAvatar = (profile?: Profile) => {
url: profile.avatar_url,
transform: {
quality: 20,
}
},
});
if (response.success) {
setAvatarUrl(response.data);
}
@ -32,15 +32,18 @@ export const useAvatar = (profile?: Profile) => {
setAvatarUrl(undefined);
}
};
getAvatarUrl().catch((error) => {
toast.error(error instanceof Error ? error.message : 'Failed to get signed avatar url.');
});
getAvatarUrl().catch((error) => {
toast.error(
error instanceof Error
? error.message
: 'Failed to get signed avatar url.',
);
});
}, [profile]);
return {
avatarUrl,
isLoading,
};
}
};

View File

@ -9,11 +9,11 @@ export const useFileUpload = () => {
const uploadToStorage = async (file: File, bucket: string) => {
try {
setIsUploading(true);
// Generate a unique filename to avoid collisions
const fileExt = file.name.split('.').pop();
const fileName = `${Date.now()}-${Math.random().toString(36).substring(2, 15)}.${fileExt}`;
// Upload the file to Supabase storage
const uploadResult = await uploadFile({
bucket,
@ -24,15 +24,19 @@ export const useFileUpload = () => {
contentType: file.type,
},
});
if (!uploadResult.success) {
throw new Error(uploadResult.error || `Failed to upload to ${bucket}`);
}
return { success: true, path: uploadResult.data };
} catch (error) {
console.error(`Error uploading to ${bucket}:`, error);
toast.error(error instanceof Error ? error.message : `Failed to upload to ${bucket}`);
toast.error(
error instanceof Error
? error.message
: `Failed to upload to ${bucket}`,
);
return { success: false, error };
} finally {
setIsUploading(false);
@ -46,4 +50,4 @@ export const useFileUpload = () => {
fileInputRef,
uploadToStorage,
};
}
};

View File

@ -22,7 +22,9 @@ export const useProfile = () => {
}
};
fetchProfile().catch((error) => {
toast.error(error instanceof Error ? error.message : 'Failed to get profile');
toast.error(
error instanceof Error ? error.message : 'Failed to get profile',
);
});
}, []);
@ -42,7 +44,9 @@ export const useProfile = () => {
return { success: true, data: result.data };
} catch (error) {
console.error('Error updating profile: ', error);
toast.error(error instanceof Error ? error.message : 'Failed to update profile');
toast.error(
error instanceof Error ? error.message : 'Failed to update profile',
);
return { success: false, error };
} finally {
setIsLoading(false);
@ -54,4 +58,4 @@ export const useProfile = () => {
isLoading,
updateProfile: updateUserProfile,
};
}
};