How can I rewrite Laravel's attempt method? Because the encryption method is customized.

Source: Internet
Author: User
The password in {code...} is intended to be encrypted in a defined method.
Auth::attempt(array('username' => $username, 'password' => $password),false)

In this case, the password is intended to be encrypted using a defined method.

Reply content:
Auth::attempt(array('username' => $username, 'password' => $password),false)

In this case, the password is intended to be encrypted using a defined method.

The document is not written, but we can look at the source code.

The Auth method is implemented inIlluminate\Auth\GuardInside

/*** Attempt to authenticate a user using the given credentials. ** @ param array $ credentials * @ param bool $ remember * @ param bool $ login * @ return bool */public function attempt (array $ credentials = [], $ remember = false, $ login = true) {$ this-> fireAttemptEvent ($ credentials, $ remember, $ login ); $ this-> lastAttempted = $ user = $ this-> provider-> retrieveByCredentials ($ credentials); // click here. // If N implementation of UserInterface was returned, we'll ask the provider // to validate the user against the given credentials, and if they are in // fact valid we'll log the users into the application and return true. if ($ this-> hasValidCredentials ($ user, $ credentials) {if ($ login) {$ this-> login ($ user, $ remember);} return true ;} return false;}/*** Determine if the user matches the credentia Ls. ** @ param mixed $ user * @ param array $ credentials * @ return bool */protected function hasValidCredentials ($ user, $ credentials) {// execute the validCredentials method return for the authentication drive! Is_null ($ user) & $ this-> provider-> validateCredentials ($ user, $ credentials );}

By default, eloquent is used as the authentication drive.Illuminate\Auth\EloquentUserProviderImplementation

    public function validateCredentials(UserContract $user, array $credentials)    {        $plain = $credentials['password'];        return $this->hasher->check($plain, $user->getAuthPassword());    }

So if you want to change the verification logic, you can inherit the original drive and then rewrite the logic in validateCredentials.

class TestUserProvider extend EloquentUserProvider{    public function validateCredentials(UserContract $user, array $credentials)    {        $plain = $credentials['password'];        return md5($plain) == $user->getAuthPassword();    }}

Finally, set the drive. it is recommended to load the boot () of AppServiceProvider.

Auth::setProvider(new TestUserProvider());

This document has been written! Don't be lazy and don't read the document. your most recent questions are written in the document.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.