Firebase身份验证无尽循环

时间:2017-05-12 21:35:01

标签: javascript firebase firebase-authentication processing.js

我使用Firebase身份验证编写了一个程序,如果用户未登录,我想重定向到Google SignIn页面,但如果用户已登录则不会重定向。除了返回时,一切正常Google登录后的页面,仅在几秒钟后重定向回SignIn。这是我的代码:

$(document).ready(function() {
var provider = new firebase.auth.GoogleAuthProvider();
var uid;
var user = firebase.auth().currentUser;
var xv = 0;
var yv = 0;
var xpos = 200;
var ypos = 200;
var changeRef = firebase.database().ref();
var keys = [];

if (user != null) {
uid = currentUser.uid;
firebase.database().ref('users/' + uid).set({
xpos: xpos,
ypos: ypos
});
} else {
firebase.auth().signInWithRedirect(provider);
firebase.auth().getRedirectResult().then(function(result) {
if (result.credential) {
// 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.
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;
// ...
});
}
var canvas = document.getElementById("canvas");
var processingInstance = new Processing(canvas, sketchProc);

});

1 个答案:

答案 0 :(得分:1)

当用户登录时,你同时调用signInWithRedirect和getRedirectResult,这就是你陷入无限循环的原因:

} else {
  firebase.auth().signInWithRedirect(provider);
  firebase.auth().getRedirectResult().then(function(result) {

您可以将signInWithRedirect附加到单击或将其从此块中拉出(用户未登录)块。