logout static method
Logs out the current session associated with the request.
This method attempts to:
- locate the current session cookie
- remove the corresponding persisted session record
- remove the session from the in-memory registry
- clear the user bound to the current request session
Returns true if a matching session was found and removed; otherwise
returns false.
Any exception during logout results in false.
Example:
final didLogout = await AuthSession.logout(request);
if (didLogout) {
print('Session ended successfully');
}
Implementation
static Future<bool> logout(HttpRequest request) async {
try {
final cookie = request.cookies.firstWhereOrNull((cookie) => cookie.name == "archery_session");
final authSessions = App().tryMake<List<AuthSession>>();
final session = authSessions?.firstWhereOrNull((session) => session.cookie?.value == cookie?.value);
if (session != null) {
final sessionRecord = await Model.firstWhere<AuthSession>(field: "email", value: session.email);
await sessionRecord?.delete();
authSessions?.remove(session);
request.thisSession?.user = null;
return true;
}
return false;
} catch (e) {
return false;
}
}