Home > Backend Development > PHP Tutorial > 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.

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-04 09:20:17
Original
1798 people have browsed it

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

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>
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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

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

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.

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
Composer cannot install laravel
From 1970-01-01 08:00:00
0
0
0
Laravel Space/laravel-backup cannot be installed
From 1970-01-01 08:00:00
0
0
0
Laravel 5.1 Login laravel comes with it No more
From 1970-01-01 08:00:00
0
0
0
Why thinkphp has better performance than laravel?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template