![](/img/trans.png)
[英]How do I redirect my login page to the homepage after successful sign in by user?
[英]How do I redirect the user after a successful login on firebase?
我剛開始一個新項目,正在從事auth。 我可以使用電子郵件,谷歌或github登錄用戶。 但是,如何在成功登錄后將它們重定向到另一個頁面:下面是以下代碼:
var config = {
apiKey: "asdfasdfasdf",
authDomain: "asdfasdfasdf",
databaseURL: "asdfasdfasdf",
storageBucket: "asdfasdfasdf",
};
firebase.initializeApp(config);
//The stuff
var logine = document.getElementById("logine");
var reggy = document.getElementById("reggy");
var usr = document.getElementById("usr");
var pwd = document.getElementById("pwd");
var reset = document.getElementById("forgot");
var google = document.getElementById("google");
var logout = document.getElementById("logout");
var github = document.getElementById("github");
//Login event
function loginEvent(){
//Retreive the email and dat pwd
var email = usr.value;
var password = pwd.value;
var auth = firebase.auth();
//User signs in
var promise = auth.signInWithEmailAndPassword(email,password).catch(function(error){
//Handle any errors here
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === 'auth/wrong-password'){
alert('Get the password right');
} else {
alert(errorMessage);
}
});
promise.catch(e => console.log(e.message));
if (email.length < 12){
document.getElementById("bademail").classList.remove("hide");
}
if(password.length < 6){
document.getElementById("bademail").classList.remove("hide");
}
}
//Register event
function registerEvent(){
var email = usr.value;
var password = pwd.value;
var auth = firebase.auth();
//User signs in
// TODO: Check for email
var promise = auth.createUserWithEmailAndPassword(email, password).catch(function(error){
//Handle any errors here
var errorCode = error.code;
var errorMessage = error.message;
if(errorCode === 'auth/weak-password'){
alert('All passwords are must be 6 characters or more')
} else {
alert(errorMessage);
}
});
promise
.catch(e => console.log(e.message));
if (email.length < 12){
document.getElementById("bademail").classList.remove("hide");
}
}
function logoutEvent(){
firebase.auth().signOut();
}
//Signing in with Google
function googleAuth(){
//Google Auth
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
}
//Signing in with github
function githubAuth(){
var provider = new firebase.auth.GithubAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a GitHub Access Token. You can use it to access the GitHub API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
}
//Password reset email
function sendPasswordReset() {
var email = document.getElementById('usr').value;
// [START sendpasswordemail]
firebase.auth().sendPasswordResetEmail(email).then(function() {
// Password Reset Email Sent!
// [START_EXCLUDE]
alert('We sent the email so check tu inbox');
// [END_EXCLUDE]
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode == 'auth/invalid-email') {
alert(errorMessage);
} else if (errorCode == 'auth/user-not-found') {
alert(errorMessage);
}
console.log(error);
// [END_EXCLUDE]
});
// [END sendpasswordemail];
}
//Realtime listener
firebase.auth().onAuthStateChanged(firebaseUser => {
if(firebaseUser){
console.log(firebaseUser);
document.getElementById("logout").classList.remove('hide');
window.location("index.html");
}else{
console.log("GET OUT KID");
document.getElementById("logout").classList.add('hide');
reset.addEventListener('click', sendPasswordReset, false);
google.addEventListener('click', googleAuth, false);
logine.addEventListener('click', loginEvent, false);
reggy.addEventListener('click', registerEvent, false);
logout.addEventListener('click', logoutEvent, false);
github.addEventListener('click', githubAuth, false);
}
});
要處理登錄和注銷,請始終使用onAuthStateChanged()
//Handle Account Status
firebase.auth().onAuthStateChanged(user => {
if(user) {
window.location = 'home.html';
}
});
當有人登錄, user
將用戶信息進行填充,你可以用它來重定向到另一個頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.