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

skunkbad's avatar

Create password reset token manually?

I'd like to create the password reset token manually, meaning I'd like to insert it into the database, and put it into a link in an email manually. I'm choosing to do this because I use a external mail queue. So I thought I might just be able to make the token like this:

$response = Password::broker($broker);

But it doesn't create the row in the password_resets table, and I'm not able to get the token from what I see in $response. I need some help with this if anyone can help.

0 likes
8 replies
skunkbad's avatar

I'm probably making a mistake, or possibly not taking something into account, but I just put this in my auth controller:

public function sendResetLinkEmail(Request $request)
{
    $validator = Validator::make($request->all(), [
        'email' => 'required|email'
    ]);

    if( ! $validator->fails() )
    {
        if( $user = User::where('email', $request->input('email') )->first() )
        {
            $token = str_random(64);

            DB::table(config('auth.passwords.users.table'))->insert([
                'email' => $user->email, 
                'token' => $token
            ]);

            Mailq::q([
                'to'      => $user->email,
                'subject' => 'Your Password Reset Link',
                'view'    => config('auth.passwords.users.email'),
                'view_data' => [
                    'token' => $token, 
                    'user'  => $user
                ]
            ]);

            return redirect()->back()->with('status', trans(Password::RESET_LINK_SENT));
        }
    }
    
    return redirect()->back()->withErrors(['email' => trans(Password::INVALID_USER)]);
}

But, since it's clear what I want to do, maybe somebody can chime in with the proper way to achieve such a thing.

pakogn's avatar

Hi!

I know this post is kind of old but for Laravel 5.5 and I think more recent versions We can achieve that with this:

$token = app(Illuminate\Auth\Passwords\PasswordBroker::class)->createToken($user);

Greetings!

5 likes
poboy's avatar

@PAKOGN - I had an error with Illuminate\Auth\Passwords\PasswordBroker::class but this worked for me.

app('auth.password.broker')->createToken($user)
7 likes
pakogn's avatar

@POBOY - May be the "use", or the laravel version, glad your solution worked!

msyadav's avatar

Its is giving me below error "Argument 1 passed to Illuminate\Auth\Passwords\PasswordBroker::createToken() must implement interface Illuminate\Contracts\Auth\CanResetPassword, null given, called in C:\wamp64\www\bet\app\Http\Controllers\Auth\LoginController.php on line 75 ◀"

rawilk's avatar

@mysadav - It is telling you your argument for the user is null. Make sure you actually have an instance of a user and that your user model implements Illuminate\Contracts\Auth\CanResetPassword before calling the createToken() method

1 like

Please or to participate in this conversation.