Search code examples
phplaravelauthenticationcredentials

Login with expire date


I have an expire_date in my User model and I want to check when login. I write about active user in credentials method like this:

protected function credentials(Request $request) {
          return array_merge(
             $request->only($this->username(), 'password'),
             ['status' => 1]
          );
}

but, it add equal condition only but i want add >= date('Y-m-d') in this method.


Solution

  • As stated in the laravel documentation we can add custom conditions to Auth::attempt but only for equality checks. In this case the only option would be to override the login method in the LoginController (method is defined in the AuthenticatesUsers trait) and add custom check just before sending the login response.

    // for check expire date
    $user = (new User())->where('email', $request->email)->get();
    if ($user[0]->expire_date < date('Y-m-d')) {
       return $this->sendFailedLoginResponse($request);
    }