Having an issue with displaying user nicknames in Angular 2 using the User ID. When attempting to call the getUserName(userId) function from dashboard.component.html, which then triggers the auth0 service to retrieve the user profile, I am encountering continuous responses without being able to show the user nickname. Here is the relevant code snippet:
dashboard.component.ts
import { Component} from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { Auth0Service } from '../../services/auth0/auth0.service';
@Component({
moduleId: module.id,
selector: 'dashboard',
templateUrl: 'dashboard.component.html'
})
export class DashboardComponent {
constructor(private authService: AuthService, private _auth0Service: Auth0Service){
}
getUserName(userId:string){
let userName:string;
this._auth0Service.getUser(userId)
.subscribe(user=>{
userName=user.nickname;
});
}
return userName;
}
dashboard.component.html
<h1>User Dashboard</h1>
{{getUserName(authService.userProfile.user_id)}}
auth0.service.ts
import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions} from '@angular/http';
import { AuthHttp } from 'angular2-jwt';
import 'rxjs/add/operator/map';
@Injectable()
export class Auth0Service {
constructor(private _http:Http, private _authHttp: AuthHttp) {
}
getUser(userId: string){
let headers = new Headers({'Content-Type': 'application/json'});
headers.append('Authorization', 'Bearer token');
let options = new RequestOptions({headers: headers});
return this._http.get('https://url.auth0.com/api/v2/users/'+userId, options)
.map(res => res.json());
}
}
app.module.ts
@NgModule({
imports: [ BrowserModule, Routing, HttpModule, ... ],
bootstrap: [ AppComponent ],
providers: [ AUTH_PROVIDERS, AuthService, Auth0Service, AuthGuard, ... ]
})
export class AppModule { }
Please assist in resolving the issue with continuously receiving responses when trying to display user nicknames. I aim to call this function from the HTML to use it in various sections with different user IDs. Your prompt assistance is greatly appreciated!
Thank you, Abbas