Firebase 身份验证在刷新时延迟

IT技术 reactjs firebase react-router firebase-authentication
2021-05-01 15:38:21

我的 SPA React/Firebase 应用程序的硬刷新不会在立即执行函数时保持身份验证状态。我有一个解决方法,但它是粗略的。

我的react路线利用该onEnter功能来确定用户是否经过身份验证。例如

<Route path="/secure" component={Dashboard} onEnter={requireAuth}/>

此外,我的requireAuth函数如下所示:

function (nextState, replace) {
        console.log('requireAuth', firebase.auth().currentUser);
        if (!firebase.auth().currentUser) {
            console.log('attempting to access a secure route. please login first.');
            replace({
                pathname: '/login',
                state: { nextPathname: nextState.location.pathname }
            });
        }
};

但是,在硬刷新时, 会有轻微的延迟firebase.auth().currentUser它首先为空,然后执行POST到 firebase 服务器以确定身份验证状态。当它返回时,currentUser对象被填充。但是,这种延迟会导致问题。

我的hacky解决方案如下:更新:这实际上不起作用......

function (nextState, replace) {
    setTimeout(function () {
        console.log('requireAuth', firebase.auth().currentUser);
        if (!firebase.auth().currentUser) {
            console.log('attempting to access a secure route. please login first.');
            replace({
                pathname: '/login',
                state: { nextPathname: nextState.location.pathname }
            });
        }
    }, 50);
};

只需将其包装在超时中即可。但是,我真的不喜欢这样……有什么想法吗?

更新:

我还尝试将它包装在一个onAuthStateChanged侦听器中,这应该比setTimeout具有明确时间延迟的 a更准确代码如下:

function (nextState, replace) {
    var unsubscribe = firebase.auth().onAuthStateChanged(function (user) {
        if (!user) {
            console.log('attempting to access a secure route');
            replace({
                pathname: '/login',
                state: { nextPathname: nextState.location.pathname }
            })
            console.log('should have called replace');
        }
        unsubscribe();
    });
    // setTimeout(function () {
    //     console.log('requireAuth', firebase.auth().currentUser);
    //     if (!firebase.auth().currentUser) {
    //         console.log('attempting to access a secure route. please login first.');
    //         replace({
    //             pathname: '/login',
    //             state: { nextPathname: nextState.location.pathname }
    //         });
    //     }
    // }, 50);
};

两条日志语句都执行了,但是react-routerreplace好像没有正确执行。对于react-router专家来说,也许这是一个不同的问题。

更新2:

我做这件事的时候已经深夜了。显然setTimeout实际上也不起作用。

4个回答

好的。因此,我能够通过利用localStoragefirebase 提供的用于存储用户信息变量来解决这个问题

function (nextState, replace) {
    if (!firebase.auth().currentUser) {
        let hasLocalStorageUser = false;
        for (let key in localStorage) {
            if (key.startsWith("firebase:authUser:")) {
                hasLocalStorageUser = true;
            }
        }
        if (!hasLocalStorageUser) {
            console.log('Attempting to access a secure route. Please authenticate first.');
            replace({
                pathname: '/login',
                state: { nextPathname: nextState.location.pathname }
            });
        }
    }
};

虽然这是一篇与 ReactJS 相关的帖子,但我最近在为 AngularJS 编写自己的身份验证/授权服务时遇到了同样的问题。在页面刷新时onAuthStateChanged传递了一个用户,这是null因为 firebase 仍在初始化(异步)。

唯一对我有用的解决方案是在localStorage用户登录后存储用户 uid 并在用户注销后删除该值。

由于我正在使用 aauthService并且userService单独注册了一个侦听器,authService一旦用户登录/注销就会触发该侦听器

代码示例 authService(不是完整的 authService):

var loginListeners = [];
var logoutListeners = [];

function addLoginListener(func) {
    loginListeners.push(func);
}

function addLogoutListener(func) {
    logoutListeners.push(func);
}

