Angular: browser refresh handling in AuthGuard
I have been trying to implement AuthGuard properly in my webapp. Currently when I navigate within the app, it works fine. But when I refresh, the authService.loggedIn is processed as false before the AuthService finished executing.
Here is my code:
auth.guard.ts
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {Injectable} from '@angular/core';
import {AuthService} from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (state.url === '/login') {
if (this.authService.loggedIn) {
this.router.navigate(['/']).then().catch();
} else {
return true;
}
} else {
if (this.authService.loggedIn) {
return true;
} else {
this.router.navigate(['/login']).then().catch();
}
}
}
}
auth.service
import {Injectable, OnInit} from '@angular/core';
import {AngularFireAuth} from '@angular/fire/auth';
import {auth} from 'firebase';
import {Router} from '@angular/router';
import {Subject} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService implements OnInit {
loggedIn = false;
constructor(public afAuth: AngularFireAuth, private router: Router) {
this.afAuth.authState.subscribe((user) => {
if (user) {
this.loggedIn = true;
}
});
}
ngOnInit() {
}
...
}
I research online and they mentioned different approach
(e.g. https://gist.github.com/codediodeio/3e28887e5d1ab50755a32c1540cfd121) but could not make it work on my app.
One error I encounter when I try this approach is "ERROR in src/app/auth.guard.ts(20,8): error TS2339: Property 'take' does not exist on type 'Observable'." I use
import {AngularFireAuth} from '@angular/fire/auth';
and not
import { AngularFireAuth } from 'angularfire2/auth';
Any help / suggestion is appreciated.
Thanks everyone.
angular angularfire2 auth-guard
add a comment |
I have been trying to implement AuthGuard properly in my webapp. Currently when I navigate within the app, it works fine. But when I refresh, the authService.loggedIn is processed as false before the AuthService finished executing.
Here is my code:
auth.guard.ts
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {Injectable} from '@angular/core';
import {AuthService} from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (state.url === '/login') {
if (this.authService.loggedIn) {
this.router.navigate(['/']).then().catch();
} else {
return true;
}
} else {
if (this.authService.loggedIn) {
return true;
} else {
this.router.navigate(['/login']).then().catch();
}
}
}
}
auth.service
import {Injectable, OnInit} from '@angular/core';
import {AngularFireAuth} from '@angular/fire/auth';
import {auth} from 'firebase';
import {Router} from '@angular/router';
import {Subject} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService implements OnInit {
loggedIn = false;
constructor(public afAuth: AngularFireAuth, private router: Router) {
this.afAuth.authState.subscribe((user) => {
if (user) {
this.loggedIn = true;
}
});
}
ngOnInit() {
}
...
}
I research online and they mentioned different approach
(e.g. https://gist.github.com/codediodeio/3e28887e5d1ab50755a32c1540cfd121) but could not make it work on my app.
One error I encounter when I try this approach is "ERROR in src/app/auth.guard.ts(20,8): error TS2339: Property 'take' does not exist on type 'Observable'." I use
import {AngularFireAuth} from '@angular/fire/auth';
and not
import { AngularFireAuth } from 'angularfire2/auth';
Any help / suggestion is appreciated.
Thanks everyone.
angular angularfire2 auth-guard
add a comment |
I have been trying to implement AuthGuard properly in my webapp. Currently when I navigate within the app, it works fine. But when I refresh, the authService.loggedIn is processed as false before the AuthService finished executing.
Here is my code:
auth.guard.ts
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {Injectable} from '@angular/core';
import {AuthService} from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (state.url === '/login') {
if (this.authService.loggedIn) {
this.router.navigate(['/']).then().catch();
} else {
return true;
}
} else {
if (this.authService.loggedIn) {
return true;
} else {
this.router.navigate(['/login']).then().catch();
}
}
}
}
auth.service
import {Injectable, OnInit} from '@angular/core';
import {AngularFireAuth} from '@angular/fire/auth';
import {auth} from 'firebase';
import {Router} from '@angular/router';
import {Subject} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService implements OnInit {
loggedIn = false;
constructor(public afAuth: AngularFireAuth, private router: Router) {
this.afAuth.authState.subscribe((user) => {
if (user) {
this.loggedIn = true;
}
});
}
ngOnInit() {
}
...
}
I research online and they mentioned different approach
(e.g. https://gist.github.com/codediodeio/3e28887e5d1ab50755a32c1540cfd121) but could not make it work on my app.
One error I encounter when I try this approach is "ERROR in src/app/auth.guard.ts(20,8): error TS2339: Property 'take' does not exist on type 'Observable'." I use
import {AngularFireAuth} from '@angular/fire/auth';
and not
import { AngularFireAuth } from 'angularfire2/auth';
Any help / suggestion is appreciated.
Thanks everyone.
angular angularfire2 auth-guard
I have been trying to implement AuthGuard properly in my webapp. Currently when I navigate within the app, it works fine. But when I refresh, the authService.loggedIn is processed as false before the AuthService finished executing.
Here is my code:
auth.guard.ts
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {Injectable} from '@angular/core';
import {AuthService} from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (state.url === '/login') {
if (this.authService.loggedIn) {
this.router.navigate(['/']).then().catch();
} else {
return true;
}
} else {
if (this.authService.loggedIn) {
return true;
} else {
this.router.navigate(['/login']).then().catch();
}
}
}
}
auth.service
import {Injectable, OnInit} from '@angular/core';
import {AngularFireAuth} from '@angular/fire/auth';
import {auth} from 'firebase';
import {Router} from '@angular/router';
import {Subject} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService implements OnInit {
loggedIn = false;
constructor(public afAuth: AngularFireAuth, private router: Router) {
this.afAuth.authState.subscribe((user) => {
if (user) {
this.loggedIn = true;
}
});
}
ngOnInit() {
}
...
}
I research online and they mentioned different approach
(e.g. https://gist.github.com/codediodeio/3e28887e5d1ab50755a32c1540cfd121) but could not make it work on my app.
One error I encounter when I try this approach is "ERROR in src/app/auth.guard.ts(20,8): error TS2339: Property 'take' does not exist on type 'Observable'." I use
import {AngularFireAuth} from '@angular/fire/auth';
and not
import { AngularFireAuth } from 'angularfire2/auth';
Any help / suggestion is appreciated.
Thanks everyone.
angular angularfire2 auth-guard
angular angularfire2 auth-guard
asked Nov 14 '18 at 6:32
yeoman_983yeoman_983
102
102
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I was able to make AuthGuard with browser refresh work.
This link helped a lot: https://gist.github.com/codediodeio/3e28887e5d1ab50755a32c1540cfd121
I just used pipe() to chain operators and used tap() instead of do() since I'm using newer version of rxjs.
Here is the code:
...
import {Observable} from 'rxjs';
import {take, map, tap} from 'rxjs/operators';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private afAuth: AngularFireAuth) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable {
return this.afAuth.authState
.pipe(
take(1),
map(user => !!user),
tap(
loggedIn => {
if (!loggedIn) {
this.router.navigate(['/login']);
}
}
)
);
}
}
Thanks.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294329%2fangular-browser-refresh-handling-in-authguard%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I was able to make AuthGuard with browser refresh work.
This link helped a lot: https://gist.github.com/codediodeio/3e28887e5d1ab50755a32c1540cfd121
I just used pipe() to chain operators and used tap() instead of do() since I'm using newer version of rxjs.
Here is the code:
...
import {Observable} from 'rxjs';
import {take, map, tap} from 'rxjs/operators';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private afAuth: AngularFireAuth) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable {
return this.afAuth.authState
.pipe(
take(1),
map(user => !!user),
tap(
loggedIn => {
if (!loggedIn) {
this.router.navigate(['/login']);
}
}
)
);
}
}
Thanks.
add a comment |
I was able to make AuthGuard with browser refresh work.
This link helped a lot: https://gist.github.com/codediodeio/3e28887e5d1ab50755a32c1540cfd121
I just used pipe() to chain operators and used tap() instead of do() since I'm using newer version of rxjs.
Here is the code:
...
import {Observable} from 'rxjs';
import {take, map, tap} from 'rxjs/operators';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private afAuth: AngularFireAuth) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable {
return this.afAuth.authState
.pipe(
take(1),
map(user => !!user),
tap(
loggedIn => {
if (!loggedIn) {
this.router.navigate(['/login']);
}
}
)
);
}
}
Thanks.
add a comment |
I was able to make AuthGuard with browser refresh work.
This link helped a lot: https://gist.github.com/codediodeio/3e28887e5d1ab50755a32c1540cfd121
I just used pipe() to chain operators and used tap() instead of do() since I'm using newer version of rxjs.
Here is the code:
...
import {Observable} from 'rxjs';
import {take, map, tap} from 'rxjs/operators';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private afAuth: AngularFireAuth) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable {
return this.afAuth.authState
.pipe(
take(1),
map(user => !!user),
tap(
loggedIn => {
if (!loggedIn) {
this.router.navigate(['/login']);
}
}
)
);
}
}
Thanks.
I was able to make AuthGuard with browser refresh work.
This link helped a lot: https://gist.github.com/codediodeio/3e28887e5d1ab50755a32c1540cfd121
I just used pipe() to chain operators and used tap() instead of do() since I'm using newer version of rxjs.
Here is the code:
...
import {Observable} from 'rxjs';
import {take, map, tap} from 'rxjs/operators';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private afAuth: AngularFireAuth) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable {
return this.afAuth.authState
.pipe(
take(1),
map(user => !!user),
tap(
loggedIn => {
if (!loggedIn) {
this.router.navigate(['/login']);
}
}
)
);
}
}
Thanks.
answered Nov 21 '18 at 3:48
yeoman_983yeoman_983
102
102
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294329%2fangular-browser-refresh-handling-in-authguard%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown