2017-12-27 262 views
0

我有一个简单的表单来注册和登录,使用firebase存储和验证。注册用户可以通过Firebase收到确认邮件。但未经验证的用户也可以登录,这里做错了什么。未经认证的用户也可以登录。

登录-page.component.ts

import { Component, OnInit } from '@angular/core'; 
import { FormBuilder, FormGroup, Validators, AbstractControl } from '@angular/forms'; 
import * as firebase from 'firebase'; 

@Component({ 
    selector: 'app-login-page', 
    templateUrl: './login-page.component.html', 
    styleUrls: ['./login-page.component.css'] 
}) 
export class LoginPageComponent implements OnInit { 
    signin:FormGroup; 
    constructor(private fb: FormBuilder) { 
    this.signin = fb.group({ 
     email : [null, Validators.compose([Validators.required, this.nospaceValidator])], 
     password : [null, Validators.required] 
    }); 
    } 

    ngOnInit() { 
    } 

signUp(){ 
    let values = this.signin.value; 
    console.log(values.email,values.password) 
    firebase.auth().createUserWithEmailAndPassword(values.email,values.password) 
    .then(
     function(user){ 
     if(user && user.emailVerified === false){ 
     user.sendEmailVerification() 
     .then(function(){ 
      console.log("email verification sent to user"); 
     }); 
     } 
    } 
    ) 
    .catch(
     function(error) { 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    console.log(errorMessage) 
}); 
} 
signIn(){ 
firebase.auth().onAuthStateChanged(
    function(user) { 
    if (user.emailVerified) { 
    console.log('Email is verified'); 
    } 
    else { 
    console.log('Email is not verified'); 
    } 
}); 
} 
} 
+0

我看到您创建用户的位置,但您实际上在哪里签名? – DoesData

回答

0

你没有做错什么。尽管Firebase身份验证允许您发送邮件来验证用户的电子邮件地址,但没有任何事情可以阻止具有未经验证的电子邮件地址的用户登录。

如果您希望某些资源只对具有经过验证的电子邮件地址,您会保护这些资源例如,如果您使用的火力地堡数据库来存储数据,你能在那里访问的数据仅为用户与验证的电子邮件地址:

{ 
    "rules": { 
    ".read": "auth.token.email_verified == true" 
    } 
} 

更多关于这一点,在这里看到我的回答:How do I lock down Firebase Database to any user from a specific (email) domain?

0

添加到Franks答案您还可以使用此代码来阻止未验证其电子邮件的用户登录您的应用。

if (user.emailVerified) { 
    // sign the user into your app 
} 
else { 
    // alert the user that the cannot sign in until they verify their email 
    // You probably want to offer to send another email verification here too 
} 
+0

你修改了什么? – codedamn

+0

什么都没有。我说你可以使用它,如果在那里检查,以防止用户登录,如果他们的电子邮件未验证。发布您的当前登录功能,我可以准确告诉您我的意思。 – DoesData

相关问题