【问题标题】:AngularJS with Firebase Authentication带有 Firebase 身份验证的 AngularJS
【发布时间】:2019-01-08 17:29:50
【问题描述】:

我正在尝试使用 AngularJS 和 firebase 身份验证(电子邮件和密码)创建用户登录页面。

我在网上查阅了一些教程并编写了代码,但似乎不起作用。

以下是我的代码。

1) page.html

  <ion-view id="page646">
  <ion-content padding="true" class="has-header">

  <form>
   <label class="item item-input">
    <span class="input-label">EMAIL</span>
    <input type="email" placeholder="" ng-model="user.email">
  </label>
  <label class="item item-input">
    <span class="input-label">PASSWORD</span>
    <input type="text" placeholder="" ng-model="user.pass">
  </label>

  <div style="margin-right:-20px;">
    <button style="left:-10px;" class="button button-positive button-full"ng-click="signIn()">SIGN IN</button>
  </div>
  </form>

</ion-content>
</ion-view> 

2) 索引.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<script src="lib/ionic/js/ionic.bundle.js"></script>

  <script src="https://www.gstatic.com/firebasejs/5.3.0/firebase.js"> 
  </script>
  <script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-app.js"> 
  </script>
  <script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-auth.js"> 
  </script>


   <!-- AngularFire -->
   <script 
   src="https://cdn.firebase.com/libs/angularfire/2.3.0/angularfire.min.js"> 
   </script>


<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->

<link href="lib/ionic/css/ionic.css" rel="stylesheet">

<style type="text/css">
  .platform-ios .manual-ios-statusbar-padding{
    padding-top:20px;
  }
  .manual-remove-top-padding{
    padding-top:0px; 
  }
  .manual-remove-top-padding .scroll{
    padding-top:0px !important;
  }
  ion-list.manual-list-fullwidth div.list, .list.card.manual-card-fullwidth {
    margin-left:-10px;
    margin-right:-10px;
  }
  ion-list.manual-list-fullwidth div.list > .item, .list.card.manual-card-fullwidth > .item {
    border-radius:0px;
    border-left:0px;
    border-right: 0px;
  }
  .show-list-numbers-and-dots ul{
    list-style-type: disc;
    padding-left:40px;
  }
  .show-list-numbers-and-dots ol{
    list-style-type: decimal;
    padding-left:40px;
  }
</style>

<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/routes.js"></script>
<script src="js/directives.js"></script>
<script src="js/services.js"></script>
<script src="lib/ngCordova/"></script>

</head>
<body ng-app="app" animation="slide-left-right-ios7">
<div>
<div>
<ion-nav-bar class="bar-stable">
  <ion-nav-back-button></ion-nav-back-button>
</ion-nav-bar>
<ion-nav-view></ion-nav-view>
</div>
</div>
</body>
</html>

3) 控制器.js

.controller('pASSWORDGMCtrl', ['$scope', '$stateParams', 'aaaService',
 function ($scope, $stateParams, aaaService) {
   $scope.user = {
        email:"",
        pass:"",
    }
   $scope.signIn = function(){
    aaaService.login($scope.user.email, $scope.user.pass);
   }

aaaService.status();


$scope.signOut = function(){
    aaaService.signout();
}
}])

4) Firebase-aaa.js

angular.module('firebaseAaa', ['firebase'])
.factory('aaaService', ['$firebaseArray','$firebaseAuth',
function($firebaseArray, $firebaseAuth){ 
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
};
if(!firebase.apps.length){
firebase.initializeApp(config);
}

var ref;
var adminArray; 

return{     

 status: function(){
 firebase.auth().onAuthStateChanged(function(user) {

if (user) {

  // User is signed in.

  // if(loggingIn){
  alert("Login success.");

    ref = firebase.database().ref().child("Admin/" + user.uid);

    adminArray = $firebaseArray(ref); 


   }

       // loggingIn=false;
   else {

  // No user is signed in.

}

});
},

login: function(email, pass) {

  firebase.auth().signInWithEmailAndPassword('abc@gmail.com', 'aaa').catch(function(error) {

    // Handle Errors here.

    var errorCode = error.code;

    var errorMessage = error.message;

    alert("Error signing in user: ", errorCode + " " + errorMessage);

  });

},


  signout: function() {

  firebase.auth().signOut().then(function() {

    // Sign-out successful.

    alert("You are now signed out.");
     // loggingIn= false;

  }).catch(function(error) {

    // An error happened.

    // Handle Errors here.

    var errorCode = error.code;

    var errorMessage = error.message;

    alert("Error signout: ", errorCode + " " + errorMessage);

  });

  },
  }

  }])

当我试用代码时,我得到“未登录”。 我在看firebase auth教程,有一部分他没有启用电子邮件和密码验证。在他尝试代码时,他在控制台中收到一个错误,指出尚未启用 Firebase 电子邮件和密码验证。 我尝试禁用我的,希望看到同样的错误,但结果并非如此。它只说“未登录”。 我找不到它的问题。 我的问题是我如何获得成功,即使是 signInWithEmailAndPassword。

【问题讨论】:

  • 您可以尝试将您的 onAuthStateChanged 移出您的登录功能
  • @Rsmusic 最初我就是这样做的,但它没有用。它只是给出了一个警告'UNseccessful'。
  • 你的配置变量填对了吗?
  • var config = { apiKey: "", authDomain: "", databaseURL: "", projectId: "", storageBucket: "", messingsSenderId: "" };
  • @Rsmusic 我的 Firebase 中有两个电子邮件和密码用户。只有当我将我的电子邮件和密码设置为我的 firebase 用户时,我才能登录。请参阅 Firebase-aaa.js - firebase.auth().signInWithEmailAndPassword('abc@gmail.com', ' aaa') 如果我将其更改为 firebase.auth().signInWithEmailAndPassword(email, pass) 就像教程中显示的那样,它不会让我的用户登录。

标签: angularjs firebase-authentication


【解决方案1】:

您需要将这些位移出登录功能

 $scope.user = {
        email:"",
        pass:"",
    }

它将对象“user”重新初始化为空值,加上 onAuthStateChanged 也应该在外面。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 2019-02-26
  • 1970-01-01
  • 2016-10-05
  • 2021-10-16
  • 2021-10-30
相关资源
最近更新 更多