Eddy
Eddy

Reputation: 381

Unable to Authenticate user in Ionic3

so Basically I am trying to do Components Communication with event Emitter. but I don,t know why I am getting this error

 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;
        }'.

My Login form component code is

 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);

   }}}

and in my login response interface file im exporting interface which will contain user Email and User ID and login response interface file code is

   export interface LoginResponse{

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

Upvotes: 0

Views: 111

Answers (2)

Moustafa Tlais
Moustafa Tlais

Reputation: 11

 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}
      }

Just in the try change it to this code and it will work. I face same issue.

Upvotes: 0

Matt McCutchen
Matt McCutchen

Reputation: 30959

Here is the type actually returned by this.afAuth.auth.signInWithEmailAndPassword (after you await the promise):

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

This doesn't match the type of LoginResponse.result:

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

Upvotes: 1

Related Questions