我已经在我现有的项目中使用用户名或电子邮件实施登录并且工作正常。我想将其扩展为使用用户名、电子邮件或电话登录。我希望用户使用用户名、电子邮件或电话号码和密码登录。
这是我的代码 App\Http\Controllers\Auth\LoginController
<?php
namespace App\Http\Controllers\Auth;
use Socialite;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Redirect the user to the GitHub authentication page.
*
* @return \Illuminate\Http\Response
*/
public function redirectToProvider($social)
{
return Socialite::driver($social)->redirect();
}
/**
* Obtain the user information from GitHub.
*
* @return \Illuminate\Http\Response
*/
public function handleProviderCallback($social)
{
$user = Socialite::driver($social)->user();
// $user->token;
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
$field = filter_var($request->get($this->username()), FILTER_VALIDATE_EMAIL)
? $this->username()
: 'username';
return [
$field => $request->get($this->username()),
'password' => $request->password,
];
}
}
应用\Http\Controllers\Auth\RegisterController
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'username' => 'required|string|max:20|unique:users',
'phone' => 'required|string|max:20|unique:users',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'gender' => 'required|bool',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
if($data['gender'])
{
$avatar = 'default/avatars/male.png';
}
else
{
$avatar = 'default/avatars/female.png';
}
return User::create([
'name' => $data['name'],
'gender' => $data['gender'],
'username' => $data['username'],
'phone' => $data['phone'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'slug' => str_slug($data['username']),
'avatar' => $avatar,
]);
}
}
注册工作正常,我无法将用户电话存储到用户表中。我已将电子邮件的输入类型更改为 views/auth/login.php 上的文本,并且我能够使用用户名或电子邮件登录。
当我在 LoginController 中将用户名更改为电话时,我无法使用电话登录,因为它说方法 [电话] 在 [App\Http\Controllers\Auth\LoginController] 上不存在。
当我添加一个方法
/**
* Create a new controller instance.
*
* @return void
*/
public function phone()
{
return 'phone';
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
$field = filter_var($request->get($this->phone()), FILTER_VALIDATE_EMAIL)
? $this->phone()
: 'phone';
return [
$field => $request->get($this->phone()),
'password' => $request->password,
];
}
它不允许我用手机登录。错误 这些凭据与我们的记录不符。也试过
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
if(is_numeric($request->get('email'))){
return ['phone'=>$request->get('email'),'password'=>$request->get('password')];
}
return $request->only($this->username(), 'password');
}
我现在可以使用电话或电子邮件登录,但不能使用用户名如何实现使用用户名、电子邮件或电话登录
原文由 user6780526 发布,翻译遵循 CC BY-SA 4.0 许可协议
此代码有效。希望它能帮助别人。
如果是数字,则使用数字登录。如果是电子邮件,请使用电子邮件地址登录。否则尝试用户名
最终的 LoginController 看起来像这样