Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

xMarston's avatar

Laravel 5.1 Middleware to check the creation date from a user to change password

I'm developing a webapp with Laravel 5.1 and I'm building the authentication system and I have to check if the user has not changed his password in six months or more and I would use a middleware to check this but I didn't find how I can do it properly. I created a global middleware but it is not working because I can't get the authenticated user.

It is possible that I have to use an AfterMiddleware to check the password?

0 likes
8 replies
WebSpanner's avatar

I assume the global middleware idea fails when the user hits a route before they sign in, like the login page. Wouldn't it be best just to put the middleware on the __contruct() method of the controllers which you want to trigger the middleware? Takes a little longer than just one global middleware, but lets you avoid routes which are publicly accessible by users that aren't signed in.

xMarston's avatar

@bestmomo I want to check this in all the private routes and only allow the user enter when they update their passwords.

pmall's avatar

Middleware has nothing to do with this, as it is not related to request or response.

I would put this in a View Composer. If the user hasn't changed its password for too long you pass a notification to the view.

xMarston's avatar

@pmall Mmm, it is a good idea but the problem is that if the lifetime of the password is superior I don't have to let the user login until he change his password.

pmall's avatar

In this case you can put it in a middleware because it will act on the response : it will redirect the user somewhere if this condition is met.

You have to use a route middleware, because session isnt available in global middleware.

martinbean's avatar

if the lifetime of the password is superior I don't have to let the user login until he change his password.

@xMarston Put it in the authentication handler, then. A password policy has nothing to do with requests or responses, that’s business logic.

xMarston's avatar

@martinbean this can be a stupid question but, where is the authentication handler? In which file?

Please or to participate in this conversation.