Hi before angular 12 I use this.userService.getUser(this.authService.decodedToken.nameid) but in angular 12 my token wasn’t decoded what can I do
I have user.service.ts file (
updateUser(id: number, user: User) {
return this.http.put(this.baseUrl + 'users/' + id, user);
}
) and in my profile userEdit.component.ts
@Injectable()
export class MemberEditResolver implements Resolve<User> {
constructor(
private userService: UserService,
private router: Router,
private alertify: AlertifyService,
private authService: AuthService
) {}
resolve(route: ActivatedRouteSnapshot): Observable<User | any> {
return this.userService.getUser(this.authService.decodedToken.nameid).pipe(
catchError(error => {
this.alertify.error('Problem retrieving your data');
this.router.navigate(['/home']);
return of(null);
})
);
}
}
in my Resolver
@Injectable()
export class MemberEditResolver implements Resolve<User> {
constructor(
private userService: UserService,
private router: Router,
private alertify: AlertifyService,
private authService: AuthService
) {}
resolve(route: ActivatedRouteSnapshot): Observable<User | any> {
return this.userService.getUser(this.authService.decodedToken.nameid).pipe(
catchError(error => {
this.alertify.error('Problem retrieving your data');
this.router.navigate(['/home']);
return of(null);
})
);
}
}
I also add my resolver in my angular Routes
{path: 'startup-edit', component: StartupEditComponent,resolve: {user: MemberEditResolver}},
and this is my authservice
login(model: any) {
return this.http.post(this.baseUrl + 'login', model).pipe(
map((response: any) => {
const user = response;
if (user) {
localStorage.setItem('token', user.token);
localStorage.setItem('user', JSON.stringify(user.user));
this.decodedToken = this.jwtHelper.decodeToken(user.token);
this.currentUser = user.user;
// this.userid = this.decodedToken.userid;
this.changeMemberPhoto(this.currentUser.photoUrl);
}
})
);
}
register(user: User) {
return this.http.post(this.baseUrl + 'register', user);
}
loggedIn() {
const token = localStorage.getItem('token');
return !this.jwtHelper.isTokenExpired(token!);
}