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,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<any>, next: HttpHandler): Observable<HttpEvent<any>>{
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);
})

View File

@ -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;
}
)

View File

@ -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ó!");
}
}