How to rewrite Laravel's attempt method? Because the encryption method is custom.

Source: Internet
Author: User
Auth::attempt(array('username' => $username, 'password' => $password),false)

Password in this thing is trying to encrypt it in a way that you define.

Reply content:

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

Password in this thing is trying to encrypt it in a way that you define.

The document did not write, but we can look at the source code

The implementation of the Auth method is Illuminate\Auth\Guard inside

    /** * 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->fireattempteven        T ($credentials, $remember, $login);                            $this->lastattempted = $user = $this->provider->retrievebycredentials ($credentials);  See here//If An implementation of UserInterface is returned, we ' ll ask the provider//to validate the user Against the given credentials, and if they is in//fact valid we ' ll log the users into the application and retur        n True. if ($this->hasvalidcredentials ($user, $credentials)) {if ($login) {$this->login ($user, $            Remember);        } return true;    } return false; }/** * Determine if the user matches the Credentials. * * @param mixed $user * @param array $credentials * @return bool */protected function Hasvalidcre Dentials ($user, $credentials) {//Validcredentials method to perform authentication drive return! Is_null ($user) && $this-&G    T;provider->validatecredentials ($user, $credentials); }

The default is to use eloquent as the authentication drive, so look Illuminate\Auth\EloquentUserProvider inside the implementation

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

So if you want to change the logic of validation, you can inherit the original drive and rewrite the logic inside the 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 Appserviceprovider boot () inside

Auth::setProvider(new TestUserProvider());

It's written in the document! Don't be lazy and don't read the documentation, the questions you've recently raised are written in the documentation.

  • 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.