feat: refactor server with shelf_static, return JSON responses, add API docs
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
|
||||
import 'package:dotenv/dotenv.dart';
|
||||
import 'dart:convert';
|
||||
|
||||
class AuthMiddleware {
|
||||
final Future<Response> Function(Request, String) handler;
|
||||
@@ -11,7 +12,7 @@ class AuthMiddleware {
|
||||
final authorization = request.headers['authorization'];
|
||||
|
||||
if (authorization == null || !authorization.startsWith('Bearer ')) {
|
||||
return Response(401, body: 'Authorization header missing or invalid');
|
||||
return Response(401, body: jsonEncode({'error': 'Authorization header missing or invalid'}), headers: {'Content-Type': 'application/json'});
|
||||
}
|
||||
|
||||
final token = authorization.substring(7);
|
||||
@@ -25,9 +26,9 @@ class AuthMiddleware {
|
||||
|
||||
return handler(request, login);
|
||||
} on JWTExpiredException {
|
||||
return Response(401, body: 'Token expired');
|
||||
return Response(401, body: jsonEncode({'error': 'Token expired'}), headers: {'Content-Type': 'application/json'});
|
||||
} on JWTException {
|
||||
return Response(401, body: 'Invalid token');
|
||||
return Response(401, body: jsonEncode({'error': 'Invalid token'}), headers: {'Content-Type': 'application/json'});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user