diff --git a/packages/bridge-ui/src/app/services/users.service.ts b/packages/bridge-ui/src/app/services/users.service.ts index 70091a2..854411c 100644 --- a/packages/bridge-ui/src/app/services/users.service.ts +++ b/packages/bridge-ui/src/app/services/users.service.ts @@ -16,7 +16,8 @@ export class UsersService { isAuthenticated = false; permissions: string[] = []; - localUser: any | null | undefined = null; + localAuth0User: any | null | undefined = null; + localUser: User | null | undefined = null; additionalInfo : any = null; constructor( @@ -37,8 +38,8 @@ export class UsersService { if(!this.isAuthenticated) { return } this.authService.user$.subscribe(data => { - this.localUser = data; - console.log(this.localUser); + this.localAuth0User = data; + console.log(this.localAuth0User); this.reportLogin(); }) @@ -56,8 +57,11 @@ export class UsersService { getUserdetails(id: string) { let promise = new Promise((resolve, reject) => { - - this.httpClient.get(this.serverEndpointService.getCurrentEndpoint()+"users/"+"auth0|"+id).subscribe(data => { + let _id = id; + if(!_id.startsWith("auth0|")) { + _id = "auth0|" + _id; + } + this.httpClient.get(this.serverEndpointService.getCurrentEndpoint()+"users/"+_id).subscribe(data => { let user = plainToClass(User, data, { excludeExtraneousValues: true, enableCircularCheck: true }); let val = user instanceof User ? user : user[0] resolve(val); @@ -67,47 +71,48 @@ export class UsersService { } updateUserDetails(nickname: string, realname: string) { - let id = this.localUser.sub; + let id = this.localAuth0User.sub; return this.httpClient.post(this.serverEndpointService.getCurrentEndpoint()+"users/"+id+"/update", {id, nickname, realname}); } - reportLogin() { - if(!this.localUser) { return } - let id = this.localUser.sub; - let nickname = this.localUser.nickname; - let picture = this.localUser.picture; + async reportLogin() { + if(!this.localAuth0User) { return } + let id = this.localAuth0User.sub; + let nickname = this.localAuth0User.nickname; + let picture = this.localAuth0User.picture; let time = Date.now(); - this.httpClient.post(this.serverEndpointService.getCurrentEndpoint()+"users/login", {id, nickname, picture, time}).subscribe(); + await this.httpClient.post(this.serverEndpointService.getCurrentEndpoint()+"users/login", {id, nickname, picture, time}).subscribe(); + this.getUserdetails(id).subscribe( data => { this.localUser = data;} ) } getUserName() : string { if(!this.isAuthenticated || !this.localUser) {return ""} - return this.localUser.nickname || ""; + return this.localUser.getUserName() } getUserId() : string { if(!this.isAuthenticated || !this.localUser) {return ""} - return this.localUser.sub?.replace("auth0|", "") || ""; + return this.localUser.getUserId(); } getUserPicture() : string { if(!this.isAuthenticated || !this.localUser) {return ""} - return this.localUser.picture || ""; + return this.localUser.getUserPicture(); } getUserEmail() : string { if(!this.isAuthenticated || !this.localUser) {return ""} - return this.localUser.email || ""; + return this.localUser.getUserEmail(); } getUserRealName() : string { - if(!this.isAuthenticated || !this.localUser || !this.additionalInfo) {return "??? ???"} + if(!this.isAuthenticated || !this.localUser ) {return "??? ???"} - return "??? ???"; + return this.localUser.getUserRealName(); } canCreateSeasons() : boolean { @@ -127,7 +132,7 @@ export class UsersService { } isLocalUser(id: string) { - if(!this.isAuthenticated || !this.localUser) {return false} + if(!this.isAuthenticated || !this.localAuth0User) {return false} let other = id.replace("auth0|", "") return this.getUserId() == other;