幼虫5 |使用多个标识符登录

时间:2016-03-27 11:47:50

标签: laravel authentication laravel-5.2

我想让用户通过提供他/她的

登录
  • 用户名
  • 或电子邮件ID(少数可用)
  • 或电话号码

以及密码,类似于它在Facebook和LinkedIn中的工作方式。

使用Laravel 5.2默认身份验证实现此目的的最佳/优雅方式是什么?

1 个答案:

答案 0 :(得分:1)

根据我的知识,没有第三方解决方案可用。 这是我的工作示例,我是如何做到的...这个postLogin一些示例代码。

$credentials = $this->getCredentials($request);

$credentials_username   =   ['username'=>$credentials['username'], 'password'=>$credentials['password']];
$credentials_email      =   ['email'=>$credentials['username'], 'password'=>$credentials['password']];
$credentials_phone      =   ['phone'=>$credentials['username'], 'password'=>$credentials['password']];

//adedd custom, we also want to check by username too
if (Auth::attempt($credentials_email, $request->has('remember'))) {
    return $this->handleUserWasAuthenticated($request, $throttles);

//also check for the username if exists then login to the system    
}elseif (Auth::attempt($credentials_username, $request->has('remember'))) {
    return $this->handleUserWasAuthenticated($request, $throttles);

//also check for phone no     
}elseif (Auth::attempt($credentials_phone, $request->has('remember'))) {
    return $this->handleUserWasAuthenticated($request, $throttles);
}