Firebase:电子邮件验证和用户登录,以便能够访问该页面

时间:2018-01-21 03:13:51

标签: javascript firebase angularjs-directive firebase-authentication email-verification

我创建了一个表单" main.html#!/ register"允许用户输入:firstname,lastname,email和login。一旦所有输入的电子邮件验证都被发送,那么他们就无法访问页面" main.html#!/ success"在验证电子邮件之前。

好消息:如果他们尝试从登录页面进入访问页面而未确认电子邮件,则他们将无法访问。 坏消息:注册后,如果他们进入" main.html#!/ success"他们将能够打开这个页面!!

用例: 用户无法访问" main.html#!/ success"没有任何注册 用户无法访问" main.html#!/ success"从登录页面" main.html#!/ login",如果他还没有验证他的电子邮件 问题是:用户能够访问" main.html#!/ success"注册后,没有电子邮件确认。

问题: 如何使用电子邮件验证条件 user.emailVerified 和用户auth $ requireSignIn()来允许访问页面" main.html#!/成功" ? 我已经设置了一个解决功能,以防止没有注册的任何访问。

这是我的代码 1解析功能: 身份验证是我创建的服务

 when('/success', {
  templateUrl: 'views/success.html',
  controller: 'SuccessController',
  resolve: {
    currentAuth: function(Authentication) {
    return Authentication.requireAuth();
    } //currentAuth
  }//resolve
}).

身份验证服务中的2个代码

requireAuth: function() {
    return auth.$requireSignIn();
}, //require Authentication

3-寄存器功能(它在服务中)

register: function(user) {   
  auth.$createUserWithEmailAndPassword(
    user.email,
    user.password
  ).then(function(regUser) {
    var regRef = ref.child('users')
      .child(regUser.uid).set({
        date: firebase.database.ServerValue.TIMESTAMP,
        regUser: regUser.uid,
        firstname: user.firstname,
        lastname: user.lastname,
        email: user.email 
      }); //userinfo
    regUser.sendEmailVerification().then(function() {
      // Email sent.
        alert("your Email is verified: " + regUser.emailVerified) ;
    }).catch(function(error) {
      // An error happened.
        alert(error);
    });
  }).catch(function(error) {
    $rootScope.message = error.message;
  }); //createUserWithEmailAndPassword
} //register

3-登录功能

login: function(user) {
            auth.$signInWithEmailAndPassword(
            user.email,
            user.password
          ).then(function(user) {
                if(user.emailVerified){
                  $location.path('/success');
                }
                else{
                $rootScope.message= "Please validate your registration first : "+user.emailVerified;
                }
          }).catch(function(error) {
            $rootScope.message = error.message;
          }); //signInWithEmailAndPassword
}, //login

1 个答案:

答案 0 :(得分:0)

您正试图在客户端强制执行此操作。相反,您应该对Firebase规则中经过验证的已登录用户实施访问权限。例如:

".write": "$user_id === auth.uid && auth.token.email_verified == true"