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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.