keycloak-auth-plugin.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { HttpModule } from '@nestjs/axios';
  2. import { MiddlewareConsumer, NestModule } from '@nestjs/common';
  3. import { PluginCommonModule, VendurePlugin } from '@vendure/core';
  4. import express from 'express';
  5. import path from 'path';
  6. import { KeycloakAuthenticationStrategy } from './keycloak-authentication-strategy';
  7. /**
  8. * A demo plugin which configures an AuthenticationStrategy for a KeyCloak ID server.
  9. *
  10. * Assumes that KeyCloak is running on port 9000, with a realm configured named "myrealm"
  11. * and a client named "vendure".
  12. *
  13. * Add the plugin to the VendureConfig and set the Admin UI `loginUrl` option to
  14. * "http://localhost:3000/keycloak-login".
  15. *
  16. * Video demo of this: https://youtu.be/Tj4kwjNd2nM
  17. */
  18. @VendurePlugin({
  19. imports: [PluginCommonModule, HttpModule],
  20. configuration: config => {
  21. config.authOptions.adminAuthenticationStrategy = [
  22. ...config.authOptions.adminAuthenticationStrategy,
  23. new KeycloakAuthenticationStrategy(),
  24. ];
  25. return config;
  26. },
  27. })
  28. export class KeycloakAuthPlugin implements NestModule {
  29. configure(consumer: MiddlewareConsumer) {
  30. consumer.apply(express.static(path.join(__dirname, 'public'))).forRoutes('keycloak-login');
  31. }
  32. }