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.t…


This content originally appeared on DEV Community and was authored by Mohamed Seif Ben Salah

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

}

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

}
`

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

}
`


This content originally appeared on DEV Community and was authored by Mohamed Seif Ben Salah


Print Share Comment Cite Upload Translate Updates
APA

Mohamed Seif Ben Salah | Sciencx (2024-07-04T01:02:54+00:00) why login not working after form submission ?. Retrieved from https://www.scien.cx/2024/07/04/why-login-not-working-after-form-submission/

MLA
" » why login not working after form submission ?." Mohamed Seif Ben Salah | Sciencx - Thursday July 4, 2024, https://www.scien.cx/2024/07/04/why-login-not-working-after-form-submission/
HARVARD
Mohamed Seif Ben Salah | Sciencx Thursday July 4, 2024 » why login not working after form submission ?., viewed ,<https://www.scien.cx/2024/07/04/why-login-not-working-after-form-submission/>
VANCOUVER
Mohamed Seif Ben Salah | Sciencx - » why login not working after form submission ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/04/why-login-not-working-after-form-submission/
CHICAGO
" » why login not working after form submission ?." Mohamed Seif Ben Salah | Sciencx - Accessed . https://www.scien.cx/2024/07/04/why-login-not-working-after-form-submission/
IEEE
" » why login not working after form submission ?." Mohamed Seif Ben Salah | Sciencx [Online]. Available: https://www.scien.cx/2024/07/04/why-login-not-working-after-form-submission/. [Accessed: ]
rf:citation
» why login not working after form submission ? | Mohamed Seif Ben Salah | Sciencx | https://www.scien.cx/2024/07/04/why-login-not-working-after-form-submission/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.