Problem with protecting admin routes with angular 8 and firebase

一曲冷凌霜 提交于 2020-01-16 09:06:14

问题


I wanted to protect admin routes in my web application. I created an admin-auth-guard service and used canActivate()(I had problems but I fixed it with reference to A previous thread). Now during the time of compiling no error popped up but in realtime, the console popped up some error messages: Console Error Message Here is my admin auth guard service code:

export class AdminAuthGuardService {

  constructor(private auth: AuthService, private userService: UserService) { }
  canActivate():Observable<boolean>{
    return this.auth.user$
    .pipe(switchMap( user => {
      return this.userService.get(user.uid).valueChanges()}))
    .pipe(map(appUser=>appUser.isAdmin));
  }
}

My auth service code:

export class AuthService {
  user$:Observable<firebase.User>
  constructor(private afAuth :  AngularFireAuth ,private route: ActivatedRoute) {
    this.user$=afAuth.authState;
   }
  login(){
    let returnUrl=this.route.snapshot.queryParamMap.get('returnUrl') || '/'
    localStorage.setItem('returnUrl',returnUrl);
    this.afAuth.auth.signInWithRedirect(new firebase.auth.GoogleAuthProvider());
  }
  logout(){
    this.afAuth.auth.signOut();
  }
}

My User-Service Code:

export class UserService {

  constructor(private db: AngularFireDatabase) { }
  save(user:firebase.User){
    this.db.object('/users/'+user.uid).set({
      name: user.displayName,
      email: user.email
    });
  }
  get (uid:string) : AngularFireObject<appUser>{
    return this.db.object('/users'+uid);
  }
}

My App-User module:

export interface appUser{
    name: string;
    email: string;
    isAdmin: boolean
}

I can not understand why this is happening.Thank You for any help.

来源:https://stackoverflow.com/questions/58630650/problem-with-protecting-admin-routes-with-angular-8-and-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!