Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Session Manager #154

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/http/session/session_file_store .dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SessionFileStore {
/// The default duration is 1 hour.
///
/// The method is asynchronous and returns a Future<void>.
void storeSession(String sessionId, Map<String, dynamic> data,
Future<void> storeSession(String sessionId, Map<String, dynamic> data,
{Duration duration = const Duration(hours: 1)}) async {
sessionId = _makeHash(sessionId).toString();
final file = File('$sessionPath/$sessionId');
Expand Down
15 changes: 8 additions & 7 deletions lib/src/http/session/session_manager.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:vania/vania.dart';
import 'session_file_store .dart';

class SessionManager {
Expand All @@ -11,7 +11,8 @@ class SessionManager {

HttpRequest? _request;

final Duration _sessionTimeout = const Duration(seconds: 3600);
final Duration _sessionLifeTime =
Duration(seconds: env<int>('SESSION_LIFETIME', 3600));

final Random _random = Random.secure();

Expand Down Expand Up @@ -60,7 +61,7 @@ class SessionManager {
..httpOnly = true
..secure = false
..path = '/'
..expires = DateTime.now().add(_sessionTimeout),
..expires = DateTime.now().add(_sessionLifeTime),
);

return sessionId;
Expand All @@ -85,7 +86,7 @@ class SessionManager {
/// Returns:
/// A map containing the session data if a valid session exists, otherwise
/// returns null.
Map<String, dynamic>? getAllSessions() {
Map<String, dynamic>? allSessions() {
final sessionId = getSessionId();
if (sessionId != null) {
if (!SessionFileStore().hasSession(sessionId)) {
Expand All @@ -100,7 +101,7 @@ class SessionManager {
}

dynamic getSession(String key) {
Map<String, dynamic>? session = getAllSessions();
Map<String, dynamic>? session = allSessions();
return session?[key];
}

Expand All @@ -111,13 +112,13 @@ class SessionManager {
/// and saves the updated session data. If the session does not exist or is invalid,
/// it does not store the value.
///
void setSession(String key, dynamic value) {
Future<void> setSession(String key, dynamic value) async {
final sessionId = getSessionId();
if (sessionId != null) {
Map<String, dynamic> session =
SessionFileStore().retrieveSession(sessionId) ?? {};
session.addAll({key: value});
SessionFileStore().storeSession(sessionId, session);
await SessionFileStore().storeSession(sessionId, session);
}
}

Expand Down
Loading