Skip to content

Commit

Permalink
improved http request performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenSoftware committed Dec 5, 2021
1 parent cdd56f3 commit 20a4f4c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
5 changes: 2 additions & 3 deletions apps/portal/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterModule } from '@angular/router';
import { ZenAuthModule } from '@zen/auth';
import { Environment, HttpRequestInterceptor } from '@zen/common';
import { Environment, HttpRequestInterceptor, tokenVar } from '@zen/common';
import { ZenGraphQLModule } from '@zen/graphql';
import { possibleTypes, typePolicies } from '@zen/graphql/client';
import { MainModule } from '@zen/main';
import ls from 'localstorage-slim';

import { environment } from '../environments/environment';
import { AppComponent } from './app.component';
Expand Down Expand Up @@ -44,7 +43,7 @@ import { AppComponent } from './app.component';
uri: environment.url.graphqlSubscriptions,
options: {
reconnect: true,
connectionParams: () => ({ token: ls.get('token', { decrypt: true }) }),
connectionParams: () => ({ token: tokenVar() }),
},
},
}),
Expand Down
5 changes: 4 additions & 1 deletion libs/auth/src/lib/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Environment } from '@zen/common';
import { Environment, tokenVar } from '@zen/common';
import {
ApiError,
AuthExchangeTokenGQL,
Expand Down Expand Up @@ -100,6 +100,8 @@ export class AuthService {
ls.set(LocalStorageKey.rememberMe, authSession.rememberMe);
ls.set(LocalStorageKey.roles, authSession.roles, { encrypt: true });

tokenVar(authSession.token);

if (!this.rolesEqual(this.roles, authSession.roles)) {
if (authSession.roles) userRolesVar(authSession.roles);
else userRolesVar([]);
Expand Down Expand Up @@ -164,6 +166,7 @@ export class AuthService {
ls.remove(LocalStorageKey.rememberMe);
ls.remove(LocalStorageKey.roles);
userRolesVar([]);
tokenVar(null);
this.apollo.client.cache.reset();
this.#graphqlSubscriptionClient$.next(null);
}
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './lib/nameof';
export * from './lib/email-validator';
export * from './lib/password-validator';
export * from './lib/trim-object-strings';
export * from './lib/token-var';
3 changes: 2 additions & 1 deletion libs/common/src/lib/http-request-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ls from 'localstorage-slim';
import { Observable } from 'rxjs';

import { Environment } from './environment';
import { tokenVar } from './token-var';

@Injectable()
export class HttpRequestInterceptor implements HttpInterceptor {
Expand All @@ -16,7 +17,7 @@ export class HttpRequestInterceptor implements HttpInterceptor {
}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token = ls.get('token', { decrypt: true }) as string | undefined;
const token = tokenVar();
const reqHost = new URL(req.url).host;

if (token && (reqHost === this.apiHost || reqHost === this.gqlHost)) {
Expand Down
5 changes: 5 additions & 0 deletions libs/common/src/lib/token-var.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { makeVar } from '@apollo/client/core';
import ls from 'localstorage-slim';

const token: string = ls.get('token', { decrypt: true }) ?? '';
export const tokenVar = makeVar<string | null>(token);

0 comments on commit 20a4f4c

Please sign in to comment.