Checking if user is still authenticated in angularfire2
Asked Answered
F

2

10

I have a working authentication using the angularfire2, I am just wondering if there is another way to check if the user is authenticated after the user recently logged in w/o hitting the server? The way I do this is like this

isAuthenticated():boolean{
 this.af.auth.subscribe(data => {
  if(data ){
  // User is authenticated
    return true;
  }
});
}

Any help is appreciated

Edit 1:

I decided to still use the original code to check if the user is recently logged in. What @cartant suggested is good enough when you are not checking the user at the start up of your application because it might return null as stated in the firebase official docs. The way I used it here is that I have menu items that should only be shown to authenticated users and using firebase.auth().currentUser will always return null on the initial load of the page.

Fade answered 15/4, 2017 at 6:11 Comment(2)
what is that you are getting. any error or?Galiot
@Galiot I am not getting an error. I just want to know if there is a way to check if the user is logged in without using the this.af.auth.subscribe()Fade
I
12

AngularFire2 is currently undergoing some refactoring and significant portions of its authentication API are going to be removed in favour of using the Firebase Web API in conjunction with AngularFire2. There is more information in this issue.

The observable that you have used will remain in AngularFire2 and you can subscribe to it to receive notifications of changes in the authentication state.

However, if all you are interested in is determining whether or not there is a currently authenticated user, there is a currentUser property in the Firebase Web API that will tell you that:

import * as firebase from "firebase";
...
console.log(firebase.auth().currentUser);
Icarian answered 15/4, 2017 at 7:34 Comment(5)
thank you for your answer. Yes, I saw that code somewhere too. I just thought that there might be an api for this in angularfire2. I guess I just need to wait for an update for this.Fade
No worries. The point I was trying to make in the answer, was that AngularFire2 is being refactored to avoid simply wrapping things that can be done with the underlying Firebase Web API and that using the two in conjunction is going to be encouraged. So an equivalent of currentUser is not going to be added to AngularFire2 and you might as well start using the Firebase Web API's currentUser right now.Icarian
Thank you for your answer. I decided to still use my original code thoughFade
In my case, using firebase.auth().currentUser, the authentication is required every time after the page is refreshed. This way, the authentication is not stored.Claudio
but even after signing out, firebase.auth().currentUser still returns and object, so how to set isLoggedIn variable to false after signing out, because i need to change html after signing in and out, so need to detect change in the isLoggedIn variable.Ophthalmitis
S
2

You can use the prebuilt Route users with AngularFire guards

AngularFireAuthGuard provides a prebuilt canActivate Router Guard using AngularFireAuth.

You can customize the behavior of this AngularFireAuthGuard by passing an RXJS pipe through the route data's authGuardPipe. You can learn more here.

Snowcap answered 28/9, 2020 at 16:44 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.