Skip to content

Commit 41d94ab

Browse files
committed
feat: handle auth errors in signup and login forms
1 parent 6b63cbd commit 41d94ab

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/app/components/login-form/login-form.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { AuthService } from '../../services/auth-service'
1515
import { Router } from '@angular/router'
1616
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'
1717
import { MatProgressBar } from '@angular/material/progress-bar'
18+
import { catchError, of } from 'rxjs'
1819

1920
@Component({
2021
selector: 'app-login-form',
@@ -63,12 +64,18 @@ export class LoginForm {
6364
this.isLoading.set(true)
6465
this.authService
6566
.login(this.formData())
66-
.pipe(takeUntilDestroyed(this.destroyRef))
67+
.pipe(
68+
takeUntilDestroyed(this.destroyRef),
69+
catchError(() => {
70+
this.isLoading.set(false)
71+
return of(null)
72+
}),
73+
)
6774
.subscribe((result) => {
6875
if (result) {
69-
this.isLoading.set(false)
7076
this.router.navigate(['/articles'])
7177
}
78+
this.isLoading.set(false)
7279
})
7380
}
7481
}

src/app/components/signup-form/signup-form.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { takeUntilDestroyed } from '@angular/core/rxjs-interop'
1010
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'
1111
import { MatIconModule } from '@angular/material/icon'
1212
import { MatProgressBarModule } from '@angular/material/progress-bar'
13+
import { catchError, of } from 'rxjs'
1314

1415
@Component({
1516
selector: 'app-signup-form',
@@ -51,12 +52,18 @@ export class SignupForm {
5152
this.isLoading.set(true)
5253
this.authService
5354
.signup({ password: this.password.value as string })
54-
.pipe(takeUntilDestroyed(this.destroyRef))
55+
.pipe(
56+
takeUntilDestroyed(this.destroyRef),
57+
catchError(() => {
58+
this.isLoading.set(false)
59+
return of(null)
60+
}),
61+
)
5562
.subscribe((result) => {
5663
if (result) {
57-
this.isLoading.set(false)
5864
this.router.navigate(['/user'])
5965
}
66+
this.isLoading.set(false)
6067
})
6168
}
6269
}

0 commit comments

Comments
 (0)