angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

signInWithEmailAndPassword from '@angular/fire/auth' does not hit auth emulator

Open taromurao opened this issue 4 years ago • 2 comments

I am using Angularfire 7.2.0 and trying to implement firstly email/password authentication with "@angular/fire/auth" (not compat). Though it works against real server https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword, under test/dev environment, providing USE_AUTH_EMULATOR in app.module.ts does not make the app hit emulator. My app.module.ts looks now like

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
import { provideAnalytics, getAnalytics, ScreenTrackingService, UserTrackingService } from '@angular/fire/analytics';
import { provideAuth, getAuth } from '@angular/fire/auth';
import { provideFirestore, getFirestore } from '@angular/fire/firestore';
import { provideFunctions, getFunctions } from '@angular/fire/functions';
import { provideMessaging, getMessaging } from '@angular/fire/messaging';
import { providePerformance, getPerformance } from '@angular/fire/performance';
import { provideRemoteConfig, getRemoteConfig } from '@angular/fire/remote-config';
import { provideStorage, getStorage } from '@angular/fire/storage';
import { MatToolbarModule } from '@angular/material/toolbar';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MAT_SNACK_BAR_DEFAULT_OPTIONS } from '@angular/material/snack-bar';
import { USE_EMULATOR as USE_AUTH_EMULATOR } from '@angular/fire/compat/auth';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { MapModule } from './map/map.module';
import { NotFoundModule } from './not-found/not-found.module';
import { environment } from '../environments/environment';
import { LoginModule } from './login/login.module';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    MatToolbarModule,
    MapModule,
    NotFoundModule,
    LoginModule,
    provideFirebaseApp(() => initializeApp(environment.firebase)),
    provideAnalytics(() => getAnalytics()),
    provideAuth(() => getAuth()),
    provideFirestore(() => getFirestore()),
    provideFunctions(() => getFunctions()),
    provideMessaging(() => getMessaging()),
    providePerformance(() => getPerformance()),
    provideRemoteConfig(() => getRemoteConfig()),
    provideStorage(() => getStorage()),
    BrowserAnimationsModule,
  ],
  providers: [
    ScreenTrackingService,
    UserTrackingService,
    {
      provide: USE_AUTH_EMULATOR,
      useValue: environment.useEmulators ?
        ['http://localhost:9099'] : undefined
    },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

LoginComponent looks like

import { Component, OnInit } from '@angular/core';
import { getAuth, signInWithEmailAndPassword } from '@angular/fire/auth';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Router } from '@angular/router';

import { Message } from '../message';
import { PATH_MAP } from '../constants';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.less']
})
export class LoginComponent {
  loginForm: FormGroup = new FormGroup({
    email: new FormControl(
      '',
      [
        Validators.required,
        Validators.email,
      ]),
    password: new FormControl(
      '',
      [
        Validators.required,
      ]),
  });

  constructor(
    private readonly router: Router,
    private readonly snackbar: MatSnackBar,
  ) { }

  async onSubmit(loginForm: FormGroup) {
    const auth = getAuth();
    const { email, password } = loginForm.value;
    try {
      const { user } = (await signInWithEmailAndPassword(auth, email, password));
      this.router.navigate([PATH_MAP]);
    } catch (error) {
      this.snackbar.open(Message.WrongCredentials);
    }
  }
}

As you can see, I applied the solution of #3036, but it still does not hit the emulator. It looks at the first look wrong to import USE_EMULATOR from @angular/fire/compat/auth module since I am using non-compat version of AuthGuard, i.e. from @angular/fire/auth-guard module. Has there been updates during the meantime? How can we let auth-related function calls, e.g. signInWithEmailAndPassword from '@angular/fire/auth' module, hit the emulator?

Version info

Angular: 13.1.0

Firebase: 9.4.0

AngularFire: 7.2.0

Expected behavior

Auth request should hit the emulator.

Actual behavior

It hits https://identitytoolkit.googleapis.com.

Request URL: https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?...

taromurao avatar Dec 27 '21 18:12 taromurao

This issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot avatar Dec 27 '21 18:12 google-oss-bot

In the sample modular app there is an example of connecting to the emulator. It works with @angular/fire v7.2.1.

taromurao avatar Feb 19 '22 18:02 taromurao