使用angularjs在Firebase中创建用户

时间:2019-01-20 15:29:19

标签: angularjs firebase firebase-authentication

您好,我正在尝试使用angularjs学习Firebase。

索引html

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-route.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-animate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-route.min.js"></script>

<!-- Firebase Starting -->
<script src="https://www.gstatic.com/firebasejs/5.8.0/firebase.js"></script>
<script>
    // Initialize Firebase
    var config = {
        apiKey: "AIzaSyDbul8FNC4posBAaNKhO8kiz2mdrkD539U",
        authDomain: "angularregistration-b9dec.firebaseapp.com",
        databaseURL: "https://angularregistration-b9dec.firebaseio.com",
        projectId: "angularregistration-b9dec",
        storageBucket: "angularregistration-b9dec.appspot.com",
        messagingSenderId: "1085945127139"
    };
    firebase.initializeApp(config);
</script>

注册控制器

myApp.controller('RegistrationController',
['$scope', '$firebase', '$firebaseAuth',
    function ($scope, $firebase, $firebaseAuth) {

        var ref = firebase.database().ref();
        var auth = $firebaseAuth();

        $scope.login = function () {
            $scope.message = "Welcome " + $scope.user.email;
        };

        $scope.register = function () {

            auth.$createUserWithEmailAndPassword(
                $scope.user.email,
                $scope.user.password
            ).then(function (regUser) {
                $scope.message = "Hi " + $scope.user.firstname + " thanks for registering !";

            }).catch(function (error) {
                $scope.message = error.message;
            }); //createUserWithEmailAndPassword
        }; //register
    }]); //controller

注册HTML

<h3 class="text-center">Register Page</h3>
<form name="myForm" novalidate ng-submit="register()">
    <div class="form-group">
        <label for="First Name">First Name</label>
        <input ng-model="user.firstname" type="text" class="form-control" id="firstname" placeholder="First name">
    </div>
    <div class="form-group">
        <label for="First Name">Last Name</label>
        <input type="text" class="form-control" id="lastname" placeholder="Last name">
    </div>
    <div class="form-group">
        <label for="exampleInputPassword1">Password</label>
        <input type="text" class="form-control" id="exampleInputPassword1" placeholder="Password">
    </div>

    <div class="form-group">
        <label for="exampleInputEmail1">Email address</label>
        <input ng-model="user.email" type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"
            placeholder="Enter email" ng-required="true">
    </div>
    <button type="submit" class="btn btn-primary" ng-disabled="myForm.$invalid">Register</button>
</form>
<a href="#!login">Back to Login</a>

<p ng-show="message">{{message}}</p>

我要尝试做的就是尝试将用户注册到Firebase。在我的index.htm中,我还要先加载angularjs,然后再加载firebase,然后再加载angularjsfire。每次尝试创建用户时,都会给我

错误
  

{代码:“ auth / argument-error”,消息:“ createUserWithEmailAndPassword失败:第二个参数” password“必须是有效的字符串。”}

我试图在stackoverflow中看到另一个答案,但是找不到我要做错的事情。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

重新访问表单并读取模板中的所有绑定值后,我忘记了绑定密码字段中的值。一旦我绑定了密码字段,它就可以正常工作。

go build

感谢@Frank van Puffelen的快速建议