firebase

时间:2017-06-02 20:22:24

标签: database firebase ionic-framework

我想知道如何以我的离子应用程序中的用户身份登录,然后将一些数据保存在该用户的列表中。如果我以其他用户身份登录,我可以为该用户保存一些数据。

我已经获得了保存数据的代码,并以不同的用户身份登录,现在我只需要弄清楚如何将数据连接到特定的用户。

我保存数据的代码:

import { Component } from '@angular/core';
import { NavParams } from 'ionic-angular/navigation/nav-params';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import {NavController, AlertController, ActionSheetController} from "ionic-angular";

@Component({
selector: 'page-benchpress',
templateUrl: 'benchpress.html',
})

export class Benchpress { 

 benchrecords: FirebaseListObservable<any[]>;
constructor(private navParams: NavParams,
          public alertCtrl: AlertController,
          public navCtrl: NavController, db: AngularFireDatabase,
          public actionSheetCtrl: ActionSheetController){
this.benchrecords = db.list('/benchrecords');
} 
addRecord(){
let prompt = this.alertCtrl.create({
  title: 'Record',
  message: "Enter your new record!",
  inputs: [
    {
      name: 'repmax',
      placeholder: 'Your repmax'
    },
    {
      name: 'weight',
      placeholder: 'Weight lifted'
    },
    {
      name: 'date',
      placeholder: 'ex. 2017-01-04'
    }
  ],
  buttons: [
    {
      text: 'Cancel',
      handler: data => {
        console.log('Cancel clicked');
      }
    },
    {
      text: 'Save',
      handler: data => {
        this.benchrecords.push({
          repmax: data.repmax,
          weight: data.weight,
          date: data.date

        });
      }
    }
  ]
});
prompt.present();
}

我的登录代码:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { User } from '../../models/user';
import { AngularFireAuth } from 'angularfire2/auth';



@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class Login {

    user = {} as User;

  constructor(private afAuth: AngularFireAuth,
              public navCtrl: NavController,
              public navParams: NavParams
              ) {
  }

  async login(user: User){
    try {
      const result = this.afAuth.auth.signInWithEmailAndPassword(user.email, user.password)
      if(result) {
        this.navCtrl.setRoot('Home');
      }
    }
    catch(e){
      console.error(e);
    }
  }

  registerUser(){
    this.navCtrl.push('Register');
  }

}

我的Home.ts(用户将在登录后访问home.html):

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';
import { AngularFireAuth } from 'angularfire2/auth';


@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
})
export class Home {

  constructor(private afAuth: AngularFireAuth,
              private toast: ToastController,
              public navCtrl: NavController,
              public navParams: NavParams) {
  }

  ionViewDidLoad() {
    this.afAuth.authState.subscribe(data => {
      if (data && data.email && data.uid) {
        this.toast.create({
          message: `Welcome to APP_NAME, ${data.email}`,
          duration: 3000
        }).present();
      }
      else {
        this.toast.create({
          message: `Could not find authentication details`,
          duration: 3000
        }).present();
      }
    });
  }

  GoToBenchpress(){
    this.navCtrl.push('Benchpress');
  }

}

我真的很感谢你的帮助!!

0 个答案:

没有答案