2016-08-23 186 views
2

我正在使用Laravel 5.0。我只想使用用户名(无密码)进行登录,因为我使用Single Sign On访问我的Laravel项目,所以如果用户成功登录单点登录,它将检查我的数据库是否登录用户的用户名已注册与否。
我一直在寻找这一整天,但我仍然找到了线索。

我已经得到了一个错误:如何在Laravel 5.0中使用Auth :: attempt无密码登录?

Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given

如果我使用下面的代码在我的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'); 
} 

我得到一个错误:

Undefined index: password

回答

0

根据文档:

the user will be retrieved by the value of the email column. If the user is found, the hashed password stored in the database will be compared with the hashed password value passed to the method via the array.

所以,密码必须以这种方法检查,因此它是一个必需的参数。

您可以登录以下一样:

$user = User::where('USER_NAME', '=', $_GET['UserName'])->first(); 

if (empty($user)) { 
    abort(404); 
} 

if(Auth::loginUsingId($user->id)){ 
    return redirect('Home'); 
} 
+0

我已经尝试过,并且在Application.php行901中有一个错误'NotFoundHttpException:' – hendraspt

+0

不确定。我试过我的代码,它工作正常。 – Sovon

0

您可以通过电子邮件只能手动authencate用户。像这样:

public function authenticate(Request $request) 
    { 
       $email=$request->get('email'); 
       if (Auth::attempt(['email' => $email)) 
       {  
        return redirect()->intended('/'); 
       } 
       else 
       { 
        return redirect('/login'); 
       } 

    } 
+0

是我需要添加此代码 – vinox

相关问题