Cleanup. Stuff from yesterday idk

This commit is contained in:
2025-06-06 08:43:18 -05:00
parent a776c5a30a
commit 35e019558f
29 changed files with 866 additions and 694 deletions

View File

@ -1,11 +1,11 @@
'use client'
import { encodedRedirect } from '@/utils/utils';
'use client';
import { createClient } from '@/utils/supabase';
import type { User } from '@/utils/supabase';
import type { Result } from './index';
import type { Result } from '.';
export const signUp = async (formData: FormData): Promise<Result<string | null>> => {
export const signUp = async (
formData: FormData,
): Promise<Result<string | null>> => {
const name = formData.get('name') as string;
const email = formData.get('email') as string;
const password = formData.get('password') as string;
@ -13,11 +13,7 @@ export const signUp = async (formData: FormData): Promise<Result<string | null>>
const origin = process.env.NEXT_PUBLIC_SITE_URL!;
if (!email || !password) {
return encodedRedirect(
'error',
'/sign-up',
'Email & password are required',
);
return { success: false, error: 'Email and password are required' };
}
const { error } = await supabase.auth.signUp({
@ -42,9 +38,7 @@ export const signUp = async (formData: FormData): Promise<Result<string | null>>
}
};
export const signIn = async (
formData: FormData,
): Promise<Result<null>> => {
export const signIn = async (formData: FormData): Promise<Result<null>> => {
const email = formData.get('email') as string;
const password = formData.get('password') as string;
const supabase = createClient();
@ -60,17 +54,16 @@ export const signIn = async (
}
};
export const signInWithMicrosoft = async (): Promise<Result<string>> => {
const supabase = createClient();
const { data, error } = await supabase.auth.signInWithOAuth({
provider: 'azure',
options: {
scopes: 'openid, profile email offline_access',
}
},
});
if (error) return { success: false, error: error.message };
return { success: true, data: data.url};
return { success: true, data: data.url };
};
export const signInWithApple = async (): Promise<Result<string>> => {
@ -79,13 +72,15 @@ export const signInWithApple = async (): Promise<Result<string>> => {
provider: 'apple',
options: {
scopes: 'openid, profile email offline_access',
}
},
});
if (error) return { success: false, error: error.message };
return { success: true, data: data.url};
return { success: true, data: data.url };
};
export const forgotPassword = async (formData: FormData): Promise<Result<string | null>> => {
export const forgotPassword = async (
formData: FormData,
): Promise<Result<string | null>> => {
const email = formData.get('email') as string;
const supabase = createClient();
const origin = process.env.NEXT_PUBLIC_SITE_URL!;
@ -101,15 +96,22 @@ export const forgotPassword = async (formData: FormData): Promise<Result<string
if (error) {
return { success: false, error: 'Could not reset password' };
}
return { success: true, data: 'Check your email for a link to reset your password.' };
return {
success: true,
data: 'Check your email for a link to reset your password.',
};
};
export const resetPassword = async (formData: FormData): Promise<Result<null>> => {
export const resetPassword = async (
formData: FormData,
): Promise<Result<null>> => {
const password = formData.get('password') as string;
const confirmPassword = formData.get('confirmPassword') as string;
if (!password || !confirmPassword) {
return { success: false, error: 'Password and confirm password are required!' };
return {
success: false,
error: 'Password and confirm password are required!',
};
}
const supabase = createClient();
if (password !== confirmPassword) {
@ -119,7 +121,10 @@ export const resetPassword = async (formData: FormData): Promise<Result<null>> =
password,
});
if (error) {
return { success: false, error: `Password update failed: ${error.message}` };
return {
success: false,
error: `Password update failed: ${error.message}`,
};
}
return { success: true, data: null };
};
@ -127,7 +132,7 @@ export const resetPassword = async (formData: FormData): Promise<Result<null>> =
export const signOut = async (): Promise<Result<null>> => {
const supabase = createClient();
const { error } = await supabase.auth.signOut();
if (error) return { success: false, error: error.message }
if (error) return { success: false, error: error.message };
return { success: true, data: null };
};

View File

@ -2,7 +2,7 @@
import { createClient, type Profile } from '@/utils/supabase';
import { getUser } from '@/lib/hooks';
import type { Result } from './index';
import type { Result } from '.';
export const getProfile = async (): Promise<Result<Profile>> => {
try {

View File

@ -1,7 +1,7 @@
'use client';
import { createClient } from '@/utils/supabase';
import type { Result } from './index';
import type { Result } from '.';
export type GetStorageProps = {
bucket: string;
@ -38,12 +38,12 @@ export type ReplaceStorageProps = {
};
export type resizeImageProps = {
file: File,
file: File;
options?: {
maxWidth?: number,
maxHeight?: number,
quality?: number,
}
maxWidth?: number;
maxHeight?: number;
quality?: number;
};
};
export const getSignedUrl = async ({
@ -75,7 +75,7 @@ export const getSignedUrl = async ({
: 'Unknown error getting signed URL',
};
}
}
};
export const getPublicUrl = async ({
bucket,
@ -85,12 +85,10 @@ export const getPublicUrl = async ({
}: GetStorageProps): Promise<Result<string>> => {
try {
const supabase = createClient();
const { data } = supabase.storage
.from(bucket)
.getPublicUrl(url, {
download,
transform,
});
const { data } = supabase.storage.from(bucket).getPublicUrl(url, {
download,
transform,
});
if (!data?.publicUrl) throw new Error('No public URL returned');
@ -104,7 +102,7 @@ export const getPublicUrl = async ({
: 'Unknown error getting public URL',
};
}
}
};
export const uploadFile = async ({
bucket,
@ -129,7 +127,7 @@ export const uploadFile = async ({
error instanceof Error ? error.message : 'Unknown error uploading file',
};
}
}
};
export const replaceFile = async ({
bucket,
@ -179,7 +177,7 @@ export const deleteFile = async ({
error instanceof Error ? error.message : 'Unknown error deleting file',
};
}
}
};
// Add a helper to list files in a bucket
export const listFiles = async ({
@ -213,53 +211,49 @@ export const listFiles = async ({
error instanceof Error ? error.message : 'Unknown error listing files',
};
}
}
};
export const resizeImage = async ({
file,
options = {},
}: resizeImageProps): Promise<File> => {
const {
maxWidth = 800,
maxHeight = 800,
quality = 0.8,
} = options;
return new Promise((resolve) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (event) => {
const img = new Image();
img.src = event.target?.result as string;
img.onload = () => {
let width = img.width;
let height = img.height;
if (width > height) {
if (width > maxWidth) {
height = Math.round((height * maxWidth / width));
width = maxWidth;
}
} else if (height > maxHeight) {
width = Math.round((width * maxHeight / height));
height = maxHeight;
const { maxWidth = 800, maxHeight = 800, quality = 0.8 } = options;
return new Promise((resolve) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (event) => {
const img = new Image();
img.src = event.target?.result as string;
img.onload = () => {
let width = img.width;
let height = img.height;
if (width > height) {
if (width > maxWidth) {
height = Math.round((height * maxWidth) / width);
width = maxWidth;
}
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx?.drawImage(img, 0, 0, width, height);
canvas.toBlob(
(blob) => {
if (!blob) return;
const resizedFile = new File([blob], file.name, {
type: 'imgage/jpeg',
lastModified: Date.now(),
});
resolve(resizedFile);
},
'image/jpeg',
quality
);
};
} else if (height > maxHeight) {
width = Math.round((width * maxHeight) / height);
height = maxHeight;
}
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx?.drawImage(img, 0, 0, width, height);
canvas.toBlob(
(blob) => {
if (!blob) return;
const resizedFile = new File([blob], file.name, {
type: 'imgage/jpeg',
lastModified: Date.now(),
});
resolve(resizedFile);
},
'image/jpeg',
quality,
);
};
});
};
});
};

View File

@ -1,14 +1,13 @@
'use client'
'use client';
import { useState, useRef } from 'react';
import { replaceFile, uploadFile } from '@/lib/hooks';
import { toast } from 'sonner';
import { useAuth } from '@/components/context/auth';
import { resizeImage } from '@/lib/hooks';
import type { Result } from '.';
export type Replace =
| { replace: true, path: string }
| false;
export type Replace = { replace: true; path: string } | false;
export type uploadToStorageProps = {
file: File;
@ -33,7 +32,7 @@ export const useFileUpload = () => {
resize = false,
options = {},
replace = false,
}: uploadToStorageProps) => {
}: uploadToStorageProps): Promise<Result<string>> => {
try {
if (!isAuthenticated) throw new Error('User is not authenticated');
@ -48,10 +47,9 @@ export const useFileUpload = () => {
},
});
if (!updateResult.success) {
console.error('Error updating file:', updateResult.error);
return { success: false, error: updateResult.error };
} else {
console.log('We used the new update function hopefully it worked!');
return { success: true, path: updateResult.data };
return { success: true, data: updateResult.data };
}
}
@ -77,15 +75,21 @@ export const useFileUpload = () => {
throw new Error(uploadResult.error || `Failed to upload to ${bucket}`);
}
return { success: true, path: uploadResult.data };
return { success: true, data: uploadResult.data };
} catch (error) {
console.error(`Error uploading to ${bucket}:`, error);
toast.error(
error instanceof Error
? error.message
: `Failed to upload to ${bucket}`,
);
return { success: false, error };
return {
success: false,
error: `Error: ${
error instanceof Error
? error.message
: `Failed to upload to ${bucket}`
}`,
};
} finally {
setIsUploading(false);
// Clear the input value so the same file can be selected again