laravel 如何在laravel中注册后自动登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36884306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make auto login after registration in laravel
提问by Harshit Sethi
I have encounterd a problem while regiserting user in laravel, $user
suppose to be array which contains all array element while auto login the following code results false. The password save in the database is hash::make('password')
.
我在 Laravel 中注册用户时遇到问题,$user
假设是包含所有数组元素的数组,而自动登录以下代码结果为false。保存在数据库中的密码是hash::make('password')
.
$user_id = $this->user_model->addUser($user);
$post = array('password' => $pass_for_auth, 'email' => $email);
var_dump(Auth::attempt($post,true));die;
Can any one tell me what is the correct problem
谁能告诉我什么是正确的问题
回答by Robin Dirksen
You can try to login the user through his $user_id
. So your code will be:
您可以尝试通过他的$user_id
. 所以你的代码将是:
$user_id = $this->user_model->addUser($user);
$post = array('password' => $pass_for_auth, 'email' => $email);
Auth::loginUsingId($user_id);
You created the user so it returns an user_id, with the user_id you can login the user.
您创建了用户,因此它返回一个 user_id,您可以使用 user_id 登录该用户。
Hope this works!
希望这有效!
More information at: https://laravel.com/docs/5.2/authentication#other-authentication-methods
更多信息请访问:https: //laravel.com/docs/5.2/authentication#other-authentication-methods
回答by Tadas Paplauskas
This would be the standard way to do user registration - you create a new user model instance and pass it to Auth::login() :
这将是进行用户注册的标准方法 - 您创建一个新的用户模型实例并将其传递给 Auth::login() :
// do not forget validation!
$this->validate($request, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
$data = $request->all();
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Auth::login($user);