keycloak-auth-plugin.ts 1.2 KB

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