34 lines
1.2 KiB
Dart
34 lines
1.2 KiB
Dart
import 'package:backend_dart/domain/errors/app_error.dart';
|
|
import 'package:backend_dart/domain/errors/error.dart';
|
|
import 'package:backend_dart/domain/repository/auth_repository.dart';
|
|
import 'package:fpdart/fpdart.dart';
|
|
import 'package:shelf/shelf.dart';
|
|
|
|
TaskOption<String> readCookie(Request request, String cookieName) {
|
|
// Retrieve the "Cookie" header
|
|
final cookieHeader = request.headers['cookie'];
|
|
if (cookieHeader == null) return TaskOption.none();
|
|
// No cookies found
|
|
|
|
// Split the cookies into individual key-value pairs
|
|
final cookies = cookieHeader.split(';');
|
|
|
|
// Find the cookie with the matching name
|
|
for (var cookie in cookies) {
|
|
final parts = cookie.trim().split('=');
|
|
if (parts.length == 2 && parts[0] == cookieName && parts[1].isNotEmpty) {
|
|
return TaskOption.some(parts[1]);
|
|
}
|
|
}
|
|
|
|
return TaskOption.none(); // Cookie not found
|
|
}
|
|
|
|
TaskEither<IError, String> checkAuth(
|
|
Request request, AuthRepository authRepository) {
|
|
return readCookie(request, 'session_token')
|
|
.toTaskEither<IError>(() => AppError.authenticationError(
|
|
message: 'No token found in the request'))
|
|
.flatMap(authRepository.validateToken);
|
|
}
|