What is the difference between "login" and "try" method in Auth

I am learning Laravel 5.4 and customizing and building my original Auth functions.

Below is my authentication method.

public function authenticate(Request $request)
{
    $remember_me = (Input::has('remember')) ? true : false;
    Auth::guard('web');
    $this->validateLogin($request);
    $credentials = array(
        'username' => trim($request->input('username')),
        'password' => trim($request->input('password'))
    );
    if(Auth::attempt($credentials, $remember_me)){
        $user = Auth::guard('web')->user();
        Auth::guard('web')->login($user, $remember_me);
        return redirect()->route('mypage');
    }
    return redirect()->back();
}

      

I have a question about the $ remember_me argument part about the try and login methods noted above.

What is the difference between them?

When I saw the documentation, he said that similarly, if you want to make a "remember me" token, you can set a second boolean argument about all of them.

+3


source to share


1 answer


attempt($credentials, $remember_me)

will try to log in if the login credentials are correct. If it is not, then the user is not logged in. This method returns boolean so you can test for success.

login($user_id, $remember_me)

will register the user without checking the credentials.

Remember what I am asking if the username should persist across browser sessions without having to re-authorize.



In your example, I see your call login(...)

in yours attempt(...)

. It is not necessary. You can remove the line login(...)

.

Example:

if(Auth::attempt($credentials, $remember_me)){
    return redirect()->route('mypage');
}

      

+5


source







All Articles