从实时Firebase数据库登录而不进行身份验证

时间:2020-01-21 09:22:08

标签: javascript typescript firebase firebase-realtime-database angular8

请帮助我从Realfire Firebase数据库登录而不进行身份验证。我想获取密钥并将密码与用户输入的密码进行比较。

我附上了一些代码,请仔细阅读。

loginComponent.html

<div class="form-group">
                <input type="text" id="empid" [(ngModel)]="empid" class="form-control m-2" placeholder="Employee Id">
            </div>
            <div class="form-group" *ngIf="!isForgotPassword">
                <input type="password" class="form-control m-2" [(ngModel)]="passwordInput" placeholder="Password" id="password">
            </div>
            <div class="form-group" *ngIf="!isForgotPassword">
            <span *ngIf="selectedVal == 'login'; else elseBlock">
                <button type="button" class="btn btn-orange btn-block m-2" (click)="loginUser()">Login</button>
            </span>

loginComponent.ts

 emailInput: string;
 passwordInput: string;
 isForgotPassword: boolean;
 empId : string;
 userDetails: any;

constructor(
 private userService: UserService,
 private router: Router
) {
 this.selectedVal = 'login';
 this.isForgotPassword = false;
}

loginUser(){
  this.userService.login(this.empId, this.passwordInput);
}

UserService.ts

import { Injectable } from '@angular/core';
import { AngularFireDatabase} from 'angularfire2/database';
import { User } from '../user.model'
import { Router } from '@angular/router';
import * as firebase from 'firebase';

@Injectable({
  providedIn: 'root'
  })
 export class UserService {
  user : User[];
  ref = 'Batch'
  batch = 'DEC2019'

  constructor(
     public af: AngularFireDatabase,
     public router: Router
  ) { }

  login(empId: string, password: string){
     return this.af.database.ref('Users/').child(empId).once('value')
    .then(snapshot=>{ 

   })
   .catch(err =>{
     console.log('Something went wrong',err.message)
   })
 }

1 个答案:

答案 0 :(得分:0)

您正在使用angularfire,可以在服务类中执行以下操作:

items: Observable<any[]>;
...

 login(empId: string){
     return this.itemRef = db.object('item/' + empId).snapshotChanges();
 }

然后在组件中,您可以subscribeobservable

loginUser(){
  this.userService.login(this.empId).subscribe(action => {
  console.log(action.type);
  console.log(action.key)
  console.log(action.payload.val())
 });
}

action.payload.val()将包含您的数据,如果您将action.payload.val().password作为数据库中的属性,则可以执行password来检索密码。然后,您可以检查检索到的两个密码是否等于passwordInput。您可能应该使用reactive-form,因为您可以轻松地使用它validation

https://angular.io/guide/reactive-forms#simple-form-validation

https://github.com/angular/angularfire/blob/master/docs/rtdb/objects.md

相关问题