使用电子邮件和用户名(相同字段)进行身份验证 (laravel API )
Authentication with both email and username (same field) (laravel API )
我正在建设 laravel API 我的网站。我正在使用 laravel 5.3
我正在使用 API 构建移动应用程序。
在登录部分,有两个字段:
- 输入电子邮件或用户名
- 输入密码
在我的数据库中,电子邮件和用户名是不同的列。
函数如下:
public function authenticate()
{
$credentials=request()->only('username','password');
try {
$token=JWTAuth::attempt($credentials);
if(!$token){
return response()->json(['error'=>'credentials wrong'],401);
}
}
catch(JWTException $e) {
return response()->json(['error'=>'something_went_wrong'],500);
}
return response()->json(['token'=>$token],200);
}
这里只使用用户名,我想同时使用用户名和密码
如果需要任何其他信息,请询问。
ty:)
您可以使用FILTER_VALIDATE_EMAIL函数
像这样:-
$login_type = filter_var( $data['email'], FILTER_VALIDATE_EMAIL ) ? 'email' : 'username';
然后像这样传入 auth attempt 函数:-
Auth::attempt([$login_type => $data['email'], 'password' => $data['password']])
你的情况:-
According to your Html you have used two input
<input type="text"required autocomplete="off" name="loginEmail" id="inputname"/>
<input type="password"required autocomplete="off" name="loginPassword" id="inputpassword"/>
And your function is:-
public function authenticate(Request $request)
{
$data = $request->all();
$login_type = filter_var( $data['loginEmail'], FILTER_VALIDATE_EMAIL ) ? 'email' : 'username';
//$credentials=request()->only('username','password');
try {
$token=JWTAuth::attempt([$login_type => $data['loginEmail'], 'password' => $data['loginPassword']]);
if(!$token){
return response()->json(['error'=>'credentials wrong'],401);
}
}
catch(JWTException $e) {
return response()->json(['error'=>'something_went_wrong'],500);
}
return response()->json(['token'=>$token],200);
}
希望对您有所帮助!
在邮递员中,我在名为 'username' 的正文中使用了密钥,所以在这里代替 loginEmail ,用户名将会出现并且它会正常工作!泰
在您的 AuthController
中覆盖此方法
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function getCredentials(Request $request)
{
$login = $request->get('loginEmail');
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
return [
$field => $login,
'password' => $request->get('password'),
];
}
我正在建设 laravel API 我的网站。我正在使用 laravel 5.3
我正在使用 API 构建移动应用程序。
在登录部分,有两个字段:
- 输入电子邮件或用户名
- 输入密码
在我的数据库中,电子邮件和用户名是不同的列。
函数如下:
public function authenticate()
{
$credentials=request()->only('username','password');
try {
$token=JWTAuth::attempt($credentials);
if(!$token){
return response()->json(['error'=>'credentials wrong'],401);
}
}
catch(JWTException $e) {
return response()->json(['error'=>'something_went_wrong'],500);
}
return response()->json(['token'=>$token],200);
}
这里只使用用户名,我想同时使用用户名和密码
如果需要任何其他信息,请询问。
ty:)
您可以使用FILTER_VALIDATE_EMAIL函数 像这样:-
$login_type = filter_var( $data['email'], FILTER_VALIDATE_EMAIL ) ? 'email' : 'username';
然后像这样传入 auth attempt 函数:-
Auth::attempt([$login_type => $data['email'], 'password' => $data['password']])
你的情况:-
According to your Html you have used two input
<input type="text"required autocomplete="off" name="loginEmail" id="inputname"/>
<input type="password"required autocomplete="off" name="loginPassword" id="inputpassword"/>
And your function is:-
public function authenticate(Request $request)
{
$data = $request->all();
$login_type = filter_var( $data['loginEmail'], FILTER_VALIDATE_EMAIL ) ? 'email' : 'username';
//$credentials=request()->only('username','password');
try {
$token=JWTAuth::attempt([$login_type => $data['loginEmail'], 'password' => $data['loginPassword']]);
if(!$token){
return response()->json(['error'=>'credentials wrong'],401);
}
}
catch(JWTException $e) {
return response()->json(['error'=>'something_went_wrong'],500);
}
return response()->json(['token'=>$token],200);
}
希望对您有所帮助!
在邮递员中,我在名为 'username' 的正文中使用了密钥,所以在这里代替 loginEmail ,用户名将会出现并且它会正常工作!泰
在您的 AuthController
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function getCredentials(Request $request)
{
$login = $request->get('loginEmail');
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
return [
$field => $login,
'password' => $request->get('password'),
];
}