2017-12-27 8 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認証では、ユーザーの電子メールアドレスを確認するメッセージを送信できますが、確認されていない電子メールアドレスを持つユーザーがサインインするのを防ぐことはできません。

特定のリソースを確認済みのメールアドレスを使用すると、そのリソースを保護できます。あなたがデータを保存するために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

関連する問題