By using multiple guards I want to attempt reset password using admin passwords broker. As in my case there are two guards web & admin. I want to use admins as a broker and use my custom Mailable class ResetPassword as an instance to send a reset link email while I am using Queues and Jobs.
Here is my auth.php configuration:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 1,
'throttle' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 1,
'throttle' => 60,
],
],
The ResetPasswordController logic:
public function sendResetLink(SendResetLinkRequest $request)
{
$adminDetails = Admin::where('email', $request->email)->first();
$email = $request->email;
if ($adminDetails) {
$token = Str::random(64);
DB::table('password_resets')->insert([
'email' => $email,
'token' => $token,
'created_at' => now(),
]);
$link = route('admin.create_new_password', encrypt($token) . '?e=' . base64_encode($email));
dispatch(new ResetPasswordMailJob($adminDetails, $link));
session(['true' => 1]);
return back();
} else {
return back()->withErrors(['email' => 'The email is invalid']);
}
}
The ResetPasswordMailJob:
class ResetPasswordMailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $details, $link;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($details, $link)
{
$this->details = $details;
$this->link = $link;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// Password::broker('admin')->sendResetLink(['email' => $this->details->email], function () {
// new ResetPassword($this->details->name, $this->link);
// });
Mail::to($this->details->email)->send(new ResetPassword($this->details->name, $this->link));
}
The mailable class ResetPassword:
class ResetPassword extends Mailable
{
use Queueable, SerializesModels;
public $name, $link;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($name, $link)
{
$this->name = $name;
$this->link = $link;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Reset Password')->view('emails.reset-password');
}