在ionic 2中的firebase中注册并创建新用户

时间:2018-02-27 11:53:32

标签: typescript firebase firebase-realtime-database ionic2 firebase-authentication

我正在开发一个离子2项目我想在firebase auth中添加注册并使用电子邮件和密码创建一个新用户

我的register.html页面是给定的

<ion-content padding>
  <form [formGroup]="registerForm" (submit)="doRegister()" novalidate>
    <ion-item margin-bottom>
      <ion-input
        type="text"
        formControlName="name" placeholder="Name"></ion-input>
    </ion-item>
    <ion-item margin-bottom>            
      <ion-input
        type="email"
        formControlName="email" placeholder="Email"></ion-input>
    </ion-item>
    <ion-item margin-bottom>
      <ion-input
        type="password"
        formControlName="password" placeholder="Password"></ion-input>
    </ion-item>
    <button ion-button block type="submit">
      Register
    </button>

register.ts如下

export class RegisterPage {

  public registerForm;
  submitAttempt: boolean = false;
  loading: any;

  constructor(public navCtrl: NavController, public authService: AuthProvider, public navParams: NavParams, public formBuilder: FormBuilder,public alertCtrl: AlertController, public loadingCtrl: LoadingController) {
    let EMAIL_REGEXP = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
    this.registerForm = formBuilder.group({
      email: ['', Validators.compose([Validators.required, Validators.pattern(EMAIL_REGEXP)])],
      password: ['', Validators.compose([Validators.minLength(6), Validators.required])],
      name: ['', Validators.compose([Validators.minLength(6), Validators.required])]
    });
  }

  doRegister() {
    firebase.auth().createUserWithEmailAndPassword(email, password).then((auth : any) =>
    {
      this.navCtrl.setRoot(HomePage);
      console.log(email,password);
    })
    .catch(function(error) {
      var errorCode = error.code;
      var errorMessage = error.message;
      if (errorCode == 'auth/weak-password') {
        alert('The password is too weak.');
      } else {
        alert(errorMessage);
      }
      console.log(error);         
    });
  }
}

但它不会创建让我跟踪错误的用户。

There is no user record corresponding to this identifier. The user may have been deleted.

任何帮助?

1 个答案:

答案 0 :(得分:0)

所有Firebase注册方法仅将用户保存在“身份验证”选项卡下。成功注册后,您必须通过调用set方法来专门创建用户

firebase.database().ref('users/' + userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl
  });

Read and Write Data on the Web

上了解有关创建和更新的更多信息。

注意:此URL供Firebase Web使用。确保从左侧菜单切换到所需的标签(iOS,Android以及特定于语言)