From 5533f7100b8e184190dfe63efae2029f5677aa81 Mon Sep 17 00:00:00 2001 From: Roland Schneider Date: Sun, 26 Sep 2021 12:45:02 +0200 Subject: [PATCH] improve login error handling --- customer/app/src/app/_helpers/error.interceptor.ts | 11 ++++------- customer/app/src/app/pages/login/login.component.ts | 13 ++++++++++--- customerapi/controllers/UserController.php | 8 ++------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/customer/app/src/app/_helpers/error.interceptor.ts b/customer/app/src/app/_helpers/error.interceptor.ts index 38f27aa..d713027 100644 --- a/customer/app/src/app/_helpers/error.interceptor.ts +++ b/customer/app/src/app/_helpers/error.interceptor.ts @@ -1,7 +1,7 @@ import { Injectable } from "@angular/core"; import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from "@angular/common/http"; -import {EMPTY, Observable, throwError} from "rxjs"; -import { catchError } from "rxjs/operators"; +import { Observable, throwError} from "rxjs"; +import {catchError} from "rxjs/operators"; import { AuthenticationService} from "../services/authentication.service"; import {NavigationService} from "../services/navigation.service"; @@ -15,7 +15,8 @@ All other errors are RE-THROWN to be caught by the calling service so an alert c @Injectable() export class ErrorInterceptor implements HttpInterceptor{ constructor(private authenticationService: AuthenticationService, - private navigationService: NavigationService){} + private navigationService: NavigationService + ){} intercept(request: HttpRequest, next: HttpHandler): Observable>{ return next.handle(request) @@ -25,11 +26,7 @@ All other errors are RE-THROWN to be caught by the calling service so an alert c // auto logout on unauthorized response this.authenticationService.logout(); this.navigationService.navigateToLogin(); - // location.reload(true); - // return EMPTY; } - // const error = err.error.message || err.statusText; - // return throwError(error); return throwError(err); }) diff --git a/customer/app/src/app/pages/login/login.component.ts b/customer/app/src/app/pages/login/login.component.ts index febd9b9..039b822 100644 --- a/customer/app/src/app/pages/login/login.component.ts +++ b/customer/app/src/app/pages/login/login.component.ts @@ -3,6 +3,7 @@ import {FormBuilder, FormGroup, Validators} from "@angular/forms"; import {ActivatedRoute, Router} from "@angular/router"; import {AuthenticationService} from "../../services/authentication.service"; import { first } from 'rxjs/operators'; +import {ToastrService} from "ngx-toastr"; @Component({ selector: 'app-login', @@ -20,7 +21,9 @@ export class LoginComponent implements OnInit { constructor(private formBuilder: FormBuilder, private route: ActivatedRoute, private router: Router, - private authenticationService: AuthenticationService) { } + private authenticationService: AuthenticationService, + private toastr: ToastrService + ) { } ngOnInit() { this.loginForm = this.formBuilder.group({ @@ -55,11 +58,15 @@ export class LoginComponent implements OnInit { this.authenticationService.login(this.f.username.value, this.f.password.value) .pipe(first()) .subscribe( - data => { + () => { this.router.navigate([this.returnUrl]); }, error => { - this.error = error; + if ( error.status == 401 ){ + this.toastr.error(error.error.message) + }else{ + this.toastr.error("Hiba történt") + } this.loading = false; } ) diff --git a/customerapi/controllers/UserController.php b/customerapi/controllers/UserController.php index 2cabb08..1c69851 100644 --- a/customerapi/controllers/UserController.php +++ b/customerapi/controllers/UserController.php @@ -12,9 +12,9 @@ use common\models\Customer; use customerapi\models\LoginForm; use customerapi\models\PasswordChangeForm; use sizeg\jwt\Jwt; -use sizeg\jwt\JwtHttpBearerAuth; use Yii; use yii\web\BadRequestHttpException; +use yii\web\UnauthorizedHttpException; /** @noinspection PhpUnused */ @@ -59,11 +59,7 @@ class UserController extends RestController 'token' => (string)$token, ]); } else { - return $this->asJson( - [ - 'errors' => $form->getErrors() - ] - ); + throw new UnauthorizedHttpException("Hibás e-mail cím vagy jelszó!"); } }