-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.service.ts
39 lines (34 loc) · 1.08 KB
/
auth.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { Injectable } from "@angular/core";
import { AngularFireAuth } from "angularfire2/auth";
import * as firebase from "firebase";
import { Observable } from "rxjs/Observable";
import { ActivatedRoute } from "@angular/router";
import { AppUser } from "./models/app-user";
import "rxjs/add/observable/of";
import "rxjs/add/operator/switchMap";
import { UserService } from "./user.service";
@Injectable()
export class AuthService {
user$: Observable<firebase.User>;
constructor(
private userService: UserService,
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();
}
get appUser$(): Observable<AppUser> {
return this.user$.switchMap(user => {
if (user) return this.userService.get(user.uid);
return Observable.of(null);
});
}
}