|
|
@@ -18,11 +18,13 @@ import { Request, Response } from 'express';
|
|
|
|
|
|
import {
|
|
|
ForbiddenError,
|
|
|
+ InternalServerError,
|
|
|
PasswordResetTokenError,
|
|
|
VerificationTokenError,
|
|
|
} from '../../../common/error/errors';
|
|
|
import { NATIVE_AUTH_STRATEGY_NAME } from '../../../config/auth/native-authentication-strategy';
|
|
|
import { ConfigService } from '../../../config/config.service';
|
|
|
+import { Logger } from '../../../config/logger/vendure-logger';
|
|
|
import { AdministratorService } from '../../../service/services/administrator.service';
|
|
|
import { AuthService } from '../../../service/services/auth.service';
|
|
|
import { CustomerService } from '../../../service/services/customer.service';
|
|
|
@@ -35,6 +37,8 @@ import { BaseAuthResolver } from '../base/base-auth.resolver';
|
|
|
|
|
|
@Resolver()
|
|
|
export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
+ private nativeAuthStrategyIsConfigured = false;
|
|
|
+
|
|
|
constructor(
|
|
|
authService: AuthService,
|
|
|
userService: UserService,
|
|
|
@@ -44,6 +48,9 @@ export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
protected historyService: HistoryService,
|
|
|
) {
|
|
|
super(authService, userService, administratorService, configService);
|
|
|
+ this.nativeAuthStrategyIsConfigured = !!this.configService.authOptions.shopAuthenticationStrategy.find(
|
|
|
+ strategy => strategy.name === NATIVE_AUTH_STRATEGY_NAME,
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
@Mutation()
|
|
|
@@ -54,6 +61,7 @@ export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
@Context('req') req: Request,
|
|
|
@Context('res') res: Response,
|
|
|
): Promise<LoginResult> {
|
|
|
+ this.requireNativeAuthStrategy();
|
|
|
return super.login(args, ctx, req, res, 'shop');
|
|
|
}
|
|
|
|
|
|
@@ -65,13 +73,7 @@ export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
@Context('req') req: Request,
|
|
|
@Context('res') res: Response,
|
|
|
): Promise<LoginResult> {
|
|
|
- return super.login(
|
|
|
- { username: args.data.username, password: args.data.password, rememberMe: args.rememberMe },
|
|
|
- ctx,
|
|
|
- req,
|
|
|
- res,
|
|
|
- 'shop',
|
|
|
- );
|
|
|
+ return this.createAuthenticatedSession(ctx, args, req, res, 'shop');
|
|
|
}
|
|
|
|
|
|
@Mutation()
|
|
|
@@ -96,6 +98,7 @@ export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
@Ctx() ctx: RequestContext,
|
|
|
@Args() args: MutationRegisterCustomerAccountArgs,
|
|
|
) {
|
|
|
+ this.requireNativeAuthStrategy();
|
|
|
return this.customerService.registerCustomerAccount(ctx, args.input).then(() => true);
|
|
|
}
|
|
|
|
|
|
@@ -107,6 +110,7 @@ export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
@Context('req') req: Request,
|
|
|
@Context('res') res: Response,
|
|
|
) {
|
|
|
+ this.requireNativeAuthStrategy();
|
|
|
const customer = await this.customerService.verifyCustomerEmailAddress(
|
|
|
ctx,
|
|
|
args.token,
|
|
|
@@ -116,12 +120,12 @@ export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
return super.createAuthenticatedSession(
|
|
|
ctx,
|
|
|
{
|
|
|
- method: NATIVE_AUTH_STRATEGY_NAME,
|
|
|
- data: {
|
|
|
- username: customer.user.identifier,
|
|
|
- password: args.password,
|
|
|
+ input: {
|
|
|
+ [NATIVE_AUTH_STRATEGY_NAME]: {
|
|
|
+ username: customer.user.identifier,
|
|
|
+ password: args.password,
|
|
|
+ },
|
|
|
},
|
|
|
- rememberMe: true,
|
|
|
},
|
|
|
req,
|
|
|
res,
|
|
|
@@ -138,6 +142,7 @@ export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
@Ctx() ctx: RequestContext,
|
|
|
@Args() args: MutationRefreshCustomerVerificationArgs,
|
|
|
) {
|
|
|
+ this.requireNativeAuthStrategy();
|
|
|
return this.customerService.refreshVerificationToken(ctx, args.emailAddress).then(() => true);
|
|
|
}
|
|
|
|
|
|
@@ -155,18 +160,19 @@ export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
@Context('req') req: Request,
|
|
|
@Context('res') res: Response,
|
|
|
) {
|
|
|
+ this.requireNativeAuthStrategy();
|
|
|
const { token, password } = args;
|
|
|
const customer = await this.customerService.resetPassword(ctx, token, password);
|
|
|
if (customer && customer.user) {
|
|
|
return super.createAuthenticatedSession(
|
|
|
ctx,
|
|
|
{
|
|
|
- method: NATIVE_AUTH_STRATEGY_NAME,
|
|
|
- data: {
|
|
|
- username: customer.user.identifier,
|
|
|
- password: args.password,
|
|
|
+ input: {
|
|
|
+ [NATIVE_AUTH_STRATEGY_NAME]: {
|
|
|
+ username: customer.user.identifier,
|
|
|
+ password: args.password,
|
|
|
+ },
|
|
|
},
|
|
|
- rememberMe: true,
|
|
|
},
|
|
|
req,
|
|
|
res,
|
|
|
@@ -183,6 +189,7 @@ export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
@Ctx() ctx: RequestContext,
|
|
|
@Args() args: MutationUpdateCustomerPasswordArgs,
|
|
|
): Promise<boolean> {
|
|
|
+ this.requireNativeAuthStrategy();
|
|
|
const result = await super.updatePassword(ctx, args.currentPassword, args.newPassword);
|
|
|
if (result && ctx.activeUserId) {
|
|
|
const customer = await this.customerService.findOneByUserId(ctx.activeUserId);
|
|
|
@@ -204,6 +211,7 @@ export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
@Ctx() ctx: RequestContext,
|
|
|
@Args() args: MutationRequestUpdateCustomerEmailAddressArgs,
|
|
|
): Promise<boolean> {
|
|
|
+ this.requireNativeAuthStrategy();
|
|
|
if (!ctx.activeUserId) {
|
|
|
throw new ForbiddenError();
|
|
|
}
|
|
|
@@ -217,6 +225,20 @@ export class ShopAuthResolver extends BaseAuthResolver {
|
|
|
@Ctx() ctx: RequestContext,
|
|
|
@Args() args: MutationUpdateCustomerEmailAddressArgs,
|
|
|
): Promise<boolean> {
|
|
|
+ this.requireNativeAuthStrategy();
|
|
|
return this.customerService.updateEmailAddress(ctx, args.token);
|
|
|
}
|
|
|
+
|
|
|
+ private requireNativeAuthStrategy() {
|
|
|
+ if (!this.nativeAuthStrategyIsConfigured) {
|
|
|
+ const authStrategyNames = this.configService.authOptions.shopAuthenticationStrategy
|
|
|
+ .map(s => s.name)
|
|
|
+ .join(', ');
|
|
|
+ const errorMessage =
|
|
|
+ 'This GraphQL operation requires that the NativeAuthenticationStrategy be configured for the Shop API.\n' +
|
|
|
+ `Currently the following AuthenticationStrategies are enabled: ${authStrategyNames}`;
|
|
|
+ Logger.error(errorMessage);
|
|
|
+ throw new InternalServerError('error.');
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|