angular - 如何为登录请求修复 401(未经授权)
问题描述
我使用 JWT 身份验证的登录请求出现 401 错误,在注册页面上它运行良好,它在数据库中创建用户,但登录不起作用。我真的不知道要包含哪些代码,所以请随意使用所需的代码。
我验证输入的凭据已存在于数据库中
登录.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);
}
解决方案
Authorization
您需要在发布请求中发送标头:
Authorization: 'Bearer ' + jwtToken
与其在每个 api 调用中手动发送授权标头,不如使用HttpInterceptor
.
推荐阅读
- html - 为什么 VoiceOver iOS 在离开输入字段后会恢复光标位置?
- php - Laravel,如何在 where 查询中使用 select AS?
- python - 正确实现 os.path.join() 以递归方式替换文件名和目录名中的字符串
- docker - 复制失败:stat /var/lib/docker/tmp/docker-xxx:没有这样的文件或目录
- python - 如何从Python中的给定字符串中单独解析日期字符串?
- python - 如何将 PyQt5 QLineEdit 响应存储到字典中特定位置的字典中
- google-cloud-datalab - 通过 Google Cloud Shell 连接时无法在 datalab 中运行笔记本
- azure - 403 禁止 microsoft-azure-application-gateway/v2
- c++ - 我如何在没有定义的情况下在 C++ 中获取变量名
- python - 如果标识符在范围内,则选择训练数据