Get a password reset token

Published 2 years ago by josh18

I'm trying to access the create function inside of Illuminate\Auth\Passwords\DatabaseTokenRepository but I'm not sure of the best way to do this.

I'm new to Laravel so please forgive me if there is an obvious answer to this, I have done a lot of research but I just can't seem to find anything. The closest I've come is to create a facade in Illuminate/Support/Facades which looks like:

<?php

namespace Illuminate\Support\Facades;

/**
 * @see \Illuminate\Auth\Passwords\DatabaseTokenRepository
 */
class PasswordToken extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'auth.password.tokens';
    }
}

and then adding 'PasswordToken' => Illuminate\Support\Facades\PasswordToken::class to config/app.php. This then lets me do $token = PasswordToken::create($user);

Am I even close to doing this the right way?

Best Answer (As Selected By josh18)
phildawson

@josh18 That seems fine to me if you want to provide additional static access to it. Like all bindings you could alway use it directly if needed.

app('auth.password.tokens')->create($user);

If you're intrigued as to where the binding and setup of the DatabaseTokenRepository takes place. Illuminate\Auth\Passwords\PasswordResetServiceProvider

If you use the out the box PasswordController with ResetsPasswords trait, what the PasswordBroker does is actually type hint the interface TokenRepositoryInterface to new this up. As you can see in Illuminate\Foundation\Application it's an alias for the same binding.

public function registerCoreContainerAliases()
{
...
'auth.password.tokens' => 'Illuminate\Auth\Passwords\TokenRepositoryInterface',

I'm not anti Facade but I would use the alias personally.

use Illuminate\Auth\Passwords\TokenRepositoryInterface;

Route::get('/reset', function (TokenRepositoryInterface $tokens) {

    $user = ...
    $tokens->create($user);
});
phildawson

@josh18 That seems fine to me if you want to provide additional static access to it. Like all bindings you could alway use it directly if needed.

app('auth.password.tokens')->create($user);

If you're intrigued as to where the binding and setup of the DatabaseTokenRepository takes place. Illuminate\Auth\Passwords\PasswordResetServiceProvider

If you use the out the box PasswordController with ResetsPasswords trait, what the PasswordBroker does is actually type hint the interface TokenRepositoryInterface to new this up. As you can see in Illuminate\Foundation\Application it's an alias for the same binding.

public function registerCoreContainerAliases()
{
...
'auth.password.tokens' => 'Illuminate\Auth\Passwords\TokenRepositoryInterface',

I'm not anti Facade but I would use the alias personally.

use Illuminate\Auth\Passwords\TokenRepositoryInterface;

Route::get('/reset', function (TokenRepositoryInterface $tokens) {

    $user = ...
    $tokens->create($user);
});
josh18

Ugh.... app('auth.password.tokens')->create($user); is exactly what I need. Thanks for your help and sorry for the stupid question.

I'm sure it's somewhere obvious but you can you point me to the right direction in the docs (5.1) where it talks about this? I've browsed the docs quite a lot and the only similar things I have seen are app()->environment(); (installation page) and the list on the facade page (hence why I thought they were only accessible via facades).

For future reference if I were to make a facade how would I go about implementing it? I tried moving my new facade from Illuminate/Support/Facades to app/Facades and changing the alias in app.php but I couldn't seem to get it working for some reason.

Francismori7
phildawson

@josh18 The app() function returns the Container instance or resolves if passed a string of what to make. The Container implements ArrayAccess too so you can also use it like app()['auth.password.tokens'].

For future reference if I were to make a facade how would I go about implementing it?

Re the Facade the 5.0 doc is actually better at explaining it with step by step.

    1. create the class
    1. create the binding
    1. create the Facade which references that binding.

http://laravel.com/docs/5.0/facades#creating-facades

robgeorgeuk

For Laravel 5.3 it's now:

app('auth.password.broker')->createToken($user);
Corys8646

Sorry to dig up this old thread, in a situation where I need to manually send a password reset token to users.

If I generate the token using app('auth.password.broker')->createToken($user);

And insert it into an email, I receive the email just as you'd expect. But when trying to reset the password, I get an Invalid Token. I have confirmed that the token is being inserted into the database.

If I attempt to reset my password the traditional way /password/reset, everything works great.

Any ideas? On Laravel 5.5

Corys8646

Odd..I have a method on my user class to generate the token, I was calling that from my mail view. If I move that token generation to the Mailable item and inject the token into the view it works fine...Guessing somewhere the generateToken method was being called twice. Overriding the initial token value..

Please sign in or create an account to participate in this conversation.