Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] expose token methods to user in password broker #13054

Merged
merged 1 commit into from
Apr 7, 2016
Merged

[5.2] expose token methods to user in password broker #13054

merged 1 commit into from
Apr 7, 2016

Conversation

browner12
Copy link
Contributor

this PR references internals issue 12. please read my initial comment on that page for my reason behind submitting this PR.

by giving the user access to the token repository, they now have full control over resetting passwords. the user is given free reign over validating the input, sending emails, and other things they would like to do.

here is how i would use it in a controller or service class.

class PasswordService
{
    public function __construct(\Illuminate\Contracts\Auth\PasswordBroker $broker)
    {
        $this->broker = $broker;
    }

    public function request(array $input)
    {
            //retrieve user
            $user = $this->broker->getUser($input);

            //no user found with email
            if (is_null($user)) {
                throw new Exception('User not found.');
            }

            //get token
            $token = $this->broker->generateToken($user);

            //send email
        }
    }

    public function reset(array $input)
    {
            //retrieve user
            $user = $this->broker->getUser($input);

            //no user found with email
            if (is_null($user)) {
                throw new Exception('User not found.');
            }

            //reset password
            if($this->broker->validateToken($user, $input['token'])){

                $user->password = bcrypt($password);
                $user->save();

                $this->broker->deleteToken($input['token'];
            } 

            throw new Exception('token was invalid');
    }
}

@GrahamCampbell GrahamCampbell changed the title expose token methods to user in password broker [5.2] expose token methods to user in password broker Apr 6, 2016
@taylorotwell
Copy link
Member

Can all this be done by calling ->getRepository() and then calling whatever methods you want? You're just wanting these methods for convenience?

@taylorotwell taylorotwell merged commit bea5690 into laravel:5.2 Apr 7, 2016
@browner12
Copy link
Contributor Author

i guess technically yes. this makes the code feel a little more descriptive and easy to follow to me, and hopefully others as well.

thanks.

@browner12 browner12 deleted the password-resets branch April 8, 2016 20:01
@tillkruss
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants