【发布时间】:2023-04-05 20:33:01
【问题描述】:
我对用于在 Angular 应用程序的授权服务中存储角色的数组有疑问。我正在使用 auth0 服务登录。 auth.service.ts 看起来像这样:
@Injectable()
export class AuthService {
userProfile: any;
private roles: string[] = [];
...
public handleAuthentication(): void {
this.auth0.parseHash((err, authResult) => {
if (authResult && authResult.accessToken && authResult.idToken) {
window.location.hash = '';
this.setSession(authResult);
this.getRoles(authResult);
this.router.navigate(['/']);
} else if (err) {
this.router.navigate(['/']);
console.log(err);
alert(`Error: ${err.error}. Check the console for further details.`);
}
});}
//THIS IS getRole function
private getRoles(authResult){
let jwtHelper = new JwtHelper();
let decodedToken = jwtHelper.decodeToken(authResult.idToken);
this.roles = decodedToken['https://tobenorme.com/roles'];
}
登录后,handleAuthentication 函数被调用,一切正常,直到点击刷新按钮。然后角色数组返回空数组,我不再能够获得角色。我尝试将它们存储在本地存储中,但可以在浏览器中进行编辑。
【问题讨论】:
标签: angular typescript jwt token auth0