fix(auth): handle users without household — fallback to 'Kein Haushalt'
Removes non-null assertions on householdId/householdName. Users who haven't joined a household get a fallback name in the sidebar. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2
frontend/src/app.d.ts
vendored
2
frontend/src/app.d.ts
vendored
@@ -12,7 +12,7 @@ declare global {
|
||||
rolle: 'planer' | 'mitglied';
|
||||
};
|
||||
haushalt?: {
|
||||
id: string;
|
||||
id: string | undefined;
|
||||
name: string;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -101,6 +101,36 @@ describe('auth guard (hooks.server.ts handle)', () => {
|
||||
expect(resolve).toHaveBeenCalledWith(event);
|
||||
});
|
||||
|
||||
it('handles user without household gracefully', async () => {
|
||||
mockGet.mockResolvedValue({
|
||||
data: {
|
||||
data: {
|
||||
id: '456',
|
||||
displayName: 'Neu',
|
||||
householdId: null,
|
||||
householdName: null,
|
||||
householdRole: null,
|
||||
email: 'neu@example.com',
|
||||
systemRole: 'user'
|
||||
}
|
||||
},
|
||||
error: undefined
|
||||
});
|
||||
|
||||
const { event, resolve } = createEvent('/planner', 'valid-session');
|
||||
await handle({ event, resolve });
|
||||
expect(event.locals.benutzer).toEqual({
|
||||
id: '456',
|
||||
name: 'Neu',
|
||||
rolle: 'mitglied'
|
||||
});
|
||||
expect(event.locals.haushalt).toEqual({
|
||||
id: undefined,
|
||||
name: 'Kein Haushalt'
|
||||
});
|
||||
expect(resolve).toHaveBeenCalledWith(event);
|
||||
});
|
||||
|
||||
it('redirects to /login with redirect param when session validation fails', async () => {
|
||||
mockGet.mockResolvedValue({ data: undefined, error: { status: 401 } });
|
||||
|
||||
|
||||
@@ -39,11 +39,11 @@ export const handle: Handle = async ({ event, resolve }) => {
|
||||
event.locals.benutzer = {
|
||||
id: user.id!,
|
||||
name: user.displayName!,
|
||||
rolle: user.householdRole as 'planer' | 'mitglied'
|
||||
rolle: (user.householdRole as 'planer' | 'mitglied') ?? 'mitglied'
|
||||
};
|
||||
event.locals.haushalt = {
|
||||
id: user.householdId!,
|
||||
name: user.householdName!
|
||||
id: user.householdId ?? undefined,
|
||||
name: user.householdName ?? 'Kein Haushalt'
|
||||
};
|
||||
|
||||
return resolve(event);
|
||||
|
||||
Reference in New Issue
Block a user