Authentication

Authentication Configuration

The authentication configuration is set into the config/auth.json file. Here is an example of authentication configuration:

{
  "model": "User",
  "username_column": "username",
  "password_column": "password",
  "is_admin_column": false
}

The user model that will be used for authentication. The username and password checks will be done from this model.

Login

You have an auth() helper that allows you to perform various operations related to authentication.

There's two functions for login user. The first is:

auth()->attempt([$credentials]);

$credentials is an array that contains values ​​of type key => value, this array must contain the username and password required for the connection. Example:

$credentials = [
    'username' => 'johndoe@example.com',
    'password' => 'password1234'
];

auth()->attempt($credentials);

The password does not need to be encrypted, Atom will encrypt it automatically before login.

You also have the login() function. Unlike the attempt() function, the login() function takes as argument a model of the type defined in the configuration. So, in example you have this:

$user = User::find('id', 1);

auth()->login($user);

Both functions will return true if the login was successful and false otherwise.

Authentication Check

You will surely need to check if a user is connected. You can do this with the check() function. the function will return true if there is a logged user and false otherwise.

if (auth()->check()) {
    echo "You're logged in !";
} else {
    echo "You're not logged in !";
}

Dernière mise à jour

Cet article vous a-t-il été utile ?