improve login error handling

This commit is contained in:
Roland Schneider 2021-09-26 12:45:02 +02:00
parent 0b5ea5df09
commit 5533f7100b
3 changed files with 16 additions and 16 deletions

View File

@ -1,6 +1,6 @@
import { Injectable } from "@angular/core"; import { Injectable } from "@angular/core";
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from "@angular/common/http"; import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from "@angular/common/http";
import {EMPTY, Observable, throwError} from "rxjs"; import { Observable, throwError} from "rxjs";
import {catchError} from "rxjs/operators"; import {catchError} from "rxjs/operators";
import { AuthenticationService} from "../services/authentication.service"; import { AuthenticationService} from "../services/authentication.service";
import {NavigationService} from "../services/navigation.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() @Injectable()
export class ErrorInterceptor implements HttpInterceptor{ export class ErrorInterceptor implements HttpInterceptor{
constructor(private authenticationService: AuthenticationService, constructor(private authenticationService: AuthenticationService,
private navigationService: NavigationService){} private navigationService: NavigationService
){}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>{ intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>{
return next.handle(request) 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 // auto logout on unauthorized response
this.authenticationService.logout(); this.authenticationService.logout();
this.navigationService.navigateToLogin(); this.navigationService.navigateToLogin();
// location.reload(true);
// return EMPTY;
} }
// const error = err.error.message || err.statusText;
// return throwError(error);
return throwError(err); return throwError(err);
}) })

View File

@ -3,6 +3,7 @@ import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {ActivatedRoute, Router} from "@angular/router"; import {ActivatedRoute, Router} from "@angular/router";
import {AuthenticationService} from "../../services/authentication.service"; import {AuthenticationService} from "../../services/authentication.service";
import { first } from 'rxjs/operators'; import { first } from 'rxjs/operators';
import {ToastrService} from "ngx-toastr";
@Component({ @Component({
selector: 'app-login', selector: 'app-login',
@ -20,7 +21,9 @@ export class LoginComponent implements OnInit {
constructor(private formBuilder: FormBuilder, constructor(private formBuilder: FormBuilder,
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
private authenticationService: AuthenticationService) { } private authenticationService: AuthenticationService,
private toastr: ToastrService
) { }
ngOnInit() { ngOnInit() {
this.loginForm = this.formBuilder.group({ 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) this.authenticationService.login(this.f.username.value, this.f.password.value)
.pipe(first()) .pipe(first())
.subscribe( .subscribe(
data => { () => {
this.router.navigate([this.returnUrl]); this.router.navigate([this.returnUrl]);
}, },
error => { 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; this.loading = false;
} }
) )

View File

@ -12,9 +12,9 @@ use common\models\Customer;
use customerapi\models\LoginForm; use customerapi\models\LoginForm;
use customerapi\models\PasswordChangeForm; use customerapi\models\PasswordChangeForm;
use sizeg\jwt\Jwt; use sizeg\jwt\Jwt;
use sizeg\jwt\JwtHttpBearerAuth;
use Yii; use Yii;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\UnauthorizedHttpException;
/** @noinspection PhpUnused */ /** @noinspection PhpUnused */
@ -59,11 +59,7 @@ class UserController extends RestController
'token' => (string)$token, 'token' => (string)$token,
]); ]);
} else { } else {
return $this->asJson( throw new UnauthorizedHttpException("Hibás e-mail cím vagy jelszó!");
[
'errors' => $form->getErrors()
]
);
} }
} }