Thursday 19 October 2017

To create a custom token password reset in Laravel 5.5 (Or Lumen)

Please use the below passwordResetToken() function to get the token for password reset for custom use.

namespace App\Traits;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Password;

trait ResetPasswordTrait 
{
    ...
    ...
    ...
    ...

    /**
     * Get the token for password reset and update the token in the database table.
     *
     * @return string|null
     */
    public function passwordResetToken($user)
    {
        $this->broker = 'users';

        $broker = $this->getBroker();

        return Password::broker($broker)->createToken($user);
    }

    /**
     * Get the broker to be used during password reset.
     *
     * @return string|null
     */
    public function getBroker()
    {
        return property_exists($this, 'broker') ? $this->broker : null;
    }

}


// Reuse in the class
use App\Traits\ResetPasswordTrait;

class NewUserAccount 
{

    use ResetPasswordTrait;

    // Sample function to print the password reset token
    public function printPasswordResetToken(Request $request)
    {
        $user = User::find( $request->input('id') );
        $passwordResetToken = $this->passwordResetToken($user);
        print_r($passwordResetToken);
    }

}

No comments:

Post a Comment

Please post any queries and comments here.