Skip to content

Commit 7d054b1

Browse files
committed
fix: bug with showing user information and enhance authentication and user display features
- Updated LoginForm to store a complete authentication structure in localStorage, improving user session management. - Added user type display in ChatSidebar to differentiate between local and OAuth users. - Refactored Chat component to handle user initialization more robustly, ensuring proper navigation and state management during authentication.
1 parent ea52e8e commit 7d054b1

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

frontend/src/features/authentication/LoginForm.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,25 @@ const LoginForm = ({ providers, returnUrl = '/chat/new' }: LoginFormProps) => {
233233
...user,
234234
password_change_required: false,
235235
};
236-
localStorage.setItem('auth', JSON.stringify(updatedUser));
236+
237+
// Get current auth data
238+
const currentAuth = localStorage.getItem('auth');
239+
let authData;
240+
try {
241+
authData = currentAuth ? JSON.parse(currentAuth) : null;
242+
} catch {
243+
authData = null;
244+
}
245+
246+
// Create a full authentication info structure
247+
const updatedAuthData = {
248+
type: 'user',
249+
user: updatedUser,
250+
providers: authData?.providers || [],
251+
};
252+
253+
// Always store the complete auth structure
254+
localStorage.setItem('auth', JSON.stringify(updatedAuthData));
237255
navigate(returnUrl);
238256
}
239257
};

frontend/src/features/chat/ChatSidebar.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ const ChatSidebar = ({
352352
<div className="grid flex-1 text-left text-sm leading-tight">
353353
<span className="truncate font-semibold">{user?.name}</span>
354354
<span className="truncate text-xs">{user?.mail}</span>
355+
<span className="truncate text-xs text-muted-foreground">
356+
{user?.type === 'local' ? 'local' : 'oauth'}
357+
</span>
355358
</div>
356359
</div>
357360
</DropdownMenuLabel>

frontend/src/pages/Chat.tsx

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ const Chat = () => {
6868
const [selectedProvider, setSelectedProvider] = useState<string | null>(null);
6969
const [user, setUser] = useState<User | null>(null);
7070
const { isDesktop } = useBreakpoint();
71-
const needsUserUpdateRef = useRef(false);
7271
const needsProviderUpdateRef = useRef(true);
73-
const userDataRef = useRef<User | null>(null);
7472
const previousFlowIdRef = useRef(flowId);
73+
const userInitialized = useRef(false);
7574

7675
// Check if provider needs initialization or update when:
7776
// 1. We have no selected provider yet (first login)
@@ -125,28 +124,27 @@ const Chat = () => {
125124
return;
126125
}
127126

128-
const user = JSON.parse(auth)?.user;
129-
130-
if (!user) {
131-
// Save current path for redirect after login
132-
const currentPath = window.location.pathname;
133-
// Only save if it's not the default route
134-
const returnParam = currentPath !== '/chat/new' ? `?returnUrl=${encodeURIComponent(currentPath)}` : '';
135-
navigate(`/login${returnParam}`);
136-
return;
127+
try {
128+
const authData = JSON.parse(auth);
129+
const user = authData?.user;
130+
131+
if (!user) {
132+
// Save current path for redirect after login
133+
const currentPath = window.location.pathname;
134+
// Only save if it's not the default route
135+
const returnParam = currentPath !== '/chat/new' ? `?returnUrl=${encodeURIComponent(currentPath)}` : '';
136+
navigate(`/login${returnParam}`);
137+
return;
138+
} else {
139+
userInitialized.current = true;
140+
window.requestAnimationFrame(() => setUser(user));
141+
}
142+
} catch {
143+
// If we have a parse error, redirect to login
144+
navigate('/login');
137145
}
138-
139-
needsUserUpdateRef.current = true;
140-
userDataRef.current = user;
141146
}, [navigate]);
142147

143-
useLayoutEffect(() => {
144-
if (needsUserUpdateRef.current && userDataRef.current) {
145-
needsUserUpdateRef.current = false;
146-
setUser(userDataRef.current);
147-
}
148-
}, []);
149-
150148
// Handle provider selection changes
151149
const handleProviderChange = (provider: string) => {
152150
setSelectedProvider(provider);

0 commit comments

Comments
 (0)