我试图制作auth API,我已成功注册。 比登录无法成功。 请帮助我,谢谢。
public function login(Request $request)
{
try
{
if (!$request->has('Account')
|| !$request->has('Password'))
{
throw new Exception('Parameter missing');
}
$checkUser = DB::table('Users')->where('Account',$request->Account)->first();
if(empty($checkUser))
{
throw new Exception('No Data');
}
$data = ([
'Account' => $request->Account,
'Password' => $request->Password,
]);
if(!Auth::attempt($data))
throw new Exception('Verification error');
此数据库信息。
答案 0 :(得分:1)
尝试以下操作,以使注册时需要先哈希密码再保存到数据库中:
User::create([
'Account' => $request->Account,
'CreateDateTime' => date('Y-m-d'),
'UpdatedDateTime' => date('Y-m-d'),
'Password' => Hash::make($request->Password),
]);
答案 1 :(得分:0)
以这种方式尝试它可能会有所帮助,并且还可以验证: 如果不确定密码,请先调试
$table->string('password', 60)->nullable();
----------------------------------------------------
return Validator::make($data, [
'email' => 'required|email',
'password' => 'required',
]);
-----------------------------------------------
$user_data=User::where('username','=',$request->username)->first();
$userScope=$user_data->scope;
Input::merge([
'client_id' => env('CLIENT_ID'),
'client_secret' => env('CLIENT_SECRET'),
'scope' => 'admin'
]);
$credentials = $request->only(['grant_type', 'username', 'password','scope']);
$validationRules = $this->getLoginValidationRules();
$credentials["client_id"] = env('CLIENT_ID');
$credentials["client_secret"] = env('CLIENT_SECRET');
$this->validateOrFail($credentials, $validationRules);
try {
if (!$accessToken = Authorizer::issueAccessToken()) {
return $this->response->errorUnauthorized();
}
} catch (\League\OAuth2\Server\Exception\OAuthException $e) {
throw $e;
return $this->response->error('could_not_create_token', 500);
}
$accessToken["groups"][] = $userScope;
return response()->json(compact('accessToken'));