From 9b2dfe688b070e5ec63fe333285f31ba9e4bd91a Mon Sep 17 00:00:00 2001 From: Sergei Zaychenko Date: Tue, 29 Aug 2023 06:16:58 -0700 Subject: [PATCH] GraphQL: added Authorization Bearer header --- src/app/app.module.ts | 23 +++++++++++++++++------ src/app/graphql.module.ts | 27 --------------------------- 2 files changed, 17 insertions(+), 33 deletions(-) delete mode 100644 src/app/graphql.module.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 5c799bad3..6ab38d368 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -16,10 +16,10 @@ import { MatToolbarModule } from "@angular/material/toolbar"; import { NgbModule, NgbTypeaheadModule } from "@ng-bootstrap/ng-bootstrap"; import { MatFormFieldModule } from "@angular/material/form-field"; import { MatIconModule } from "@angular/material/icon"; -import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS } from "@angular/common/http"; +import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS, HttpHeaders } from "@angular/common/http"; import { MatTableModule } from "@angular/material/table"; import { CdkTableModule } from "@angular/cdk/table"; -import { InMemoryCache } from "@apollo/client/core"; +import { ApolloLink, InMemoryCache, NextLink, Operation } from "@apollo/client/core"; import { SearchApi } from "./api/search.api"; import { FormsModule, ReactiveFormsModule } from "@angular/forms"; import { SearchService } from "./search/search.service"; @@ -66,6 +66,7 @@ import { LoggedUserService } from "./auth/logged-user.service"; import { firstValueFrom } from "rxjs"; import { LoginService } from "./auth/login/login.service"; import { logError } from "./common/app.helpers"; +import AppValues from "./common/app.values"; const Services = [ { @@ -93,6 +94,19 @@ const Services = [ { provide: APOLLO_OPTIONS, useFactory: (httpLink: HttpLink, appConfig: AppConfigService) => { + const httpMainLink: ApolloLink = httpLink.create({ uri: appConfig.apiServerGqlUrl }); + + const authorizationMiddleware: ApolloLink = new ApolloLink((operation: Operation, forward: NextLink) => { + const accessToken: string | null = localStorage.getItem(AppValues.LOCAL_STORAGE_ACCESS_TOKEN); + if (accessToken) { + operation.setContext({ + headers: new HttpHeaders().set("Authorization", `Bearer ${accessToken}`), + }); + } + + return forward(operation); + }); + return { cache: new InMemoryCache({ typePolicies: { @@ -102,9 +116,7 @@ const Services = [ }, }, }), - link: httpLink.create({ - uri: appConfig.apiServerGqlUrl, - }), + link: authorizationMiddleware.concat(httpMainLink), }; }, deps: [HttpLink, AppConfigService], @@ -179,7 +191,6 @@ const MatModules = [ }), NgbModule, NgbTypeaheadModule, - //GraphQLModule, HttpClientModule, CdkTableModule, ...MatModules, diff --git a/src/app/graphql.module.ts b/src/app/graphql.module.ts deleted file mode 100644 index c10733d6d..000000000 --- a/src/app/graphql.module.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { APOLLO_OPTIONS } from "apollo-angular"; -import { HttpLink, HttpLinkHandler } from "apollo-angular/http"; -import { NgModule } from "@angular/core"; - -import { InMemoryCache } from "@apollo/client/core"; - -const uri = "kamu"; // <-- add the URL of the GraphQL server here -export function createApollo(httpLink: HttpLink): { - cache: InMemoryCache; - link: HttpLinkHandler; -} { - return { - link: httpLink.create({ uri }), - cache: new InMemoryCache(), - }; -} - -@NgModule({ - providers: [ - { - provide: APOLLO_OPTIONS, - useFactory: createApollo, - deps: [HttpLink], - }, - ], -}) -export class GraphQLModule {}