Merge pull request 'bugfix: logout behaviour fixed.' (#5) from bugfix/logout-bad-state into master
Reviewed-on: http://178.131.50.201:3000/admin/dyolink/pulls/5
This commit was merged in pull request #5.
This commit is contained in:
@@ -136,6 +136,28 @@ export class AuthController {
|
||||
return this.authService.getProfile(req.user.id);
|
||||
}
|
||||
|
||||
// =========================
|
||||
// LOGOUT
|
||||
// =========================
|
||||
@Post('logout')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiOperation({ summary: 'Logout current user' })
|
||||
@ApiResponse({ status: 200, description: 'Logout successful' })
|
||||
async logout(@Req() req, @Res({ passthrough: true }) res: Response) {
|
||||
const accessToken = req?.cookies?.accessToken;
|
||||
|
||||
if (accessToken) {
|
||||
await this.authService.logout(accessToken);
|
||||
}
|
||||
|
||||
this.clearAuthCookies(res);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'Logged out successfully',
|
||||
};
|
||||
}
|
||||
|
||||
// =========================
|
||||
// TEST
|
||||
// =========================
|
||||
@@ -174,4 +196,19 @@ export class AuthController {
|
||||
path: '/',
|
||||
});
|
||||
}
|
||||
|
||||
private clearAuthCookies(res: Response) {
|
||||
res.clearCookie('accessToken', {
|
||||
httpOnly: true,
|
||||
secure: false,
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
});
|
||||
res.clearCookie('refreshToken', {
|
||||
httpOnly: true,
|
||||
secure: false,
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -181,11 +181,22 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
}, [router]);
|
||||
|
||||
const logout = useCallback(async () => {
|
||||
try {
|
||||
// Important: clear auth cookies/session on the server first,
|
||||
// otherwise middleware may still treat the user as authenticated.
|
||||
await authApi.logout();
|
||||
} catch (err) {
|
||||
console.error('Logout API failed:', err);
|
||||
} finally {
|
||||
localStorage.clear();
|
||||
setUser(null);
|
||||
setOrganizations([]);
|
||||
setCurrentOrganization(null);
|
||||
router.push('/');
|
||||
setError(null);
|
||||
setIsAuthReady(true);
|
||||
router.replace('/');
|
||||
router.refresh();
|
||||
}
|
||||
}, [router]);
|
||||
|
||||
const selectOrganization = useCallback(async (orgId: string) => {
|
||||
|
||||
Reference in New Issue
Block a user