question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$auth.getAuth() returns null for anonymous user?

See original GitHub issue

Version info

Angular: 1.5.8 Firebase: 3.5.2 AngularFire: 2.1.0

Other (e.g. Node, browser, operating system) (if applicable): Win 10 Firefox 49.0.2

Test case

Don’t have time at the moment

Steps to reproduce

  1. auth anymously.
  2. use following directive in resolve of route you want to disallow logged in user to view. i found here that $auth.getAuth() returns null $auth = firebaseAuth from factory:
	$stateProvider
	//game -> login
	.state('login',{
		url:'/login',
		controller: 'GameLoginCtrl',
		templateUrl:'views/desktop/game/login.html',
		resolve: {
			loadMyFiles:function($ocLazyLoad) {
				return $ocLazyLoad.load({
					name:'app',
					files:[
						'js/controllers/GameLogin.js',
						'css/login.css'
					]
				});
			},
			'currentAuth': ['$q','$auth',function($q,$auth){
				var u = $auth.$getAuth();
				var p = $q.defer();
				
				console.log(u); // null, but localStorage shows anonymous user.
				
				if (u) {
					return p.reject('ALREADY_AUTH');
				} else {
					return p.resolve();
				}
			}]
		}
	})

Expected behavior

Actual behavior

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:10

github_iconTop GitHub Comments

1reaction
jwngrcommented, Nov 1, 2016

Yup, you can do that. $waitForSignIn() is fulfilled with the auth state, so you can do:

resolve: {
  "currentAuth": ["Auth", function(Auth) {
    return Auth.$waitForSignIn()
      .then(function(currentUser) {
        if (currentUser !== null) {
          throw new Error("User logged in");
        }
      });
  }]
}
1reaction
r3wtcommented, Nov 1, 2016

looks like a race condition. if i wait a while and navigate to the url without a page refresh, it shows up correctly

Read more comments on GitHub >

github_iconTop Results From Across the Web

$auth.getAuth() returns null for anonymous user? #875 - GitHub
auth anymously. use following directive in resolve of route you want to disallow logged in user to view. i found here that $auth.getAuth()...
Read more >
$firebaseAuth().$getAuth() returns a null - Stack Overflow
to get the current user you should do this: var user = firebase.auth().currentUser; if (user) { // User is signed in. } else...
Read more >
Why is my currentUser == null in Firebase Auth? - Medium
Here's an easy enough bit of code in JavaScript that checks to see if a user is signed in: const user = firebase.auth().currentUser...
Read more >
Auth | JavaScript SDK | Firebase JavaScript API reference
Reference for Auth. ... The current emulator configuration (or null). ... If there is already an anonymous user signed in, that user will...
Read more >
Authentication
import { getAuth, onAuthStateChanged } from "firebase/auth"; const auth ... additionalUserInfo.profile == null // You can check if the user is new or ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found