无法在Ionic3中对用户进行身份验证

时间:2018-08-20 10:37:06

标签: typescript ionic3 angular-components eventemitter

因此,基本上,我试图与事件Emitter进行组件通信。 但我不知道为什么会收到这个错误

 Type '{ result: UserCredential; }' is not assignable to type 'LoginResponse'. Types of property 'result' are
        incompatible. Type 'UserCredential' has no properties in common with type '{ email?: string; uid?: string;
        }'.

我的登录表单组件代码为

 import { Component,EventEmitter,Output } from '@angular/core';
 import { NavController} from  'ionic-angular';
 import { AngularFireAuth} from 'angularfire2/auth';
 import { Account } from '../../model/account/account.interface';
 import { LoginResponse} from '../../model/login/login.response.interface';

 @Component({
 selector: 'app-login-form',
 templateUrl: 'login-form.components.html'
 })
 export class LoginFormComponent {

 account = {} as Account;
 @Output() loginStatus : EventEmitter<LoginResponse>;
 constructor(private afAuth:AngularFireAuth, private navCtrl:NavController) 
 {
 this.loginStatus = new EventEmitter<LoginResponse>();
 }

 async login(){

 try {

  const result : LoginResponse = { result :  await 

  this.afAuth.auth.signInWithEmailAndPassword(
   this.account.email,this.account.password)
  } 
  this.loginStatus.emit(result);
} catch (e) {
  console.error(e);
  const error : LoginResponse = {
    error : e
  }
  this.loginStatus.emit(error);

   }}}

在我的登录响应界面文件的即时消息导出界面中,该界面将包含用户电子邮件和用户ID,并且登录响应界面文件的代码为

   export interface LoginResponse{

     result?: {
    email?: string;
    uid?: string;
      }
      error?: {
      code?:string;
      message?: string;
     }
      }

2 个答案:

答案 0 :(得分:1)

Herethis.afAuth.auth.signInWithEmailAndPassword实际返回的类型(在您等待诺言之后):

type UserCredential = {
  additionalUserInfo?: firebase.auth.AdditionalUserInfo | null;
  credential: firebase.auth.AuthCredential | null;
  operationType?: string | null;
  user: firebase.User | null;
};

这与LoginResponse.result的类型不匹配:

{
  email?: string;
  uid?: string;
}

答案 1 :(得分:0)

 try {
      let res = await this.afAuth.auth.signInWithEmailAndPassword(this.account.email, this.account.password);
      const result: LoginResponse = { 
        result: {email: res.user.email, uid: res.user.uid}
      }

只需尝试将其更改为此代码,它就会起作用。我遇到同样的问题。

相关问题