[英]How to login without password using Auth::attempt in Laravel 5.0?
我正在使用 Laravel 5.0。 我只想使用用戶名(無密碼)登錄,因為我使用單點登錄訪問我的 Laravel 項目,所以如果用戶成功登錄單點登錄,它將檢查我的數據庫是否登錄用戶的用戶名注冊與否。
我整天都在尋找這個,但我仍然找到了線索。
我有一個錯誤:
傳遞給 Illuminate\Auth\Guard::login() 的參數 1 必須實現接口 Illuminate\Contracts\Auth\Authenticatable,給出 null
如果我在我的 LoginController 中使用下面的代碼:
$user = User::where('USER_NAME', '=', $_GET['UserName'])->first();
if(Auth::login($user)->USER_ID == $_GET['UserName']){
return redirect('Home');
}
如果我在我的 LoginController 中使用下面的代碼:
if(Auth::attempt(['USER_ID' => $_GET['UserName']])){
return redirect('Home');
}
我有一個錯誤:
未定義索引:密碼
根據文件:
將通過電子郵件列的值檢索用戶。 如果找到用戶,則將存儲在數據庫中的散列密碼與通過陣列傳遞給方法的散列密碼值進行比較。
因此,必須在此方法中檢查密碼,因此它是必需參數。
您可以登錄如下:
$user = User::where('USER_NAME', '=', $_GET['UserName'])->first();
if (empty($user)) {
abort(404);
}
if(Auth::loginUsingId($user->id)){
return redirect('Home');
}
您只能通過電子郵件手動驗證用戶。 像這樣:
public function authenticate(Request $request)
{
$email=$request->get('email');
if (Auth::attempt(['email' => $email))
{
return redirect()->intended('/');
}
else
{
return redirect('/login');
}
}
你可以這樣試試
public function login(Request $request) {
$email = $request->email;
$user = User::where('email', $email)->first();
if ($user) {
Auth::login($user);
return redirect()->route('dashboard');
}
else {
return redirect()->back();
}
}
希望它有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.