why login not working after form submission ?

samir_soupape

Mohamed Seif Ben Salah

Posted on July 4, 2024

why login not working after form submission ?

login.ts :
`async onSubmit(): Promise {
this.email = true;
this.pwd = true;
this.mailform = true;
this.isLoginInProgress.set(true);

this._authService
  .login(this.form.controls.email.value.trim(), this.form.controls.password.value.trim())
  .pipe(
    catchError((error: HttpErrorResponse) => {
      this.handleAuthError(error);
      return of(error);
    }),
    tap(response => this._handleLogin(response)),
    finalize(() => this.isLoginInProgress.set(false))
  )
  .subscribe({
    error: (error) => {
      console.error('Login error:', error);
    }
  });
Enter fullscreen mode Exit fullscreen mode

}

private _handleLogin(response: any): void {
if (!response?.user) return;
this.login = true;
const accessToken = response.user.accessToken;
const user_uid = response.user.uid;
localStorage.setItem('accessToken', accessToken);
localStorage.setItem('user_uid', user_uid);
window.location.reload();
this._router.navigateByUrl('/dashboard');

}

handleAuthError(err: HttpErrorResponse): void {
if (!err.error.code) return;

this.authError.set(true);
this.form.valueChanges
  .pipe(
    take(1),
    tap(() => this.authError.set(true))
  )
  .subscribe();
Enter fullscreen mode Exit fullscreen mode

}
`

login.html :
<form [formGroup]="form" (ngSubmit)="onSubmit()" class="form-detail">
</form>

authservice.ts :
`login(email: string, password: string): Observable {
console.log('Logging in user:', email);
const promise = signInWithEmailAndPassword(this.firebaseAuth, email, password)
.then(response => {
console.log('Login successful:', response);
return response;
})
.catch(error => {
console.error('Login error:', error);
throw error;
});

return from(promise);
Enter fullscreen mode Exit fullscreen mode

}
`

💖 💪 🙅 🚩
samir_soupape
Mohamed Seif Ben Salah

Posted on July 4, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related