google-auth-plugin.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { INestApplication } from '@nestjs/common';
  2. import { OnVendureBootstrap, OnVendureClose, PluginCommonModule, VendurePlugin } from '@vendure/core';
  3. import express from 'express';
  4. import { Server } from 'http';
  5. import path from 'path';
  6. import { GoogleAuthenticationStrategy } from './google-authentication-strategy';
  7. export type GoogleAuthPluginOptions = {
  8. clientId: string;
  9. };
  10. @VendurePlugin({
  11. imports: [PluginCommonModule],
  12. configuration: config => {
  13. config.authOptions.shopAuthenticationStrategy = [
  14. ...config.authOptions.shopAuthenticationStrategy,
  15. new GoogleAuthenticationStrategy(GoogleAuthPlugin.options.clientId),
  16. ];
  17. return config;
  18. },
  19. })
  20. export class GoogleAuthPlugin implements OnVendureBootstrap, OnVendureClose {
  21. static options: GoogleAuthPluginOptions;
  22. private staticServer: Server;
  23. static init(options: GoogleAuthPluginOptions) {
  24. this.options = options;
  25. return GoogleAuthPlugin;
  26. }
  27. onVendureBootstrap() {
  28. // Set up a static express server to serve the demo login page
  29. // from public/index.html.
  30. const app = express();
  31. app.use(express.static(path.join(__dirname, 'public')));
  32. this.staticServer = app.listen(80);
  33. }
  34. onVendureClose(): void | Promise<void> {
  35. return new Promise((resolve, reject) => {
  36. this.staticServer.close(err => {
  37. if (err) {
  38. reject(err);
  39. } else {
  40. resolve();
  41. }
  42. });
  43. });
  44. }
  45. }