From 8b2efc099728b5704dd1737c574baa07f5ade7c7 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 27 Apr 2022 00:31:01 +0200 Subject: [PATCH] JWT authentication using header (GraphQL) --- backend/src/application.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/backend/src/application.ts b/backend/src/application.ts index 014108d..dab8732 100644 --- a/backend/src/application.ts +++ b/backend/src/application.ts @@ -2,10 +2,15 @@ import { BootMixin } from '@loopback/boot'; import { Application, ApplicationConfig } from '@loopback/core'; import { RepositoryMixin } from '@loopback/repository'; import { ServiceMixin } from '@loopback/service-proxy'; -import { AuthenticationComponent } from '@loopback/authentication'; -import { JWTAuthenticationComponent, UserServiceBindings } from '@loopback/authentication-jwt'; +import { AuthenticationBindings, AuthenticationComponent } from '@loopback/authentication'; +import { + JWTAuthenticationComponent, + JWTAuthenticationStrategy, + TokenServiceBindings, + UserServiceBindings +} from '@loopback/authentication-jwt'; import { SzakdolgozatUserService } from './services'; -import { GraphQLServer } from '@loopback/graphql'; +import { GraphQLBindings, GraphQLServer } from '@loopback/graphql'; import { UserResolver } from './graphql-resolvers/user-resolver'; export { ApplicationConfig }; @@ -28,6 +33,16 @@ export class SzakdolgozatBackendApplication extends BootMixin( // Authentication this.component(AuthenticationComponent); this.component(JWTAuthenticationComponent); + this.get(TokenServiceBindings.TOKEN_SERVICE).then(tokenService => { + this.bind(AuthenticationBindings.STRATEGY).to(new JWTAuthenticationStrategy(tokenService)); + }); + this.bind(GraphQLBindings.GRAPHQL_AUTH_CHECKER).to(async (resolverData, roles) => { + const authenticate = await this.get(AuthenticationBindings.AUTH_ACTION); + const res = await authenticate(( resolverData.context).req); + console.log('Res: ', res); + return true; + }); + this.service(SzakdolgozatUserService, UserServiceBindings.USER_SERVICE); this.projectRoot = __dirname;