function login(email, password) {
    return firebase.auth().signInWithEmailAndPassword(email, password).then(function(user) {
        for(var i = 0; i < loginListeners.length; i++) {
            loginListeners[i](user); // call registered listeners for login
        }
    });
}

function logout() {
    return firebase.auth().signOut().then(function() {
        for(var i = 0; i < logoutListeners.length; i++) {
            logoutListeners[i](); // call registered listeners for logout
        }
    });
}

代码示例 userService(不是完整的 userService):

.provider('userService', ['authServiceProvider',
function UserService(authServiceProvider) {

var usersRefUrl = '/users';
var userInfo = null;
var userDetails = null;

// refreshHack auto-executed when this provider creates the service
var storageId = 'firebase:uid'; // storing uid local because onAuthStateChanged gives null (when async initializing firebase)
(function addRefreshHackListeners() {
    authServiceProvider.addLoginListener(function(user) {
        userInfo = user;
        localStorage.setItem(storageId, user.uid); // store the users uid after login so on refresh we have uid to retreive userDetails
    });
    authServiceProvider.addLogoutListener(function() {
        userInfo = null;
        localStorage.removeItem(storageId);
    });
    firebase.auth().onAuthStateChanged(function(user) {
        if(user) { // when not using refreshHack user is null until async initializing is done (and no uid is available).
            localStorage.setItem(storageId, user.uid);
            userInfo = user;
            resolveUserDetails();
        } else {
            localStorage.removeItem(storageId);
            userInfo = null;
            userDetails = null;
        }
    });
})();

function isLoggedIn() {
    return userInfo ? userInfo.uid : localStorage.getItem(storageId); // check localStorage for refreshHack
}

function resolveUserDetails() {
    var p = null;
    var uid = isLoggedIn();
    if(uid)
        p = firebase.database().ref(usersRefUrl + '/' + uid).once('value').then(function(snapshot) {
            userDetails = snapshot.val();
            return userDetails;
        }).catch(function(error) {
            userDetails = null;
        });

    return p; // resolve by returning a promise or null
}
}]);

在运行块中,您可以全局注册用户并解析每次路由更改的用户信息/详细信息(使其更安全):

.run(['$rootScope', 'userService', 'authService',
function($rootScope, userService, authService) {

// make user available to $root in every view
$rootScope.user = userService.getUser();

$rootScope.$on('$routeChangeStart',
        function(event, next, current) {

    // make sure we can add resolvers for the next route
    if(next.$$route) {
        if(next.$$route.resolve == null)
            next.$$route.resolve = {};

        // resolve the current userDetails for every view
        var user = userService.resolveUserDetails();
        next.$$route.resolve.userDetails = function() {
            return user;
        }
    }
});
}]);

也许这可以帮助那些在同一问题上挣扎的人。除此之外,请随时优化和讨论代码示例。

通过管理 localStorage 工作。这是我如何做的例子。

  constructor(props) {
    super(props);

    let authUser = null;

    // setting auth from localstorage
    for (let key in localStorage) {
      if (key === storageId) {
        authUser = {};
        break;
      }
    }

    this.state = {authUser};
  }

  componentDidMount() {
    firebase
      .auth
      .onAuthStateChanged(authUser => {

        if (authUser) {
          localStorage.setItem(storageId, authUser.uid);
        } else {
          localStorage.removeItem(storageId);
        }

        // change state depending on listener
        authUser
          ? this.setState({authUser})
          : this.setState({authUser: null});
      });
  }

这对我有用,尝试根据 firebase.auth 的状态将您的主应用程序包装在 if else 语句中。

 constructor() {
    super();
    this.state={
    user:{},
    stateChanged:false
    };
  } 
componentDidMount(){
fire.auth().onAuthStateChanged((user)=>{
      this.setState({stateChanged:true})
    });
}
render() { 
    if(this.state.stateChanged==false){
      return(
        <div>
          Loading
        </div>
      )
    }
else{
    
        return(<div>your code goes here...</div> )

}
}