why login not working after form submission ?
Mohamed Seif Ben Salah
Posted on July 4, 2024
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);
}
`
Posted on July 4, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.