Skip to content

Commit

Permalink
Merge pull request #124 from Apes2getherStrong/WTG-1-login-error-hand…
Browse files Browse the repository at this point in the history
…ling

WTG-1-login error handling
  • Loading branch information
niksuu authored Sep 13, 2024
2 parents 4684f96 + 0bc7af7 commit efee1bb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/app/user/log-in/log-in.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Component } from '@angular/core';
import {UserService} from "../user.service";
import {Component} from '@angular/core';
import {Router} from "@angular/router";
import {FormsModule} from "@angular/forms";
import {AuthService} from "../../auth/auth.service";
import {SnackbarService} from "../../shared/snackbar/snackbar.service";
import {SnackbarType} from "../../shared/snackbar/snackbar-type";

@Component({
selector: 'app-log-in',
Expand All @@ -17,16 +18,18 @@ export class LogInComponent {
username: string = '';
password: string = '';

constructor(private router: Router, private authService: AuthService) {}
constructor(private router: Router,
private authService: AuthService,
private snackbarService: SnackbarService,
) {}

onSubmit() {
this.authService.login(this.username, this.password).subscribe({
next: response => {
this.router.navigate(['/']);
},
error: err => {
console.error('Login failed', err);
// handle error (show message to user, etc.)
this.snackbarService.displaySnackbar("Wrong username or password", SnackbarType.DARK);
}
});
}
Expand Down
15 changes: 10 additions & 5 deletions src/app/user/register/register.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Component } from '@angular/core';
import {Component} from '@angular/core';
import {Router} from "@angular/router";
import {FormsModule} from "@angular/forms";
import {AuthService} from "../../auth/auth.service";
import {SnackbarService} from "../../shared/snackbar/snackbar.service";
import {SnackbarType} from "../../shared/snackbar/snackbar-type";

@Component({
selector: 'app-register',
Expand All @@ -18,11 +20,14 @@ export class RegisterComponent {
confirmPassword: string = '';
login: string = '';

constructor(private router: Router, private authService: AuthService) {}
constructor(private router: Router,
private authService: AuthService,
private snackbarService: SnackbarService,
) {}

onSubmit() {
if (this.password !== this.confirmPassword) {
console.error('Passwords do not match');
this.snackbarService.displaySnackbar('Passwords do not match',SnackbarType.DARK);
return;
}

Expand All @@ -34,11 +39,11 @@ export class RegisterComponent {

this.authService.register(user).subscribe({
next: response => {
this.snackbarService.displaySnackbar('Your registration was successful!',SnackbarType.DARK);
this.router.navigate(['/log-in']);
},
error: err => {
console.error('Registration failed', err);
// handle error (show message to user, etc.)
this.snackbarService.displaySnackbar('Username already exist',SnackbarType.DARK);
}
});
}
Expand Down

0 comments on commit efee1bb

Please sign in to comment.