Home > Backend Development > PHP Tutorial > Laravel framework user login authentication

Laravel framework user login authentication

巴扎黑
Release: 2023-03-16 08:22:02
Original
1916 people have browsed it

This article mainly introduces the implementation method of user login authentication of Laravel framework. It analyzes the principles, implementation methods and related precautions of user login authentication of Laravel framework in the form of examples. Friends in need can refer to it

This article describes the implementation method of user login authentication in the Laravel framework through examples. Share it with everyone for your reference, the details are as follows:

In laravel, there is the following code to detect whether the user is logged in:

if ( !Auth::guest() )
{
  return Redirect::to('/dashboard');
}
Copy after login

How is Auth::guest called? Woolen cloth?

Laravel uses the Facade mode. The relevant facade classes are defined in the laravel/framework/src/Illuminate/Support/Facades folder. Take a look at the definition of the Auth class:

class Auth extends Facade {
  /**
   * Get the registered name of the component.
   *
   * @return string
   */
  protected static function getFacadeAccessor() { return 'auth'; }
}
Copy after login

In the laravel framework, Facade mode uses reflection. The related methods actually call the methods in app['auth']. When was app['auth'] created?

AuthServiceProvider:: The register method will register:

$this->app->bindShared('auth', function($app)
{
  // Once the authentication service has actually been requested by the developer
  // we will set a variable in the application indicating such. This helps us
  // know that we need to set any queued cookies in the after event later.
  $app['auth.loaded'] = true;
  return new AuthManager($app);
});
Copy after login

Then why will it be transferred to where in the end? Look at the stack:

Illuminate\Support\Facades\Auth::guest()
Illuminate\Support\Facades\Facade::__callStatic
Illuminate\Auth\AuthManager->guest()
Illuminate\Support\Manager->__call
public function __call($method, $parameters)
{
    return call_user_func_array(array($this->driver(), $method), $parameters);
}
Copy after login

Look at the driver code:

public function driver($driver = null)
{
    $driver = $driver ?: $this->getDefaultDriver();
    // If the given driver has not been created before, we will create the instances
    // here and cache it so we can return it next time very quickly. If there is
    // already a driver created by this name, we'll just return that instance.
    if ( ! isset($this->drivers[$driver]))
    {
      $this->drivers[$driver] = $this->createDriver($driver);
    }
    return $this->drivers[$driver];
}
Copy after login

No Calling the getDefaultDrive method

/**
* Get the default authentication driver name.
*
* @return string
*/
public function getDefaultDriver()
{
    return $this->app['config']['auth.driver'];
}
Copy after login

ultimately calls the driver configured in the configuration file. If

'driver' => 'eloquent'
Copy after login

is configured, the

public function createEloquentDriver()
{
    $provider = $this->createEloquentProvider();
    return new Guard($provider, $this->app['session.store']);
}
Copy after login

is called. So Auth::guestThe final call is Guard::guestmethod

The logic here first gets the user information from the session. The strange thing is that only the user information is saved in the session. It is the user ID, and then use this ID to get the user information from the database

public function user()
{
    if ($this->loggedOut) return;
    // If we have already retrieved the user for the current request we can just
    // return it back immediately. We do not want to pull the user data every
    // request into the method because that would tremendously slow an app.
    if ( ! is_null($this->user))
    {
      return $this->user;
    }
    $id = $this->session->get($this->getName());
    // First we will try to load the user using the identifier in the session if
    // one exists. Otherwise we will check for a "remember me" cookie in this
    // request, and if one exists, attempt to retrieve the user using that.
    $user = null;
    if ( ! is_null($id))
    {
      //provider为EloquentUserProvider
     $user = $this->provider->retrieveByID($id);
    }
    // If the user is null, but we decrypt a "recaller" cookie we can attempt to
    // pull the user data on that cookie which serves as a remember cookie on
    // the application. Once we have a user we can return it to the caller.
    $recaller = $this->getRecaller();
    if (is_null($user) && ! is_null($recaller))
    {
      $user = $this->getUserByRecaller($recaller);
    }
    return $this->user = $user;
}
Copy after login

The above is the detailed content of Laravel framework user login authentication. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template