简体   繁体   English

Laravel还记得我,使用Auth :: attempt运行吗?

[英]Laravel Remember Me, running with Auth::attempt?

So I have this code: 所以我有这段代码:

public function postLogin() {
    // validate the info, create rules for the inputs
    $rules = array(
        'username'    => 'required', // make sure the email is an actual email
        'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
    );

    // run the validation rules on the inputs from the form
    $validator = Validator::make(Input::all(), $rules);

    // if the validator fails, redirect back to the form
    if ($validator->fails()) {
        return Redirect::route('login')
            ->withErrors($validator) // send back all errors to the login form
            ->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
    } else {

        $remember = (Input::has('remember')) ? true : false;
        // create our user data for the authentication
        $userdata = (array(
            'username'  => Input::get('username'),
            'password'  => Input::get('password')
        ), $remember);

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

            // validation successful!
            // redirect them to the secure section or whatever
            // return Redirect::to('secure');
            // for now we'll just echo success (even though echoing in a controller is bad)
            echo 'SUCCESS!';

        } else {

            // validation not successful, send back to form
            return Redirect::route('login')
             ->with('global', 'Incorrect username or password. Please try again.');
        }
    }
}

When it runs I get: syntax error, unexpected ',' . 当它运行时,我得到: syntax error, unexpected ',' Basically it isn't expecting the $remember to be passed there. 基本上,不希望$ remember在那里传递。 Where is it meant to be? 这意味着要在哪里? I've tried putting it here: Auth::attempt($userdate), $remember) { } but that didn't work either. 我尝试将其放在此处: Auth::attempt($userdate), $remember) { }但这也不起作用。 It had the same error. 它有相同的错误。 Not sure what's going on. 不知道发生了什么。 Any help would be appreciated. 任何帮助,将不胜感激。

You can use Auth::viaRemember() in your Authcontroller, to check if a user was already logged: 您可以在Authcontroller中使用Auth::viaRemember()来检查用户是否已经登录:

if (Auth::check() || Auth::viaRemember()) {...

And change your login-check as follows: 并如下更改登录检查:

//Assuming, the remember-input is a checkbox and its value is 'on'
if (Auth::attempt($userData, (Input::get('remember') == 'on') ? true : false)) {...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM