I am currently working on implementing Role-Based User Access Control With Firebase in order to grant access to a route only if the user is authenticated and has admin privileges. I am following this tutorial for guidance:
My AuthService
import { Injectable } from '@angular/core';
import {AngularFireAuth} from 'angularfire2/auth';
import {Observable} from 'rxjs/Observable';
import {User} from '../models/user.model';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {AngularFireDatabase} from 'angularfire2/database';
import * as firebase from 'firebase/app';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/switchMap';
@Injectable()
export class AuthService {
user: BehaviorSubject<User> = new BehaviorSubject(null)
constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase) {
this.afAuth.authState
.switchMap(auth => {
if (auth) {
/// signed in
return this.db.object('users/' + auth.uid)
} else {
/// not signed in
return Observable.of(null)
}
})
.subscribe(user => {
this.user.next(user)
})
}
}
My User Model
export interface Roles {
reader:boolean;
admin?:boolean;
}
export class User {
id:number;
admin:boolean;
activated:boolean;
usertype:string;
profile : {
title:string;
firstname:string;
lastname:string;
gender:string;
houseNr:string;
plz:string;
street:string;
place:string;
nation:string;
email:string;
companyname:string;
ustID:string;
phone:number;
mobile:number;
fax:number;
intPhonePrefix:string;
intMobilePrefix:string;
intFaxPrefix:string;
phonePrefix:string;
mobilePrefix:string;
faxPrefix:string;
bankDepositor:string;
bankIban:string;
bankBic:string;
bankInstitute:string;
}
roles: Roles;
constructor() {
this.roles = { reader: true }
}
}
Unfortunately, I encountered a syntax error on .switchMap(auth => {
:
https://i.sstatic.net/b0c68.png
Any idea what could be causing this error?