【问题标题】:Ionic 4 & Firebase integrationIonic 4 和 Firebase 集成
【发布时间】:2020-06-09 19:51:50
【问题描述】:

所以,在 ionic 4 firebase 集成方面,我是个菜鸟。基本上我已经设法让firebase身份验证工作,但问题是我似乎无法将注册页面中的用户输入保存到firebase数据库。我需要它来工作,因为我想将用户链接到他/她的清单。

这是我的注册页面.ts

import { Component, OnInit } from '@angular/core';
import { Router } from "@angular/router";
import { Platform, AlertController } from '@ionic/angular';
import { LoadingController, ToastController } from '@ionic/angular';
import { AngularFireAuth } from '@angular/fire/auth';


//disable side menu
import { MenuController } from '@ionic/angular';


@Component({
  selector: 'app-signup',
  templateUrl: './signup.page.html',
  styleUrls: ['./signup.page.scss'],
})
export class SignupPage implements OnInit {

  email: string = '';
  password: string = '';
  error: string = '';
  username: string = '';

  constructor(
    private fireauth: AngularFireAuth,
    public router: Router,
    public menuCtrl: MenuController,
    private toastController: ToastController, 
    private platform: Platform, 
    public loadingController: LoadingController,
    public alertController: AlertController
  ) { }

  async openLoader() {
    const loading = await this.loadingController.create({
      message: 'Please Wait ...',
      duration: 2000
    });
    await loading.present();
  }
  async closeLoading() {
    return await this.loadingController.dismiss();
  }

  signup() {
    this.fireauth.auth.createUserWithEmailAndPassword(this.email, this.password)
      .then(res => {
        if (res.user) {
          console.log(res.user);
          this.updateProfile();
        }
      })
      .catch(err => {
        console.log(`login failed ${err}`);
        this.error = err.message;
      });
  }

  updateProfile() {
    this.fireauth.auth.onAuthStateChanged((user) => {
      if (user) {
        console.log(user);
        user.updateProfile({
          displayName: this.username
        })
          .then(() => {
            this.router.navigateByUrl('/login');
          })
      }
    })
  }

  async presentToast(message, show_button, position, duration) {
    const toast = await this.toastController.create({
      message: message,
      showCloseButton: show_button,
      position: position,
      duration: duration
    });
    toast.present();
  }


  ionViewWillEnter (){
    this.menuCtrl.enable(false);
  }

  ngOnInit(){}

}

【问题讨论】:

  • 嗨,@Oddie123,欢迎来到 Stack Overflow。您能否提供当前用于将用户输入保存到 Firebase 数据库的代码?这将有助于我们评估您当前代码可能存在的问题,并提供更有针对性的指导。
  • 嗨@JeremyCaney,我已经更新了我的问题:)

标签: firebase ionic-framework firebase-realtime-database firebase-authentication ionic4


【解决方案1】:

实际上,您并没有使用 firebase 数据库。 user.updateProfile 方法将 displayName 属性存储在 Firebase 身份验证系统中。该信息保存在 Firebase 身份验证系统中。

【讨论】:

  • 哦,我明白了,有没有办法可以将它们存储到 firebase 数据库中,因为我看到的大多数教程都使用 firebase auth 进行注册和登录页面:/
  • Firebase Auth 仅用于为您处理身份验证,Firebase 提供的另一项服务是数据库,因此如果您需要存储信息,您也可以使用 Firebase 数据库,同时使用这两种服务。
  • 你能指导我完成这个吗?那么我是否只是将“this.fireauth.auth.onAuthStateChanged((user)”中的fireauth更改为firestore?
  • 嘿,我找到了这篇博文 (positronx.io/…) 我已经尝试过了,但我仍然没有在我的 firebase 控制台中看到用户的输入。
  • 在您的 Firebase 控制台中仍然看不到用户的输入是什么意思?身份验证控制台?还是进入数据库控制台?
猜你喜欢
  • 1970-01-01
  • 2018-05-12
  • 2019-12-29
  • 1970-01-01
  • 2019-09-18
  • 2019-06-01
  • 2019-03-03
  • 1970-01-01
  • 2019-06-04
相关资源
最近更新 更多