gpt4 book ai didi

Angular 4 jwt 身份验证

转载 作者:太空狗 更新时间:2023-10-29 18:30:52 25 4
gpt4 key购买 nike

使用 Angular 应用程序中的 Passport jwt 进行身份验证 我将我的请求 header 设置为 angular 4 我在服务器端使用 passport jwt,但出现未经授权的错误

`let strategy = new JwtStrategy(jwtOpts, (jwt_payload, cb)=>{
let stmt = "select * from user where id = ?";
let id = jwt_payload.id;
connection.query(stmt, id, (error, result) => {
if(error){
throw error;
}
if(result){
var userinfo = {
email: result[0].email,
id: result[0].id
}
cb(null, userinfo);
}
else{
cb(null, false);
}
})
})`

//this is my route
`app.use('/tournament',passport.authenticate('jwt', {session: false}), tour_route);`


//This is my angular call
createTournament(name: string){
this.token = 'JWT ' + this.authService.getToken();
const body = {
name
}
this.http.post('http://localhost:8000/tournament',
{
headers: new HttpHeaders().set('Authorization', this.token)
})
.subscribe(
response => {}
);
}

But i am getting an unauthorised access even if i am sending the right token

最佳答案

尝试添加 Bearer 或 Token 或 Basic 以及 Token,这应该可以解决问题。

headers: new HttpHeaders().set('Authorization', 'Bearer '+this.token)

关于Angular 4 jwt 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45459660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com