فهرست منبع

fix: remove sessionid check via CookieService

fzadrazil 4 سال پیش
والد
کامیت
71bb54acf4

+ 2 - 1
.gitignore

@@ -3,4 +3,5 @@
 ################################################################################
 
 /.vs
-/node_modules
+node_modules
+/dist

+ 2 - 1
package.json

@@ -4,7 +4,8 @@
   "scripts": {
     "ng": "ng",
     "start": "ng serve",
-    "build": "ng build --prod",
+    "build": "ng build --configuration production",
+    "build-stage": "ng build --configuration production --base-href /senslog-staging",
     "test": "ng test",
     "lint": "ng lint",
     "e2e": "ng e2e"

+ 2 - 2
src/app/auth/auth.module.ts

@@ -2,7 +2,7 @@ import {NgModule} from '@angular/core';
 import {CommonModule} from '@angular/common';
 import {RouterModule} from '@angular/router';
 import {HTTP_INTERCEPTORS} from '@angular/common/http';
-import {AuthInterceptor} from './interceptors/auth.interceptor';
+//import {AuthInterceptor} from './interceptors/auth.interceptor';
 import {ApiModule} from '../shared/api/endpoints/api.module';
 
 
@@ -14,7 +14,7 @@ import {ApiModule} from '../shared/api/endpoints/api.module';
     ApiModule
   ],
   providers: [
-    {provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true}
+/*    {provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true}*/
   ]
 })
 export class AuthModule {

+ 65 - 65
src/app/auth/interceptors/auth.interceptor.ts

@@ -1,72 +1,72 @@
-import {Injectable} from '@angular/core';
-import {
-  HttpRequest,
-  HttpHandler,
-  HttpEvent,
-  HttpInterceptor, HttpErrorResponse
-} from '@angular/common/http';
-import {Observable, throwError, of} from 'rxjs';
+////import {Injectable} from '@angular/core';
+////import {
+////  HttpRequest,
+////  HttpHandler,
+////  HttpEvent,
+////  HttpInterceptor, HttpErrorResponse
+////} from '@angular/common/http';
+////import {Observable, throwError, of} from 'rxjs';
 
-import {catchError} from 'rxjs/operators';
-import {AuthService} from '../services/auth.service';
-import {CookieService} from 'ngx-cookie-service';
-import {GlobalVariable} from '../../globals';
-import {ToastService} from '../../shared/services/toast.service';
+////import {catchError} from 'rxjs/operators';
+////import {AuthService} from '../services/auth.service';
+////import {CookieService} from 'ngx-cookie-service';
+////import {GlobalVariable} from '../../globals';
+////import {ToastService} from '../../shared/services/toast.service';
 
-@Injectable()
-export class AuthInterceptor implements HttpInterceptor {
+////@Injectable()
+////export class AuthInterceptor implements HttpInterceptor {
 
-  constructor(
-    private authService: AuthService,
-    private cookieService: CookieService,
-    private toastService: ToastService
-  ) {
-  }
+////  constructor(
+////    private authService: AuthService,
+////    private cookieService: CookieService,
+////    private toastService: ToastService
+////  ) {
+////  }
 
-  /**
-   * Intercept every request to backend
-   * @param request request
-   * @param next next
-   */
-  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
-    // check if data in cookies
-    if (!this.cookieService.get(GlobalVariable.SESSION_ID) && !request.url.includes('ControllerServlet')) {
-      console.log('No sessionid!');
-      this.cookieService.deleteAll();
-      this.authService.doLogout();
-      return of(null);
-    }
+////  /**
+////   * Intercept every request to backend
+////   * @param request request
+////   * @param next next
+////   */
+////  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
+////    // check if data in cookies
+////    if (!this.cookieService.get(GlobalVariable.SESSION_ID) && !request.url.includes('ControllerServlet')) {
+////      console.log('No sessionid!');
+////      this.cookieService.deleteAll();
+////      this.authService.doLogout();
+////      return of(null);
+////    }
 
-    console.log('Sending request!', request.url);
-    return next.handle(request)
-      .pipe(
-        catchError(err => {
-          if ((err instanceof HttpErrorResponse && err.status === 502 || err.status === 401 ||
-            err.status === 0)) {
-            console.log(err);
-            this.toastService.showError(err.error.message);
-            return this.handleError(request, next);
-          } else if ((err instanceof HttpErrorResponse && err.status === 500 || err.status === 504 || err.status === 500)) {
-            if (err.error) {
-              this.toastService.showError(err.error);
-            } else {
-              this.toastService.showError('500 - Error!');
-            }
-            this.handle500Error(err);
-          } else {
-            return throwError(err);
-          }
-        })
-      );
-  }
+////    console.log('Sending request!', request.url);
+////    return next.handle(request)
+////      .pipe(
+////        catchError(err => {
+////          if ((err instanceof HttpErrorResponse && err.status === 502 || err.status === 401 ||
+////            err.status === 0)) {
+////            console.log(err);
+////            this.toastService.showError(err.error.message);
+////            return this.handleError(request, next);
+////          } else if ((err instanceof HttpErrorResponse && err.status === 500 || err.status === 504 || err.status === 500)) {
+////            if (err.error) {
+////              this.toastService.showError(err.error);
+////            } else {
+////              this.toastService.showError('500 - Error!');
+////            }
+////            this.handle500Error(err);
+////          } else {
+////            return throwError(err);
+////          }
+////        })
+////      );
+////  }
 
-  handleError(request: HttpRequest<any>, next: HttpHandler) {
-    this.authService.doLogout();
-    return of(null);
-  }
+////  handleError(request: HttpRequest<any>, next: HttpHandler) {
+////    this.authService.doLogout();
+////    return of(null);
+////  }
 
-  handle500Error(err: any) {
-    this.authService.redirectToDashboard();
-    return of(null);
-  }
-}
+////  handle500Error(err: any) {
+////    this.authService.redirectToDashboard();
+////    return of(null);
+////  }
+////}

