Home  >  Article  >  Backend Development  >  How to override Laravel's attempt method? Because the encryption method is customized.

How to override Laravel's attempt method? Because the encryption method is customized.

WBOY
WBOYOriginal
2016-08-04 09:20:171777browse

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

The password in this thing is to be encrypted using a method defined by yourself

Reply content:

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

The password in this thing is to be encrypted using a method defined by yourself

The document is indeed not written, but we can take a look at the source code

The implementation of the Auth method is in IlluminateAuthGuard inside

<code>    /**
     * 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);            
        
        // 看这里
        // If an 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 credentials.
     *
     * @param  mixed  $user
     * @param  array  $credentials
     * @return bool
     */
    protected function hasValidCredentials($user, $credentials)
    {
        // 执行认证驱动器的validCredentials方法
        return ! is_null($user) && $this->provider->validateCredentials($user, $credentials);
    }</code>

The default is to use eloquent as the authentication driver, so take a look at the implementation in IlluminateAuthEloquentUserProvider

<code>    public function validateCredentials(UserContract $user, array $credentials)
    {
        $plain = $credentials['password'];

        return $this->hasher->check($plain, $user->getAuthPassword());
    }</code>

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

<code>class TestUserProvider extend EloquentUserProvider
{
    public function validateCredentials(UserContract $user, array $credentials)
    {
        $plain = $credentials['password'];

        return md5($plain) == $user->getAuthPassword();
    }
}</code>

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

<code>Auth::setProvider(new TestUserProvider());</code>

It’s written in the document! Don't be too lazy to read the documentation. The questions you asked recently are all written in the documentation.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn