问题
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