JWT authentication using header (GraphQL)

This commit is contained in:
Norbi Peti 2022-04-27 00:31:01 +02:00
parent 65332d4947
commit 8b2efc0997
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56

View file

@ -2,10 +2,15 @@ import { BootMixin } from '@loopback/boot';
import { Application, ApplicationConfig } from '@loopback/core'; import { Application, ApplicationConfig } from '@loopback/core';
import { RepositoryMixin } from '@loopback/repository'; import { RepositoryMixin } from '@loopback/repository';
import { ServiceMixin } from '@loopback/service-proxy'; import { ServiceMixin } from '@loopback/service-proxy';
import { AuthenticationComponent } from '@loopback/authentication'; import { AuthenticationBindings, AuthenticationComponent } from '@loopback/authentication';
import { JWTAuthenticationComponent, UserServiceBindings } from '@loopback/authentication-jwt'; import {
JWTAuthenticationComponent,
JWTAuthenticationStrategy,
TokenServiceBindings,
UserServiceBindings
} from '@loopback/authentication-jwt';
import { SzakdolgozatUserService } from './services'; import { SzakdolgozatUserService } from './services';
import { GraphQLServer } from '@loopback/graphql'; import { GraphQLBindings, GraphQLServer } from '@loopback/graphql';
import { UserResolver } from './graphql-resolvers/user-resolver'; import { UserResolver } from './graphql-resolvers/user-resolver';
export { ApplicationConfig }; export { ApplicationConfig };
@ -28,6 +33,16 @@ export class SzakdolgozatBackendApplication extends BootMixin(
// Authentication // Authentication
this.component(AuthenticationComponent); this.component(AuthenticationComponent);
this.component(JWTAuthenticationComponent); 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((<any> resolverData.context).req);
console.log('Res: ', res);
return true;
});
this.service(SzakdolgozatUserService, UserServiceBindings.USER_SERVICE); this.service(SzakdolgozatUserService, UserServiceBindings.USER_SERVICE);
this.projectRoot = __dirname; this.projectRoot = __dirname;