Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do "remember me" login option in laravel?

so what i am working on is when i check the remember me checkbox the user when logout the form (username and password) remember the user data like that : enter image description here

so this is my code it not works :(

// create our user data for the authentication
        $userdata = array(
                'email'     => Input::get('email'),
                'password'  => Input::get('password')
        );

        // attempt to do the login
        if (Auth::attempt($userdata, true)) {

            Session::flash('message', array('body'=>trans('login-signup.welcome'), 'type'=>'success'));
            return Redirect::to('/');

    }
like image 320
Mohammadov Avatar asked Dec 20 '22 15:12

Mohammadov


2 Answers

Use Cookies

cookie, is a small piece of data sent from a website and stored in a user's web browser while the user is browsing that website. Every time the user loads the website, the browser sends the cookie back to the server to notify the website of the user's previous activity

To create:

$response->withCookie(Cookie::make('name', 'value', $minutes));

To retrieve

$value = Cookie::get('name');

Your question is not to remember the user login.. The question is how to fill the inputs based on saved auth information. You can do that if you print the authentication values in the input value attribute while loading the page.

larval Cookies Docs

Also Laravel has it's own implementation of "Remember Me"

if (Auth::attempt(array('email' => $email, 'password' => $password), true))
{
// The user is being remembered...
}

if (Auth::viaRemember())
{
//
}

More information about "Authenticating A User And "Remembering" Them"

like image 152
Othman Avatar answered Dec 30 '22 12:12

Othman


public function validate() 
{
    // set the remember me cookie if the user check the box
    $remember = (Input::has('remember')) ? true : false;

    // attempt to do the login
    $auth = Auth::attempt(
        [
            'username'  => strtolower(Input::get('username')),
            'password'  => Input::get('password')    
        ], $remember
    );
    if ($ auth) {
        return Redirect::to('home');
    } else {
        // validation not successful, send back to form 
        return Redirect::to('/')
            ->with Input(Input::except('password'))
            ->with('flash_notice', 'Your username/password combination was incorrect.');
    }

}
like image 31
Rahul Tathod Avatar answered Dec 30 '22 12:12

Rahul Tathod