From 8fbef531a75eb4ac3094cb6bab90aed79676f64f Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Tue, 12 Aug 2025 14:56:46 -0600 Subject: [PATCH] Add in sign in functionality --- apps/web-tss/biome.json | 2 +- .../src/integrations/supabase/signIn.ts | 153 ++++++------------ 2 files changed, 48 insertions(+), 107 deletions(-) diff --git a/apps/web-tss/biome.json b/apps/web-tss/biome.json index df38305bf..1796ac26c 100644 --- a/apps/web-tss/biome.json +++ b/apps/web-tss/biome.json @@ -25,7 +25,7 @@ "correctness": { "noUnusedVariables": "off", "noUnusedImports": "off", - "useExhaustiveDependencies": "warn" + "useExhaustiveDependencies": "off" }, "style": { "noNonNullAssertion": "error", diff --git a/apps/web-tss/src/integrations/supabase/signIn.ts b/apps/web-tss/src/integrations/supabase/signIn.ts index 5ea1f1b9c..2c6e05a15 100644 --- a/apps/web-tss/src/integrations/supabase/signIn.ts +++ b/apps/web-tss/src/integrations/supabase/signIn.ts @@ -1,6 +1,7 @@ import { redirect } from '@tanstack/react-router'; import { createServerFn } from '@tanstack/react-start'; import { z } from 'zod'; +import { env } from '@/env'; import { ServerRoute as AuthCallbackRoute } from '../../routes/auth.callback'; import { getSupabaseServerClient } from './server'; @@ -21,12 +22,12 @@ export const signInWithEmailAndPassword = createServerFn({ method: 'POST' }) const supabase = getSupabaseServerClient(); const { error } = await supabase.auth.signInWithPassword({ email: data.email, - password: data.password + password: data.password, }); if (error) { return { error: true, - message: error.message + message: error.message, }; } @@ -49,8 +50,8 @@ export const signInWithGoogle = createServerFn({ method: 'POST' }) const { data, error } = await supabase.auth.signInWithOAuth({ provider: 'google', options: { - redirectTo: callbackUrl.toString() - } + redirectTo: callbackUrl.toString(), + }, }); if (error) { @@ -97,120 +98,60 @@ export const signInWithAnonymousUser = createServerFn({ method: 'POST' }).handle created_at: session.user.created_at, updated_at: session.user.updated_at, role: session.user.role, - deleted_at: session.user.deleted_at + deleted_at: session.user.deleted_at, } - : null - } + : null, + }, }; }); -// export const signInWithGithub = async ({ -// redirectTo, -// }: { -// redirectTo?: string | null; -// } = {}): Promise> => { -// "use server"; +export const signInWithGithub = createServerFn({ method: 'POST' }) + .validator(z.object({ redirectUrl: z.string().optional() })) + .handler(async ({ data: { redirectUrl } }) => { + const supabase = getSupabaseServerClient(); -// const supabase = await createSupabaseServerClient(); + const redirectTo = redirectUrl || '/'; -// const callbackUrl = new URL(authURLFull); -// if (redirectTo && isValidRedirectUrl(redirectTo)) { -// callbackUrl.searchParams.set("next", redirectTo); -// } + const callbackUrl = new URL(AuthCallbackRoute.to); -// const { data, error } = await supabase.auth.signInWithOAuth({ -// provider: "github", -// options: { -// redirectTo: callbackUrl.toString(), -// }, -// }); + if (redirectTo && isValidRedirectUrl(redirectTo)) { + callbackUrl.searchParams.set('next', redirectTo); + } -// if (error) { -// return { success: false, error: error.message }; -// } + const { data, error } = await supabase.auth.signInWithOAuth({ + provider: 'github', + options: { + redirectTo: callbackUrl.toString(), + }, + }); -// revalidatePath("/", "layout"); -// return redirect(data.url); -// }; + if (error) { + return { success: false, error: error.message }; + } -// export const signInWithAzure = async ({ -// redirectTo, -// }: { -// redirectTo?: string | null; -// } = {}): Promise> => { -// "use server"; + throw redirect({ to: data.url }); + }); -// const supabase = await createSupabaseServerClient(); +export const signUpWithEmailAndPassword = createServerFn({ method: 'POST' }) + .validator( + z.object({ email: z.string(), password: z.string(), redirectUrl: z.string().optional() }) + ) + .handler(async ({ data }) => { + const supabase = getSupabaseServerClient(); -// const callbackUrl = new URL(authURLFull); -// if (redirectTo && isValidRedirectUrl(redirectTo)) { -// callbackUrl.searchParams.set("next", redirectTo); -// } + const authURLFull = `${env.VITE_PUBLIC_URL}${AuthCallbackRoute.to}`; -// const { data, error } = await supabase.auth.signInWithOAuth({ -// provider: "azure", -// options: { -// redirectTo: callbackUrl.toString(), -// scopes: "email", -// }, -// }); + const { error } = await supabase.auth.signUp({ + email: data.email, + password: data.password, + options: { + emailRedirectTo: data.redirectUrl, + }, + }); -// if (error) { -// return { success: false, error: error.message }; -// } -// revalidatePath("/", "layout"); -// return redirect(data.url); -// }; + if (error) { + return { success: false, error: error.message }; + } -// export const signUp = async ({ -// email, -// password, -// redirectTo, -// }: { -// email: string; -// password: string; -// redirectTo?: string | null; -// }): Promise => { -// "use server"; -// const supabase = await createSupabaseServerClient(); -// const authURL = createBusterRoute({ -// route: BusterRoutes.AUTH_CONFIRM, -// }); -// const authURLFull = `${process.env.NEXT_PUBLIC_URL}${authURL}`; - -// const { error } = await supabase.auth.signUp({ -// email, -// password, -// options: { -// emailRedirectTo: authURLFull, -// }, -// }); -// if (error) { -// console.error("supabase error in signUp", error); -// // Return the actual Supabase error message -// return { success: false, error: error.message }; -// } - -// revalidatePath("/", "layout"); -// const finalRedirect = -// redirectTo && isValidRedirectUrl(redirectTo) -// ? decodeURIComponent(redirectTo) -// : createBusterRoute({ route: BusterRoutes.APP_HOME }); -// return redirect(finalRedirect); -// }; - -// export const signInWithAnonymousUser = async () => { -// "use server"; - -// const supabase = await createSupabaseServerClient(); - -// const { data, error } = await supabase.auth.signInAnonymously(); - -// if (error) { -// throw error; -// } - -// revalidatePath("/", "layout"); - -// return data; -// }; + throw redirect({ to: authURLFull }); + });