+ 11 - 1
src/app/auth/states/user.state.ts

@@ -3,6 +3,8 @@ import {BehaviorSubject, Observable} from 'rxjs';
 import {User} from '../models/user';
 import {LoginService} from '../../shared/api/endpoints/services/login.service';
 import {ToastService} from '../../shared/services/toast.service';
+//import {AuthService} from '../services/auth.service';
+import {CookieService} from 'ngx-cookie-service';
 
 @Injectable({
   providedIn: 'root'
@@ -12,6 +14,8 @@ export class UserState {
 
   constructor(
     private loginService: LoginService,
+    //private authService: AuthService,
+    private cookieService: CookieService,
     private toastService: ToastService
   ) {}
 
@@ -35,7 +39,13 @@ export class UserState {
    if (this.userState$.getValue()){
       this.loginService.getUserInfo$Response().subscribe(res => {
         this.userState$.next({...this.userState$.getValue(), userInfo: res.body});
-      }, err => this.toastService.showError(err.error.message));
+      }, err => {
+        this.toastService.showError(err.error.message);
+        console.log('Authentication failed!');
+        this.setUser(null);
+        this.cookieService.deleteAll();
+      //this.authService.doLogout();
+      });
    }
     return this.userState$.asObservable();
   }

+ 2 - 1
src/app/shared/api/endpoints/request-builder.ts

@@ -346,7 +346,8 @@ export class RequestBuilder {
       params: httpParams,
       headers: httpHeaders,
       responseType: options.responseType,
-      reportProgress: options.reportProgress
+      reportProgress: options.reportProgress,
+      //withCredentials: true
     });
   }
 }

+ 6 - 2
src/app/shared/api/endpoints/services/login.service.ts

@@ -6,8 +6,8 @@ import { BaseService } from '../base-service';
 import { ApiConfiguration } from '../api-configuration';
 import { StrictHttpResponse } from '../strict-http-response';
 import { RequestBuilder } from '../request-builder';
-import { Observable } from 'rxjs';
-import { map, filter } from 'rxjs/operators';
+import { Observable, throwError } from 'rxjs';
+import { map, filter, catchError } from 'rxjs/operators';
 
 import { UserCookie } from '../models/user-cookie';
 import { UserInfo } from '../models/user-info';
@@ -126,6 +126,10 @@ export class LoginService extends BaseService {
       responseType: 'json',
       accept: 'application/json'
     })).pipe(
+      catchError((err) => {
+        console.error(err);
+        return throwError(err);
+      }),
       filter((r: any) => r instanceof HttpResponse),
       map((r: HttpResponse<any>) => {
         return r as StrictHttpResponse<UserInfo>;