All files cross-strategies-checker.service.ts

100% Statements 12/12
77.77% Branches 7/9
100% Functions 2/2
100% Lines 9/9

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          1x   3x 3x       6x       6x 17x 12x     12x     4x    
import { Inject, Injectable } from "@nestjs/common";
import { AuthenticationStrategy, ConfigService, ExternalAuthenticationService, RequestContext } from "@vendure/core";
import { UserExistedInAnotherStrategyError } from "./interfaces";
 
@Injectable()
export class CrossStrategiesChecker {
    constructor(
        @Inject(ExternalAuthenticationService) private externalAuthService: ExternalAuthenticationService,
        @Inject(ConfigService) private configService: ConfigService
    ) {}
 
    async check(ctx: RequestContext, email: string, againstStrategy: string, forAdmin: boolean = false) {
        const strategies = forAdmin 
            ? this.configService.authOptions.adminAuthenticationStrategy
            : this.configService.authOptions.shopAuthenticationStrategy
 
        for (let strategy of strategies) {
            if (strategy.name === againstStrategy) { continue }
            const user = forAdmin 
                ? await this.externalAuthService.findAdministratorUser(ctx, strategy.name, email)
                : await this.externalAuthService.findCustomerUser(ctx, strategy.name, email)
            if (user) { throw new UserExistedInAnotherStrategyError(strategy.name) }
        }
        
        return true
    }
}