2017-12-27 218 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

相關問題