Skip to content

Commit

Permalink
Merge pull request #169 from javad-zobeidi/dev
Browse files Browse the repository at this point in the history
Add Redirect Exception
  • Loading branch information
javad-zobeidi authored Feb 3, 2025
2 parents 09f7c96 + cf7fad7 commit c2fc2c0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/src/authentication/redirect_if_authenticated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class RedirectIfAuthenticated extends Middleware {
Future handle(Request req) async {
bool loggedIn = await getSession<bool?>('logged_in') ?? false;
if (loggedIn) {
return Response.redirect(path);
throw RedirectException(
message: path,
responseType: ResponseType.html,
);
}
}
}
13 changes: 8 additions & 5 deletions lib/src/exception/http_exception.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'dart:io';

import '../http/response/response.dart';
import 'base_http_exception.dart';

class HttpResponseException extends BaseHttpResponseException {
HttpResponseException(
{required super.message,
required super.code,
super.responseType = ResponseType.json,
super.errorCode = 'Error'});
HttpResponseException({
super.message,
super.code = HttpStatus.found,
super.responseType = ResponseType.json,
super.errorCode = 'Error',
});
}
12 changes: 12 additions & 0 deletions lib/src/exception/redirect_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'dart:io';

import '../http/response/response.dart';
import 'base_http_exception.dart';

class RedirectException extends BaseHttpResponseException {
RedirectException({
super.message,
super.code = HttpStatus.found,
super.responseType = ResponseType.html,
});
}
4 changes: 4 additions & 0 deletions lib/src/http/request/request_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class RequestHandler {
return Response.redirect(error.message).makeResponse(req.response);
}

if (error is RedirectException && isHtml) {
return Response.redirect(error.message).makeResponse(req.response);
}

error
.response(
isHtml,
Expand Down
1 change: 1 addition & 0 deletions lib/vania.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export 'src/http/response/response.dart';

export 'src/exception/base_http_exception.dart';
export 'src/exception/http_exception.dart';
export 'src/exception/redirect_exception.dart';

export 'src/websocket/websocket_client.dart';
export 'src/websocket/websocket_event.dart';
Expand Down

0 comments on commit c2fc2c0

Please sign in to comment.