Unable to get name or other properties from Json Web Token after authentication
See original GitHub issueIssue type
I’m submitting a … (check one with “x”)
- code issue
- feature request
Issue description
Current behavior: After a successful authentication with Azure AD B2C and redirect back to the dashboard, the header.component and a custom component I’ve built, which is injected with the user.service (which has code to update the user object on token change), isn’t displaying any of the information in the token payload, such as the user’s name.
Steps to reproduce: My user.service.ts code is here:
import { Observable } from 'rxjs';
import { of } from 'rxjs/observable/of';
import { Injectable } from '@angular/core';
import { NbAuthOAuth2Token, NbAuthService } from '@nebular/auth';
@Injectable()
export class UserService {
user = {
name: 'JJGriff93',
given_name: 'James',
family_name: 'Griffin',
rank: 'PD Foundation 1',
level: 1,
totalXP: 704,
toNextLvl: 296,
picture: 'assets/images/defaultThumbnail.png',
leaderboardPosition: 1,
};
constructor(private authService: NbAuthService) {
this.authService.onTokenChange()
.subscribe((token: NbAuthOAuth2Token) => {
if (token.isValid()) {
this.user = token.getPayload(); // receive payload from token and assign it to our `user` variable
}
});
}
getUser(): Observable<any> {
return of(this.user);
}
// Add in assigning of token values to user properties
}
And an example of how it’s injected into the header.component:
import { Component, Input, OnInit } from '@angular/core';
import { NbMenuService, NbSidebarService } from '@nebular/theme';
import { UserService } from '../../../@core/data/users.service';
import { AppMonitoringService } from '../../../@core/monitoring/app-monitoring.service';
@Component({
selector: 'ngx-header',
styleUrls: ['./header.component.scss'],
templateUrl: './header.component.html',
})
export class HeaderComponent implements OnInit {
@Input() position = 'normal';
user = {};
userMenu = [{ title: 'Profile' }, { title: 'Log out' }];
constructor(private sidebarService: NbSidebarService,
private menuService: NbMenuService,
private userService: UserService,
private appMonitoringService: AppMonitoringService) { }
ngOnInit() {
this.user = this.userService.user;
}
toggleSidebar(): boolean {
this.sidebarService.toggle(true, 'menu-sidebar');
return false;
}
toggleSettings(): boolean {
this.sidebarService.toggle(false, 'settings-sidebar');
return false;
}
goToHome() {
this.menuService.navigateHome();
}
startSearch() {
this.appMonitoringService.logEvent('startSearch');
}
I’ve double checked the JWT in a decoder and it has the name, given_name and family_name properties, so not sure why Nebular is unable to find these
May be a problem with how I’m injecting it, any advice would be really appreciated
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Unable to access additional data with token in Web api 2 ...
If you're using JWT (JSON Web Tokens) you can view the contents of the generated token at https://jwt.io/ Just paste your token into...
Read more >How to get an access token with JWT Grant
Demonstrates how to get an access token using JSON Web Token (JWT) Grant authentication.
Read more >Troubleshooting JWT validation
Check that the "iss" (issuer) claim in your JWT token matches the first parameter of the endpoints.Issuer object. Error: Audience not allowed. If...
Read more >JWT authentication: Best practices and when to use it
Learn how to best use JWT to trust requests by using signatures, exchanging information between parties, and preventing basic security ...
Read more >JWT: The Complete Guide to JSON Web Tokens
This post is the first part of a two-parts step-by-step guide for implementing JWT-based Authentication in an Angular application (also ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jjgriff93 ` I did not try it, but If you want your component to observe the user attribute you declared in your user service, you could try to
userService
, declare theuser
attribute as a BehaviorSubject, something like this :userService
create apublishUser(user: any)
method with something likeuserService
create aonUserChange()
method with something like this :userService
constructor, modify your subscription to NbAuthService.onTokenChange(), with something like this :userService.onUserChange()
with something like thisAnd moreover, in the code above, you have to call
getAccessTokenPayload()
instead ofgetPayload()
:Before that do not forget to declare
NbAuthOAuth2JWTToken
class instead ofNbAuthOAuth2Token
in your auth strategy