gpt4 book ai didi

javascript - 无论如何如何让用户使用 Firebase 登录?

转载 作者:行者123 更新时间:2023-11-29 21:07:55 25 4
gpt4 key购买 nike

我正在 Ionic Framework 下开发一个应用程序,我也在使用 Firebase。

现在,在几个小时后或应用程序崩溃或重新启动设备时,身份验证会丢失。

无论发生什么情况,我怎样才能让我的用户始终登录?(例如 Facebook)

这是我在 login.html 页面中的登录 Controller :

.controller('loginController',['$scope', '$firebaseArray', 'CONFIG', '$document', '$state', function($scope, $firebaseArray, CONFIG, $document, $state) {
// Perform the login action when the user submits the login form
$scope.doLogin = function(userLogin) {
if($document[0].getElementById("user_name").value != "" && $document[0].getElementById("user_pass").value != ""){
firebase.auth().signInWithEmailAndPassword(userLogin.username, userLogin.password).then(function() {
var userId = firebase.auth().currentUser.uid;
firebase.database().ref('accounts/' + userId + '/currentBusiness/').update({
name: "No current business arround",
description: "Seems there's nothing arround...",

})
$state.go("tab.favorites");

}, function(error) {
// An error happened.
var errorCode = error.code;
var errorMessage = error.message;

if (errorCode === 'auth/invalid-email') {
alert('Enter a valid email.');
return false;
}else if (errorCode === 'auth/wrong-password') {
alert('Incorrect password.');
return false;
}else if (errorCode === 'auth/argument-error') {
alert('Password must be string.');
return false;
}else if (errorCode === 'auth/user-not-found') {
alert('No such user found.');
return false;
}else if (errorCode === 'auth/too-many-requests') {
alert('Too many failed login attempts, please try after sometime.');
return false;
}else if (errorCode === 'auth/network-request-failed') {
alert('Request timed out, please try again.');
return false;
}else {
alert(errorMessage);
return false;
}
});
}else{
alert('Please enter email and password');
return false;
}//end check client username password
};// end $scope.doLogin()
}])

最佳答案

我会回答我自己的问题,因为我找到了解决方案:

在我的例子中,你必须使用这段代码:

firebase.auth().onAuthStateChanged(function(user) {
if (user) {
$state.go("tab.favorites");
// User is signed in.
} else {
// No user is signed in.
}
});

关于javascript - 无论如何如何让用户使用 Firebase 登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43073371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com