Firebase passing Authentication between web pages

天涯浪子 提交于 2019-12-11 04:04:47

问题


i don't know how to share the authentication between my web. For example, i want to pass authentication to home page after user logged in in login.html.

What is the problem:

After user logged in in login.html, the authentication is created.

//login.html
var user = firebase.auth().currentUser;
if (user) {
  // User is signed in.
}

However, i need to change location.href to home.html after user logged in.

//login.html
if (user) {
    // User is signed in.
    location.href = "home.html";
}

So, what is the solution that keep the authentication when changing web page?

  //home.html
    if (user) {
        // 
    }else {
        // No user is signed in.
     } 

web/login.html <--> web/home.html


回答1:


Firebase keeps track of the authentication state between pages by storing the session id in local storage.

The best way to ensure a smooth flow for you users is to monitor the authentication state. From that documentation:

The recommended way to get the current user is by setting an observer on the Auth object:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

By using an observer, you ensure that the Auth object isn't in an intermediate state—such as initialization—when you get the current user.



来源:https://stackoverflow.com/questions/37550498/firebase-passing-authentication-between-web-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!