使用angular和firebase中的电子邮件进行身份验证。

时间:2017-12-23 10:57:12

标签: angular firebase firebase-authentication

我是这个角度的新手,但不知何故,我在角度上玩了大约一个月。我开发了一个包含登录,注册表单的Web应用程序。我可以轻松注册和登录。我使用firebase来存储数据和进行身份验证。

如果新用户使用我的页面,他们应首先注册然后登录,这是一个常见的过程。

现在面临一种情况,600名员工已经拥有了emailid和密码,但没有在我的应用程序中注册。这600名员工必须直接登录,而无需在我的应用程序中注册。 怎么做。?

是否有任何方法可以实现将这600个用户的名称和密码导入firebase或实现此目的的任何想法。?

登录-page.component.html

<div class="col-md-12">
<form [formGroup]="signin" (ngSubmit)=signInNewtt()>
 <div class="form-group">
    <label class="center-block">Email:
      <input class="form-control" formControlName="email" >
    </label>
  </div>
  <div class="form-group">
    <label class="center-block">Password:
      <input type="password" class="form-control" formControlName="password" >
    </label>
  </div>
<button type="submit" class="btn"  [disabled]="!signin.valid">submit</button>
</form>
{{signin.value | json}}
{{signin.status | json}}
</div>

登录-page.component.ts

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

@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.name,values.email)
    firebase.auth().createUserWithEmailAndPassword(values.name,values.email)
    .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');
  }
});
}
signInNewtt(){
  let values = this.signin.value;
  //Problem here
  admin.auth().createUser({
    email: values.email,
    password: values.password
  })
  .then(function(userRecord) {
    See the UserRecord reference doc for the contents of userRecord.
    console.log("Successfully created new user:", userRecord.uid);
  })
  .catch(function(error) {
    console.log("Error creating new user:", error);
  });
}

nospaceValidator(control: AbstractControl): { [s: string]: boolean } {
  let re = / /;
  if (control.value && control.value.match(re)) {
    return { nospace: true };
  }
  }
}

1 个答案:

答案 0 :(得分:1)

要让这些用户加入Firebase身份验证,您可以create each of them with the Admin SDKimport them using the command-line interface