Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel send password reset link to a separate authentication guard

In Laravel 5.4, is there a way to send the password reset link to a separate authentication guard instead of the default one. I am using the default PasswordResetController which does the job in this way

 public function company(Request $request)
    {
        $this->validate(request(), [
            'email' => 'required|email',
        ]);
        $response = Password::sendResetLink([
            'email' => $request->email
        ]);

        //overridden if condition 
        if($response == "passwords.sent")
        {
        return back()->with('message','Password reset link has been sent, please check your email');
        }

        return back()->with('message', 'No such email address in our records, try again');
    } 

The sendResetLink() method checks and sends the reset link to the default guard, but I am defining a new guard in auth.php called web

 'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'companies',
        ],

sendResetLink method is like this

  public function sendResetLink(array $credentials)
    {
        // First we will check to see if we found a user at the given credentials and
        // if we did not we will redirect back to this current URI with a piece of
        // "flash" data in the session to indicate to the developers the errors.
        $user = $this->getUser($credentials);

        if (is_null($user)) {
            return static::INVALID_USER;
        }

Any way for this method to check in a separate table or use a separate auth guard?

like image 530
Cowgirl Avatar asked Jan 05 '18 19:01

Cowgirl


2 Answers

In your auth.php configuration file, you may configure multiple "guards", which may be used to define authentication behavior for multiple user tables. You can customize the included ResetPasswordController to use the guard of your choice by overriding the guard method on the controller. This method should return a guard instance:

protected function guard()
{
    return Auth::guard('guard-name');
}

The magic lies, in using the broker - the PasswordBroker for your custom guard. But, you make sure to set up multiple password brokers in your auth.php configuration file.

protected function broker()
{

    return Password::broker('name');
}
like image 70
Dhiraj Avatar answered Sep 28 '22 08:09

Dhiraj


Here is my method of how to send password reset link to a guard which is a part of multi-auth system.

I am going to assume you have properly set your new guard in config/auth.php which may look like below code:

I use the word admin for the new guard's name for better understanding.

'guards' => [

    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
]

'providers' => [

    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],
]


 'passwords' => [

    'admins' => [
        'provider' => 'admins',
        'table' => 'password_resets',
        'expire' => 15,
    ],
]

You will have to create new controllers(AdminForgotPasswordController and AdminResetPasswordController) for your new guard

They both use the Password facade and AdminResetPasswordController uses Auth facade as well.

Modify the construct function according to your new guard.
So add this to both controllers, because we have specific type of guest users.

public function __construct()
{
    $this->middleware('guest:admin');
}

Now we need to tell AdminResetPasswordController to use the proper guard for authenticating.

So add this method to the controller

protected function guard()
{
  return Auth::guard('admin');
}

Now add this piece of code to both controllers.

protected function broker()
{
  return Password::broker('admins'); //set password broker name according to guard which you have set in config/auth.php
}

Notice: It's not the only step for implementing password reset for guard you will have to take other steps like creating new routes, notifications, forms and corresponding views.

like image 45
Mahdi Younesi Avatar answered Sep 28 '22 07:09

Mahdi Younesi