使用JWT身份验证时,我的登录请求出现401错误,在注册页面上它运行良好,并且在数据库中创建了用户,但登录不起作用。 我真的不知道要包含哪些代码,因此随时可以获取所需的代码。
我验证了输入的凭据已存在于数据库中
login.ts
export class LoginComponent {
public form={
em:null,
pass:null
};
public error = null;
constructor(private http :HttpClient) {
}
Submit(){
this.http.post('http://localhost:8000/api/login',this.form).subscribe(
data => console.log(data),
error => console.log(error)
);
}
}
api.php
<?php
Route::group([
'middleware' => 'api',
], function () {
Route::post('login', 'AuthController@login');
});
登录功能
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = auth()->attempt($credentials)) {
return response()->json(['error' => 'user does not exist'], 401);
}
return $this->respondWithToken($token);
}
答案 0 :(得分:1)
您需要在发帖请求中发送一个Authorization
标头:
Authorization: 'Bearer ' + jwtToken
代替在每个api调用中手动发送授权标头,更好的选择是使用HttpInterceptor
。