Skip to content

Commit 7f30a6e

Browse files
committed
fix: broswer refresh authguard
1 parent 14bed08 commit 7f30a6e

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/app/core/auth.guard.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { Injectable } from '@angular/core';
22
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
33
import { Observable } from 'rxjs/Observable';
44
import { AuthService } from './auth.service';
5-
// import { AngularFireAuth } from "angularfire2";
5+
import 'rxjs/add/operator/do';
6+
import 'rxjs/add/operator/map';
7+
import 'rxjs/add/operator/take';
68

79
@Injectable()
810
export class AuthGuard implements CanActivate {
@@ -14,10 +16,15 @@ export class AuthGuard implements CanActivate {
1416
state: RouterStateSnapshot): Observable<boolean> | boolean {
1517
if (this.auth.authenticated) { return true; }
1618

17-
console.log('access denied!')
18-
this.router.navigate(['/login']);
19-
return false
20-
19+
return this.auth.currentUserObservable
20+
.take(1)
21+
.map(user => !!user)
22+
.do(loggedIn => {
23+
if (!loggedIn) {
24+
console.log("access denied")
25+
this.router.navigate(['/login']);
26+
}
27+
})
2128

2229
}
2330
}

src/app/core/auth.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export class AuthService {
3333
return this.authenticated ? this.authState.auth : null;
3434
}
3535

36+
// Returns
37+
get currentUserObservable(): any {
38+
return this.af.auth
39+
}
40+
3641
// Returns current user UID
3742
get currentUserId(): string {
3843
return this.authenticated ? this.authState.uid : '';

0 commit comments

Comments
 (0)