I have follow the documentation of auth0 to implement profile picture and other profile data. The profile object from auth0 is empty until the page is loaded. Here is my code to call profile data from navbar component,
ngOnInit() {
if (this.auth.userProfile) {
this.profile = this.auth.userProfile;
return;
}
if (this.auth.authenticated) {
this.auth.getProfile((err, profile) => {
this.profile = profile;
});
}
}
Here is getProfile method from auth.service,
public getProfile(cb): void {
const accessToken = localStorage.getItem('access_token');
if (!accessToken) {
throw new Error('Access token must exist to fetch profile');
}
const self = this;
this.auth0.client.userInfo(accessToken, (err, profile) => {
if (profile) {
self.userProfile = profile;
}
cb(err, profile);
});
}
After login, i get the error 'Access token must exist to fetch profile' but if i reload it i dont see it.
I had the same issue as @Kaws
It works in the tutorial but when I try and implement it in my solution, I want to show the "nickname" in a navbar which is loaded before the access token is stored.
The solution to this is to use an observable as suggested by chenkie
AuthService.ts:
Then in your getProfile call in the component: