javascript - 使用 AngularJs 在 firebase 中创建用户

标签 javascript angularjs firebase angularfire firebase-authentication

我正在使用 AngularJs,我想在 firebase 中创建一个用户,然后我编写了以下代码,但它不起作用!

var myApp = angular.module('myApp', ['firebase', 'ngRoute']);
myApp.constant('FIREBASE_URL', 'MY_FIREBASE_URL');
myApp.controller('RegisterController', ['$scope', '$firebaseAuth', 'FIREBASE_URL', function($scope, $firebaseAuth, FIREBASE_URL) {
  $scope.headerTitle = "Registration Form";
  var ref = new Firebase(FIREBASE_URL);
  var auth = $firebaseAuth(ref);

  $scope.register = function() {
        auth.$createUser({
          email: $scope.user.email,
          password: $scope.user.password
        }).then(function(regUser) {
          $scope.message = $scope.user.firstName + " " + $scope.user.lastName + " " + "welcome !";
        }).catch(function(error) {
          $scope.message = error.message;
        });
  }
}]);

提交表单时出现以下错误:Projects created at console.firebase.google.com must use the new Firebase Authentication SDKs available from firebase.google.com/docs/auth/

最佳答案

对于 Firebase 3.x 和 AngularFire 2.x,您必须通过将代码段添加到 html 文件来初始化 Firebase SDK

1) 复制代码片段

  • 转到您的 Project Firebase 控制台 https://console.firebase.google.com/project/
  • 在项目的主页中,点击将 Firebase 添加到您的网络应用
  • 在弹出窗口中,复制代码片段,它可能如下所示:

    <script src="https://www.gstatic.com/firebasejs/3.1.0/firebase.js"></script>
    <script>
        // Initialize Firebase
        var config = {
            apiKey: " ** your key ** ",
            authDomain: "your-domain.firebaseapp.com",
            databaseURL: "https://your-domain.firebaseio.com",
            storageBucket: "your-domain.appspot.com",
        };
        firebase.initializeApp(config);
    </script>
    

2) 将代码片段粘贴到您的 html 文件中。

  • 将该代码段粘贴到 HTML 底部或其他脚本标记之前。

玩得开心!

关于javascript - 使用 AngularJs 在 firebase 中创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38170665/

相关文章:

javascript - 运动跟踪 javascript = html5/canvas 游戏输入/控制

javascript - 是否可以使用 Graphql 生成的模式作为流定义?

javascript - 使用 javascript 进行高效的实时 SVG 实体管理

javascript - 使用内部页面引用隐藏/取消隐藏 HTML 中的元素

angularjs - 带有 grunt 服务的 html5 模式不起作用

javascript - AngularJS 从服务器加载数据(设置)

javascript - 如何将 "reference"值插入到 firestore 中?

javascript - 如何从url中获取参数?

javascript - 如何使用 ng-repeat 显示来自 API 的所有值,其中我只需要显示所有对象中的一个键值?

javascript - React+Next.js+Firebase :Auth URL changes to username and password after signInWithEmailAndPassword