Skip to content

Commit 0f7c4cf

Browse files
committed
feat: refine authentication flow and dashboard data hooks
1 parent 031eabe commit 0f7c4cf

4 files changed

Lines changed: 64 additions & 28 deletions

File tree

react/features/dashboard/components/Sidebar.tsx

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,51 @@ const Sidebar: React.FC<SidebarProps> = ({ activeTab, setActiveTab, isCollapsed,
1414
const [userProfile, setUserProfile] = useState<any>(null);
1515

1616
useEffect(() => {
17-
const fetchProfile = async () => {
17+
const getFallbackProfile = (session: any) => {
18+
const fullName = session?.user?.user_metadata?.full_name
19+
|| session?.user?.user_metadata?.name
20+
|| session?.user?.email?.split('@')[0]
21+
|| 'User';
22+
23+
return {
24+
full_name: fullName,
25+
email: session?.user?.email || ''
26+
};
27+
};
28+
29+
const syncProfile = async (session: any) => {
30+
if (!session) {
31+
setUserProfile(null);
32+
return;
33+
}
34+
35+
setUserProfile(getFallbackProfile(session));
36+
1837
try {
19-
const { data: { session } } = await supabase.auth.getSession();
20-
if (!session) return;
21-
2238
const res = await fetch('/api/v1/users/me', {
2339
headers: { 'Authorization': `Bearer ${session.access_token}` }
2440
});
2541
if (res.ok) {
2642
const data = await res.json();
27-
setUserProfile(data);
43+
setUserProfile({
44+
...getFallbackProfile(session),
45+
...data
46+
});
2847
}
2948
} catch (err) {
3049
console.error("Failed to fetch profile:", err);
3150
}
3251
};
33-
fetchProfile();
52+
53+
supabase.auth.getSession().then(({ data: { session } }) => {
54+
void syncProfile(session);
55+
});
56+
57+
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => {
58+
void syncProfile(session);
59+
});
60+
61+
return () => subscription.unsubscribe();
3462
}, []);
3563

3664
// Helper to close mobile menu on tab switch

react/features/dashboard/hooks/useDashboardData.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ export const useDashboardData = () => {
4242

4343
useEffect(() => {
4444
void refresh();
45+
46+
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => {
47+
if (session) {
48+
void refresh();
49+
} else {
50+
setState({
51+
meetings: [],
52+
stats: null,
53+
actionItems: [],
54+
loading: false,
55+
error: 'No active session'
56+
});
57+
}
58+
});
59+
60+
return () => subscription.unsubscribe();
4561
}, []);
4662

4763
return {

react/features/supabase-auth/components/Auth.web.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import React, { useState } from 'react';
22
import { supabase } from '../client';
33

4-
interface AuthProps {
5-
onLoginSuccess: () => void;
6-
}
7-
8-
export const Auth: React.FC<AuthProps> = ({ onLoginSuccess }) => {
4+
export const Auth: React.FC = () => {
95
const [isSignUp, setIsSignUp] = useState(false);
106
const [email, setEmail] = useState('');
117
const [password, setPassword] = useState('');
@@ -31,14 +27,12 @@ export const Auth: React.FC<AuthProps> = ({ onLoginSuccess }) => {
3127
password,
3228
});
3329
if (signInError) throw signInError;
34-
onLoginSuccess();
3530
} else {
3631
const { error: signInError } = await supabase.auth.signInWithPassword({
3732
email,
3833
password,
3934
});
4035
if (signInError) throw signInError;
41-
onLoginSuccess();
4236
}
4337
} catch (err: any) {
4438
setError(err.message || 'An error occurred during authentication.');

react/features/supabase-auth/components/AuthWrapper.web.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,25 @@ export const AuthWrapper: React.FC<AuthWrapperProps> = ({ children }) => {
1414
const dispatch = useDispatch();
1515

1616
useEffect(() => {
17-
supabase.auth.getSession().then(({ data: { session } }) => {
18-
if (session) {
19-
setSession(session);
17+
const syncSession = (nextSession: any) => {
18+
setSession(nextSession);
19+
20+
if (nextSession?.user?.email) {
2021
dispatch(updateSettings({
21-
displayName: session.user.email.split('@')[0],
22-
email: session.user.email
22+
displayName: nextSession.user.email.split('@')[0],
23+
email: nextSession.user.email
2324
}));
2425
}
26+
2527
setLoading(false);
28+
};
29+
30+
supabase.auth.getSession().then(({ data: { session } }) => {
31+
syncSession(session);
2632
});
2733

2834
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => {
29-
if (session) {
30-
setSession(session);
31-
dispatch(updateSettings({
32-
displayName: session.user.email.split('@')[0],
33-
email: session.user.email
34-
}));
35-
} else {
36-
setSession(null);
37-
}
35+
syncSession(session);
3836
});
3937

4038
return () => subscription.unsubscribe();
@@ -49,7 +47,7 @@ export const AuthWrapper: React.FC<AuthWrapperProps> = ({ children }) => {
4947
}
5048

5149
if (!session) {
52-
return <Auth onLoginSuccess={() => setSession(true)} />;
50+
return <Auth />;
5351
}
5452

5553
return <>{children}</>;

0 commit comments

Comments
 (0)