diff --git a/src/masonite/drivers/authentication/AuthJwtDriver.py b/src/masonite/drivers/authentication/AuthJwtDriver.py index 66b6239ae..66cbbb218 100644 --- a/src/masonite/drivers/authentication/AuthJwtDriver.py +++ b/src/masonite/drivers/authentication/AuthJwtDriver.py @@ -89,7 +89,8 @@ def save(self, _, **kwargs): serialized_dictionary = model.serialize() serialized_dictionary.update({"expired": cookie_expire_time("5 minutes")}) token = self.jwt.encode(serialized_dictionary, KEY, algorithm="HS256") - token = bytes(token).decode("utf-8") + if isinstance(token, bytes): + token = bytes(token).decode("utf-8") self.request.cookie("token", token) def delete(self): diff --git a/src/masonite/helpers/misc.py b/src/masonite/helpers/misc.py index 70cc9f7a9..39d90fa42 100644 --- a/src/masonite/helpers/misc.py +++ b/src/masonite/helpers/misc.py @@ -105,6 +105,8 @@ def __new__(cls, *args): def deprecated(message): + warnings.simplefilter("default", DeprecationWarning) + def deprecated_decorator(func): def deprecated_func(*args, **kwargs): warnings.warn( @@ -112,7 +114,6 @@ def deprecated_func(*args, **kwargs): category=DeprecationWarning, stacklevel=2, ) - warnings.simplefilter("default", DeprecationWarning) return func(*args, **kwargs) return deprecated_func diff --git a/src/masonite/providers/RouteProvider.py b/src/masonite/providers/RouteProvider.py index 5bf9be64c..d7b751b6a 100644 --- a/src/masonite/providers/RouteProvider.py +++ b/src/masonite/providers/RouteProvider.py @@ -118,7 +118,8 @@ def boot(self, router: Route, request: Request, response: Response): """No Response was found in the for loop so let's set an arbitrary response now. """ - response.view("Route not found. Error 404", status=404) # If the route exists but not the method is incorrect - if request.is_status(404) and request.route_exists(request.path): + if request.route_exists(request.path): response.view("Method not allowed", status=405) + else: + response.view("Route not found. Error 404", status=404) diff --git a/src/masonite/request.py b/src/masonite/request.py index 11609b516..04dfb985e 100644 --- a/src/masonite/request.py +++ b/src/masonite/request.py @@ -955,7 +955,16 @@ def __getattr__(self, key): raise AttributeError("class 'Request' has no attribute {}".format(key)) def with_errors(self, errors): - self.session.flash("errors", errors) + """Easily attach errors message to session request.""" + return self.with_flash("error", errors) + + def with_success(self, success): + """Easily attach success message to session request.""" + return self.with_flash("success", success) + + def with_flash(self, key, value): + """Easily attach data to session request.""" + self.session.flash(key, value) return self def reset_redirections(self): diff --git a/tests/core/test_session.py b/tests/core/test_session.py index 7551114dc..90894d882 100644 --- a/tests/core/test_session.py +++ b/tests/core/test_session.py @@ -141,7 +141,7 @@ def test_can_redirect_with_inputs(self): def test_can_redirect_with_bytes_inputs(self): for driver in ('memory', 'cookie'): - + request = self.container.make('Request') session = self.container.make('SessionManager').driver(driver) request.request_variables = { @@ -167,3 +167,20 @@ def test_intended_returns_correct_url(self): # Assert redirect intended method resets the redirection request.redirect_intended() self.assertEqual(request.session.get('__intend'), None) + + def test_with_flash(self): + request = self.container.make('Request') + request.redirect('/dashboard').with_flash('success', 'Ok') + self.assertEqual(request.session.get('success'), 'Ok') + request.redirect('/dashboard').with_flash('any_key', 'any_value') + self.assertEqual(request.session.get('any_key'), 'any_value') + + def test_with_errors(self): + request = self.container.make('Request') + request.redirect('/dashboard').with_errors('Form error') + self.assertEqual(request.session.get('error'), 'Form error') + + def test_with_success(self): + request = self.container.make('Request') + request.redirect('/dashboard').with_success('Created !') + self.assertEqual(request.session.get('success'), 'Created !')