guest static method
Factory method for guest-only middleware (redirects authenticated users)
Implementation
static Middleware guest({
String redirectTo = '/dashboard',
List<String> except = const [],
String guard = 'web',
}) {
return Middleware(
(Request request, ResponseContract response, NextFunction next) async {
// Check if route is excluded
if (_isExcluded(request.path, except)) {
return next();
}
// Check if user is authenticated
final user = request.session.get('user');
final token = request.session.get('token') as String?;
if (user != null && token != null) {
if (response is Response) {
await response.redirect(redirectTo);
} else {
response.status(401).sendJson({'message': 'Unauthenticated'});
}
return;
}
// Continue to next middleware
return next();
},
priority: MiddlewarePriority.auth,
name: 'web-guest',
);
}