Back

The importance of Attempt function, how to create a manual login in Laravel?

Laravel

The importance of Attempt function, how to create a manual login in Laravel?

In this article, we shed light on how to create a manual login process in Laravel. After we have finished creating a new user, we should start by allowing users to perform logins to the site.

Here the importance of Attempt function appears, which we will explain how to use it in this article.

Requirements for creating Attempt function

First, in the App/controllers/AuthController.php file, we will write the following code:

public function postLogin(Request $r){    if(auth()->attempt(['email' => $r->email , 'password' => $r->password] , true)){            return redirect()->route('admin.home');    }else{           return back()->withErrors('Login info not correct');     } }

 

The importance of Attempt function appears, as it requires an email in addition to the password in order to log in, in the database, the name of the fields should be email, password.

As for the second section that the function contains true, its goal is to save login.

The default value is False, meaning that once the session ends, the user is logged out.

If true, the user will remain without logging out until manually logged out.

Password encryption to creat a new user

It is also noted in the importance of Attempt function, that the value of the password is sent without being encrypted as in the past regarding the process of creating a new user, since the attempt function encrypts the password automatically.

It is also necessary that the password field be named password, changing the name to passcode for example or renaming it to any other name can only be done in the case of more in-depth operations of Laravel and contains a high level of expertise and excellence.

Regarding outputs of this function, if the information about Attempt function is found to be correct, we will return true, and vice versa.

Specify conditions in Attempt function

It is also possible to specify several other conditions in Attempt function, more different conditions can be used, this is an example of this code:

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {    // The user is active, not suspended, and exists.}

 

One of the important notes regarding Attempt function, is that we have used Facade Auth, therefore we have to call it at the beginning of the page by using this code: use Auth; .

It is noted in the code, in the database, this condition compares between three information, through the results, the login is done at the beginning, the desired fields can then be added, but the name of the password field should be password!

Create a manual checkout in Laravel

This step is one of the simplest steps that take place through Laravel, you do not need much talk to explain it as this code is used.

// We Must Include The Auth Facade Here

Auth::logout();

// or this

auth()->logout(); // The auth() helper method is global , we done have to include Auth Facade if we use the helper method