使用AngularJs进行用户验证

时间:2016-01-08 12:43:10

标签: angularjs authentication firebase-authentication

我刚开始使用angularfire Auth!当我登录时,我遇到了这个问题。如果你们对如何制作电子邮件注册表有任何建议,我会非常高兴听到这个。这是我收到的错误:

  

错误:Firebase.authWithPassword失败:第一个参数必须是有效对象。

这是我的 的的script.js:

var app = angular.module('LoginApp', ["firebase"])
app.controller('AuthCtrl', [
  '$scope', '$rootScope', '$firebaseAuth', function($scope, $rootScope, $firebaseAuth) {

  var ref = new Firebase('https://uniquecoders.firebaseio.com/')
  $scope.auth = $firebaseAuth(ref);

  $scope.signIn = function(){
    var ref = new Firebase('https://uniquecoders.firebaseio.com/')
    ref.authWithPassword('email', {
        email: $scope.signInEmail,
        password: $scope.signInPassword
    }, function(error,userData){
        if(error = 'INVALID_EMAIL'){
            alert("The Email you entered is Invalid")
        }
        else if(error = 'INVALID_PASSWORD'){
            alert("The Password you entered is Invalid")
        }
        else{
            console.log(error)
        }
    })

  }
  }]
  );

和我们的 Index.html

<!DOCTYPE html>
<html lang="en-US">
<head>
        <script src="jquery.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.js"></script>
    <script src="https://cdn.firebase.com/js/client/2.2.4/firebase.js"></script>
    <script src="https://cdn.firebase.com/libs/angularfire/1.1.3/angularfire.min.js"></script>
    <script src="https://cdn.firebase.com/js/simple-login/1.6.2/firebase-simple-login.js"></script>
    <script src = "http://s.codepen.io/assets/libs/modernizr.js"></script>
    <script type="text/javascript" src="script.js"></script>

    <meta charset="UTF-8">
    <meta name="X-UA-Compatible" content="">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title>Login - UniqueCoders</title>
    <link rel="icon" href="favicon.ico">
    <link rel="stylesheet" href="main.css">
</head>
<body ng-app="LoginApp">
    <div id="page">
        <header class="container">
            <div class="row">
                <img src="https://lh5.googleusercontent.com/-UXr_le-DEc4/VogTLE2yDfI/AAAAAAAAAfI/9RycRDVGNu8/w993-h207-no/uniquecoders.png" alt="UniqueCoders">
            </div>
        </header>
        <main class="container">
            <div class="row login">
                <form ng-controller="AuthCtrl">
                    <input type="email" name="email" placeholder="Email" ng-model = "signInemail" id="email">
                    <input type="password" name="password" ng-model = "signInpassword" placeholder="Password" id="password">
                    <input type="submit" value="Login" ng-click = "signIn()">
                </form>
            </div>
        </main>
        <footer class="container">
            <div class="row">
                <small>&copy; 2016. Unique Coders</small>
            </div>
        </footer>
    </div>
    <!-- Scripts at the end for faster loading -->

</body>
</html>

1 个答案:

答案 0 :(得分:1)

从这里的文档是我看到的: https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-users-and-authentication

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.authWithPassword({
    "email": "yourname@email.com",
    "password": "password"
  }, function(error, authData) {
    if (error) {
        console.log("Login Failed!", error);
    } else {
        console.log("Authenticated successfully with payload:", authData);
    }
});

您可以在上面更改它并检查它是否有效。这是一个关于如何使用angularjs进行身份验证的简洁示例。

http://jasonwatmore.com/post/2015/03/10/AngularJS-User-Registration-and-Login-Example.aspx