All files plugin.module.ts

100% Statements 19/19
100% Branches 8/8
100% Functions 5/5
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                  3x             3x 2x             3x 2x                 3x 3x 6x 6x 4x     4x   3x       1x       2x       2x    
import { HttpModule } from '@nestjs/axios';
import { Abstract, DynamicModule, ForwardReference, HttpService, Provider, Type } from '@nestjs/common';
import { VendurePlugin, PluginCommonModule } from '@vendure/core'
import { DEFAULT_PLUGIN_OPTIONS, THIRD_PARTY_OPTIONS_PROVIDER, LIST_OF_STRATEGY } from './constants'
import { CrossStrategiesChecker } from './cross-strategies-checker.service';
import { FacebookAuthService } from './facebook-auth.service';
import { FacebookAuthStrategy } from './facebook-auth.strategy';
import { GoogleAuthService } from './google-auth.service';
import { GoogleAuthStrategy } from './google-auth.strategy';
import { IThirdPartyAuthPluginOptions } from './interfaces'
 
@VendurePlugin({
    imports: [PluginCommonModule, HttpModule],
    providers: [
        {
            provide: THIRD_PARTY_OPTIONS_PROVIDER,
            useFactory: () =>{ 
                return ThirdPartyAuthPlugin.options
            }
        },
        {
            provide: GoogleAuthService,
            inject: [THIRD_PARTY_OPTIONS_PROVIDER],
            useFactory: async (options: IThirdPartyAuthPluginOptions) => {
                if (!options.google) return null
                return new GoogleAuthService(options as Required<IThirdPartyAuthPluginOptions>)
            }
        },
        {
            provide: FacebookAuthService,
            inject: [THIRD_PARTY_OPTIONS_PROVIDER, HttpService],
            useFactory: async (options: IThirdPartyAuthPluginOptions, httpService) => {
                if (!options.facebook) return null
                return new FacebookAuthService(
                    httpService,
                    options as Required<IThirdPartyAuthPluginOptions>
                )
            }
        },
        CrossStrategiesChecker
    ],
    configuration: (conf) => {
        const options = ThirdPartyAuthPlugin.options
        for (let name of LIST_OF_STRATEGY) {
            const optionDetail = options[name]
            if (!optionDetail) { continue }
            const strategy = name === 'google' 
                ? new GoogleAuthStrategy()
                : new FacebookAuthStrategy()
            conf.authOptions.shopAuthenticationStrategy.push(strategy)
        }
        return conf
    }
    
})
export class ThirdPartyAuthPlugin {
    static options: IThirdPartyAuthPluginOptions
    
    static init(options: Partial<IThirdPartyAuthPluginOptions>) {
        ThirdPartyAuthPlugin.options = {
            ...DEFAULT_PLUGIN_OPTIONS,
            ...options
        }
        return ThirdPartyAuthPlugin
    }
}