import {BootMixin} from '@loopback/boot'; import {ApplicationConfig} from '@loopback/core'; import { RestExplorerBindings, RestExplorerComponent, } from '@loopback/rest-explorer'; import {RepositoryMixin} from '@loopback/repository'; import {RestApplication} from '@loopback/rest'; import {ServiceMixin} from '@loopback/service-proxy'; import {OpenApiSpec} from '@loopback/openapi-v3-types'; import * as path from 'path'; import {MySequence} from './sequence'; import { AuthComponent, TokenServiceBindings, PasswordServiceBindings, UserServiceBindings, } from '@hgmap/lbext-auth'; //import { UserDataSource } from './datasources'; export class AppTestApplication extends BootMixin( ServiceMixin(RepositoryMixin(RestApplication)), ) { constructor(options: ApplicationConfig = {}) { super(options); const spec: OpenApiSpec = { openapi: '3.0.0', info: { title: 'API Application v1', version: '1.1.0', }, paths: {}, }; this.api(spec); // Set up the custom sequence this.sequence(MySequence); // Set up default home page this.static('/', path.join(__dirname, '../public')); // Customize @loopback/rest-explorer configuration here this.bind(RestExplorerBindings.CONFIG).to({ path: '/explorer', }); this.component(RestExplorerComponent); this.projectRoot = __dirname; // Customize @loopback/boot Booter Conventions here this.bootOptions = { controllers: { // Customize ControllerBooter Conventions here dirs: ['controllers'], extensions: ['.controller.js'], nested: true, }, }; // component lbext-auth config options.lbextAuth = { defaultArtifacts: { repositories: { repoUser: true // use UserRepository from component; if false then use copy or your own from /src/repositories/.ts }, controllers: { auth: true, // use AuthController from component; if false then use copy or your own from /src/controllers/.ts user: true, // use UserController from component; if false then use copy or your own from /src/controllers/.ts }, datasources: { dataUser: true, // use UserDataSource from component; if false then use copy or your own from /src/datasources/.ts // if it's true, you can also reconfigure datasource by it's config only in the UserServiceBindings.DS_CFG variable }, }, }; this.component(AuthComponent); /** configure TokenService (for JWT authentication) and PasswordService (hash settings) */ // default: "ZDUwYTc3OGM4Y2NjOTUwODQ0NTMyNWU3MWJlZmJjOTI3ZWUwNzRmMSAgLQo=" this.bind(TokenServiceBindings.SECRET).to('AS SECRET YOU CAN USE RS256 ALG WITH CERTIFICATE'); /** you can create and use certificate: const private_key = fs.readFileSync('path/to/cert/directory/private.key','utf8'); - for generateToken - as your secret const public_key = fs.readFileSync('path/to/cert/directory/public.key','utf8'); - for verifyToken - you can use it on many other servers... */ // default: "LoopbackApp" this.bind(TokenServiceBindings.ISSUER).to('Issuer'); // default: "Loopback" this.bind(TokenServiceBindings.AUDIENCE).to('Audience'); // default: "600" NOTE: string this.bind(TokenServiceBindings.EXPIRES_IN).to('3600'); // default: "MzQ5MjBhZGQyYTZhZmJkNzJiZmNlNDc0NGU0OWVhMTg1NDk0YzM4YiAgLQo=" this.bind(PasswordServiceBindings.SALT).to('SALT ITS NOT REQUIRED'); // default: 64 bytes - hash length this.bind(PasswordServiceBindings.LENGTH).to('64'); /** if defaultArtifacts.datasources.dataUser is true (also any of available datasources) then you can reconfigure default UserDataSource (overwrite config) by binding of datasource config to UserServiceBindings.DS_CFG. See below --- this.bind(UserServiceBindings.DS_CFG).to({ "name": "dbusers", "connector": "memory", "localStorage": "", "file": process.cwd()+'/db-users.json', }); --- or use your favourite connector... */ /** if you didn't define datasource User that use the default one from component then you need uncomment lines below and copy defaults/create your own datasource. As below: --- this.bind(UserServiceBindings.DS).toClass(UserDataSource); --- */ /** if you didn't define defaultArtifacts.repositories or set defaultArtifacts.repositories.repoUser to false then you need copy default to your application src/repositories directory or create your own by lb4/manual And bind it to UserServiceBindings.REPO_USER as you can see below: --- this.bind(UserServiceBindings.REPO_USER).toClass(UserRepository); --- NOTE: you also need to create User model and UserRelations ... */ } }