feat(login): add login authentication and authorization (#122)

Add login basic authentication, oauth2 authentication, two factor
authentication and authorization and allow to reset password on expiry.

Fixes #117, #118, #119, #120
This commit is contained in:
Abhay Chawla
2018-07-14 20:09:27 +05:30
committed by GitHub
parent 91e9744bb8
commit d9c35b8759
33 changed files with 953 additions and 271 deletions
+33 -6
View File
@@ -1,11 +1,12 @@
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { environment } from '../../../environments/environment';
import { Logger } from '../logger.service';
import { AlertService } from '../alert.service';
const log = new Logger('ErrorHandlerInterceptor');
@@ -15,16 +16,42 @@ const log = new Logger('ErrorHandlerInterceptor');
@Injectable()
export class ErrorHandlerInterceptor implements HttpInterceptor {
constructor(private alertService: AlertService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(error => this.errorHandler(error)));
return next.handle(request).pipe(catchError(error => this.handleError(error)));
}
// Customize the default error handler here if needed
private errorHandler(response: HttpEvent<any>): Observable<HttpEvent<any>> {
if (!environment.production) {
// Do something with the error
log.error('Request error', response);
private handleError(response: HttpErrorResponse): Observable<HttpEvent<any>> {
const status = response.status;
let errorMessage = (response.error.developerMessage || response.message);
if (response.error.errors) {
if (response.error.errors[0]) {
errorMessage = response.error.errors[0].developerMessage;
}
}
if (!environment.production) {
log.error(`Request Error: ${errorMessage}`);
}
if (status === 401 || (environment.oauth.enabled && status === 400)) {
this.alertService.alert({ type: 'Authentication Error', message: 'Invalid User Details. Please try again!' });
} else if (status === 403 && errorMessage === 'The provided one time token is invalid') {
this.alertService.alert({ type: 'Invalid Token', message: 'Invalid Token. Please try again!' });
} else if (status === 400) {
this.alertService.alert({ type: 'Bad Request', message: 'Invalid parameters were passed in the request!' });
} else if (status === 403) {
this.alertService.alert({ type: 'Unauthorized Request', message: 'You are not authorized for this request!' });
} else if (status === 404) {
this.alertService.alert({ type: 'Resource does not exist', message: 'Resource does not exist!' });
} else if (status === 500) {
this.alertService.alert({ type: 'Internal Server Error', message: 'Internal Server Error. Please try again later.' });
} else {
this.alertService.alert({ type: 'Unknown Error', message: 'Unknown Error. Please try again later.' });
}
throw response;
}