google-auth-plugin.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { MiddlewareConsumer, NestModule, OnApplicationBootstrap, OnModuleDestroy } from '@nestjs/common';
  2. import { PluginCommonModule, VendurePlugin } from '@vendure/core';
  3. import express from 'express';
  4. import path from 'path';
  5. import { GoogleAuthenticationStrategy } from './google-authentication-strategy';
  6. export type GoogleAuthPluginOptions = {
  7. clientId: string;
  8. };
  9. /**
  10. * An demo implementation of a Google login flow.
  11. *
  12. * To run this you'll need to install `google-auth-library` from npm.
  13. *
  14. * Then add this plugin to the dev config.
  15. *
  16. * The "storefront" is a simple html file which is served on http://localhost:3000/google-login,
  17. * but to get it to work with the Google login button you'll need to resolve it to some
  18. * public-looking url such as `http://google-login-test.com` by modifying your OS
  19. * hosts file.
  20. */
  21. @VendurePlugin({
  22. imports: [PluginCommonModule],
  23. configuration: config => {
  24. config.authOptions.shopAuthenticationStrategy = [
  25. ...config.authOptions.shopAuthenticationStrategy,
  26. new GoogleAuthenticationStrategy(GoogleAuthPlugin.options.clientId),
  27. ];
  28. return config;
  29. },
  30. })
  31. export class GoogleAuthPlugin implements NestModule {
  32. static options: GoogleAuthPluginOptions;
  33. static init(options: GoogleAuthPluginOptions) {
  34. this.options = options;
  35. return GoogleAuthPlugin;
  36. }
  37. configure(consumer: MiddlewareConsumer) {
  38. consumer.apply(express.static(path.join(__dirname, 'public'))).forRoutes('google-login');
  39. }
  40. }