[英]How to make auto login after registration in laravel
我在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;
誰能告訴我什么是正確的問題
您可以嘗試通過他的$user_id
登錄用戶。 所以你的代碼將是:
$user_id = $this->user_model->addUser($user);
$post = array('password' => $pass_for_auth, 'email' => $email);
Auth::loginUsingId($user_id);
您創建了用戶,因此它返回user_id,使用user_id可以登錄用戶。
希望這個有效!
更多信息請訪問: https : //laravel.com/docs/5.2/authentication#other-authentication-methods
這將是進行用戶注冊的標准方法 - 您創建一個新的用戶模型實例並將其傳遞給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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.