From 7b62c1210f08dd662a61b9577f641c4458617492 Mon Sep 17 00:00:00 2001 From: Luke Weaver Date: Tue, 26 May 2026 15:59:05 -0400 Subject: [PATCH 1/4] Switch to PKCE flow, which fixes redirect issues --- src/hooks/useAuth.ts | 90 +++++++++++++++----------------- src/public-problems | 2 +- src/routes/AuthCallback.tsx | 47 +++++------------ src/routes/ChangePassword.tsx | 25 ++------- src/routes/ReqPasswordChange.tsx | 2 +- src/routes/root.tsx | 7 +++ src/supabaseClient.ts | 2 +- 7 files changed, 69 insertions(+), 106 deletions(-) diff --git a/src/hooks/useAuth.ts b/src/hooks/useAuth.ts index 3c1983c0..c630bae1 100644 --- a/src/hooks/useAuth.ts +++ b/src/hooks/useAuth.ts @@ -21,77 +21,71 @@ export default function useAuth(): UseAuthReturn { const [isAdmin, setIsAdmin] = useState(false); const [isRecoverySession, setIsRecoverySession] = useState(false); - const fetchProfile = useCallback(async () => { - if (!session) return; - const { user } = session; - - const { data } = await supabase + const fetchAdminStatus = useCallback((userId: string) => { + supabase .from('profiles') - .select('username, pfp_id') - .eq('profile_id', user.id) - .single(); + .select('is_admin') + .eq('profile_id', userId) + .single() + .then(({ data, error }) => { + if (!error && data) setIsAdmin(data.is_admin); + }); + }, []); - if (data) { - setUserData({ - name: data.username, - pfp_id: data.pfp_id, + useEffect(() => { + // PKCE code exchange + const code = new URLSearchParams(window.location.search).get('code'); + if (code) { + supabase.auth.exchangeCodeForSession(code).then(({ data, error }) => { + if (!error && data.session) { + setSession(data.session); + fetchAdminStatus(data.session.user.id); + window.history.replaceState({}, '', window.location.pathname + window.location.hash); + } }); } - }, [session]); - useEffect(() => { - supabase.auth.onAuthStateChange((_event, newSession) => { + const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, newSession) => { if (_event === 'PASSWORD_RECOVERY') { setIsRecoverySession(true); + setSession(newSession); return; } setIsRecoverySession(false); setSession(newSession); - if (newSession?.user) { - supabase - .from('profiles') - .select('is_admin') - .eq('profile_id', newSession.user.id) - .single() - .then(({ data, error }) => { - if (!error && data) { - setIsAdmin(data.is_admin); - } - }); + fetchAdminStatus(newSession.user.id); } else { setIsAdmin(false); } }); - if (!isRecoverySession) { + // only fetch existing session if no code to exchange + if (!code) { supabase.auth.getSession().then(({ data: { session: currentSession } }) => { setSession(currentSession); - if (currentSession?.user) { - supabase - .from('profiles') - .select('is_admin') - .eq('profile_id', currentSession.user.id) - .single() - .then(({ data, error }) => { - if (!error && data) { - setIsAdmin(data.is_admin); - } - }); - } + if (currentSession?.user) fetchAdminStatus(currentSession.user.id); }); } - }, [isRecoverySession]); + + return () => subscription.unsubscribe(); + }, []); + + const fetchProfile = useCallback(async () => { + if (!session) return; + const { data } = await supabase + .from('profiles') + .select('username, pfp_id') + .eq('profile_id', session.user.id) + .single(); + if (data) { + setUserData({ name: data.username, pfp_id: data.pfp_id }); + } + }, [session]); useEffect(() => { fetchProfile(); }, [fetchProfile]); - return { - session, - userData, - isAdmin, - isRecoverySession, - fetchProfile, - }; -} + return { session, userData, isAdmin, isRecoverySession, fetchProfile }; +} \ No newline at end of file diff --git a/src/public-problems b/src/public-problems index 26639174..460fdd1e 160000 --- a/src/public-problems +++ b/src/public-problems @@ -1 +1 @@ -Subproject commit 26639174284e563dfcf6631eee8fb08ab8eafa24 +Subproject commit 460fdd1e8bc17dd118ad5be93a99fc1a2ceddade diff --git a/src/routes/AuthCallback.tsx b/src/routes/AuthCallback.tsx index a205d401..d3c1c054 100644 --- a/src/routes/AuthCallback.tsx +++ b/src/routes/AuthCallback.tsx @@ -1,43 +1,22 @@ -import { useEffect, useState } from 'react'; +import { useEffect } from 'react'; +import { useNavigate } from 'react-router'; import { supabase } from '../supabaseClient'; -import { Navigate } from 'react-router-dom'; export default function AuthCallback() { - const [redirectTo, setRedirectTo] = useState(null) - const [tokens, setTokens] = useState<{ access_token: string, refresh_token: string } | null >(null); + const navigate = useNavigate(); useEffect(() => { - // parsing the URL for the access and refresh tokens + const code = new URLSearchParams(window.location.search).get('code'); - // hash should give something like ex: - // #/auth/callback#access_token=...&type=recovery - const hash = window.location.hash; - - // split gives ['', '/auth/callback', 'access_token=...&type=recovery'] - // slice to remove first 2 indexes - // join reassembles in case of another # - const tokenPart = hash.split('#').slice(2).join('#'); - - if (tokenPart) { - const params = new URLSearchParams(tokenPart); - const access_token = params.get('access_token'); - const refresh_token = params.get('refresh_token'); - const type = params.get('type'); - - if (access_token && refresh_token) { - supabase.auth.setSession({ access_token, refresh_token }).then(() => { - setTokens({ access_token, refresh_token }); - if (type === 'recovery') { - setRedirectTo('/auth/change-password'); - } else { - setRedirectTo('/profile'); - } - }); - } + if (code) { + supabase.auth.exchangeCodeForSession(code).then(({ error }) => { + if (error) console.error('Auth callback error:', error); + navigate('/', { replace: true }); + }); + } else { + navigate('/', { replace: true }); } - }, []); - - if (redirectTo) return ; + }, [navigate]); - return

Confirming your account...

; + return

Signing you in...

; } \ No newline at end of file diff --git a/src/routes/ChangePassword.tsx b/src/routes/ChangePassword.tsx index d15d106d..408234d9 100644 --- a/src/routes/ChangePassword.tsx +++ b/src/routes/ChangePassword.tsx @@ -1,41 +1,26 @@ import { FormEvent, useState } from 'react' import { supabase } from '../supabaseClient' import { Box, Button, FormLabel, Input, Stack, Typography } from '@mui/joy'; -import { useLocation } from 'react-router-dom'; import { Navigate } from 'react-router-dom'; /** * Change Password page for the app. */ export default function ChangePassword() { - const location = useLocation(); - const [done, setDone] = useState(false); - const [loading, setLoading] = useState(false); const [newPassword, setNewPassword] = useState(""); - const [error, setError] = useState(""); - const [success, setSuccess] = useState(""); const handleSubmit = async (event: FormEvent) => { event.preventDefault(); setError(""); - setSuccess(""); setLoading(true); - const { access_token, refresh_token } = location.state || {}; - if (access_token && refresh_token) { - await supabase.auth.setSession({ access_token, refresh_token }); - } - - const { error } = await supabase.auth.updateUser({ - password: newPassword - }); + const { error } = await supabase.auth.updateUser({ password: newPassword }); if (error) { setError(error.message); - setLoading(false); } else { await supabase.auth.signOut(); setDone(true); @@ -43,15 +28,14 @@ export default function ChangePassword() { setLoading(false); } - // On success, navigate to signin - if(done) return + if (done) return ; return ( Change Password
- Password + New Password Loading : Change Password} - { !!error && {error} } - { !!success && {success} } + {!!error && {error}}
) } diff --git a/src/routes/ReqPasswordChange.tsx b/src/routes/ReqPasswordChange.tsx index 3e4b7503..85013da8 100644 --- a/src/routes/ReqPasswordChange.tsx +++ b/src/routes/ReqPasswordChange.tsx @@ -21,7 +21,7 @@ export default function ReqPasswordChange() { // !! if this is edited, it needs to match with the Supabase redirect URLs !! // check 'Authentication/URL Configuration // ${window.location.origin} sets it dynamically to localhost or coding-cat.club - redirectTo: `${window.location.origin}/#/auth/callback` + redirectTo: `${window.location.origin}/` }); if(error){ diff --git a/src/routes/root.tsx b/src/routes/root.tsx index 977b1f44..9a9dafa6 100644 --- a/src/routes/root.tsx +++ b/src/routes/root.tsx @@ -24,6 +24,13 @@ export default function App() { const navigate = useNavigate(); const { session, userData, isAdmin, isRecoverySession, fetchProfile } = useAuth(); + + useEffect(() => { + if(isRecoverySession){ + navigate('/auth/change-password', { replace: true }); + } + }, [navigate, isRecoverySession]); + const { progress, contractProgress, fetchProgress } = useContractData(session); const { activeSession, sessionId, sessionDuration,sessionRemainingSeconds,plannedExerciseCount, diff --git a/src/supabaseClient.ts b/src/supabaseClient.ts index e1d05845..5a504a52 100644 --- a/src/supabaseClient.ts +++ b/src/supabaseClient.ts @@ -8,7 +8,7 @@ export const supabase = createClient( supabaseAnonKey, { auth: { - flowType: 'implicit' + flowType: 'pkce' } } ); From f06e3cf6d4869be38a6ae62da300b684ef6e2556 Mon Sep 17 00:00:00 2001 From: Luke Weaver Date: Tue, 26 May 2026 16:04:04 -0400 Subject: [PATCH 2/4] Add fetchAdminStatus to deps array --- src/hooks/useAuth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useAuth.ts b/src/hooks/useAuth.ts index c630bae1..a8a9ddf4 100644 --- a/src/hooks/useAuth.ts +++ b/src/hooks/useAuth.ts @@ -69,7 +69,7 @@ export default function useAuth(): UseAuthReturn { } return () => subscription.unsubscribe(); - }, []); + }, [fetchAdminStatus]); const fetchProfile = useCallback(async () => { if (!session) return; From 4b1dec096b8eaa2382fd4c3fca47ea43cd6f6372 Mon Sep 17 00:00:00 2001 From: Luke Weaver Date: Wed, 27 May 2026 15:22:07 -0400 Subject: [PATCH 3/4] Hopefully fix auth flow --- src/routes/ReqPasswordChange.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/routes/ReqPasswordChange.tsx b/src/routes/ReqPasswordChange.tsx index 85013da8..6fd3d211 100644 --- a/src/routes/ReqPasswordChange.tsx +++ b/src/routes/ReqPasswordChange.tsx @@ -18,10 +18,11 @@ export default function ReqPasswordChange() { setLoading(true); const { error } = await supabase.auth.resetPasswordForEmail(email, { + // dynamically builds the base URL to support both coding-cat.club and + // the github pages deployment at coding-cat-official.github.io/coding-cat // !! if this is edited, it needs to match with the Supabase redirect URLs !! - // check 'Authentication/URL Configuration - // ${window.location.origin} sets it dynamically to localhost or coding-cat.club - redirectTo: `${window.location.origin}/` + // check 'Authentication/URL Configuration' + redirectTo: `${window.location.origin}${window.location.pathname.split('/').slice(0, 2).join('/')}/` }); if(error){ From 5d9e58235a2302605582e6652642587d56ddb051 Mon Sep 17 00:00:00 2001 From: Luke Weaver Date: Wed, 27 May 2026 15:28:59 -0400 Subject: [PATCH 4/4] Remaining HEAD? --- src/routes/ReqPasswordChange.tsx | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/routes/ReqPasswordChange.tsx b/src/routes/ReqPasswordChange.tsx index 8b79cbf1..6fd3d211 100644 --- a/src/routes/ReqPasswordChange.tsx +++ b/src/routes/ReqPasswordChange.tsx @@ -21,14 +21,8 @@ export default function ReqPasswordChange() { // dynamically builds the base URL to support both coding-cat.club and // the github pages deployment at coding-cat-official.github.io/coding-cat // !! if this is edited, it needs to match with the Supabase redirect URLs !! -<<<<<<< HEAD // check 'Authentication/URL Configuration' redirectTo: `${window.location.origin}${window.location.pathname.split('/').slice(0, 2).join('/')}/` -======= - // check 'Authentication/URL Configuration - // ${window.location.origin} sets it dynamically to localhost or coding-cat.club - redirectTo: `${window.location.origin}/` ->>>>>>> 0c9fb3f1c0ac3efdc9e779bb448c6c9f7d22853c }); if(error){