Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No merge/mi 121 login service override issue #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/mesh/bigcommerce/src/application.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createApplication } from 'graphql-modules';
import { createBigCommerceModule } from '@aligent/bigcommerce-graphql-module';
import { createApplication, Scope } from 'graphql-modules';
import { createBigCommerceModule, ClientLoginService } from '@aligent/bigcommerce-graphql-module';
import { createAuthModule, LoginService } from '@aligent/auth-module';

export default createApplication({
modules: [
Expand All @@ -11,5 +12,22 @@ export default createApplication({
clientId: process.env.BC_CLIENT_ID as string,
storeHash: process.env.STORE_HASH as string,
}),
createAuthModule({
dynamoDbRegion: process.env.DYNAMO_DB__REGION as string,
dynamoDbAuthTable: process.env.DYNAMO_DB_AUTH_TABLE as string,
extendRefreshTokenExpiryInMinutes: 43200 | (30 * 24 * 60), // The time in minutes an extended user session should end in - optional
nonExtendRefreshTokenExpiryInMinutes: 15, // The time in minutes a non-extended user session should end in - optional
accessTokenExpiryInMinutes: 10, // The time in minutes an access token is valid for - optional
}),
],
// This works when both Auth and BigCommerce modules don't define the loginServices in the
// there local providers.
// providers: [
// {
// useClass: ClientLoginService,
// provide: LoginService,
// scope: Scope.Singleton,
// global: true,
// },
// ],
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { LoginService } from '../../services/login-service';
export const generateCustomerTokenResolver = {
resolve: async (_root, args, context, _info) => {
const loginService = context.injector.get(LoginService);
console.dir('Auth before login Service');
const entityId = await loginService.login(args);
console.dir('Auth after login Service');

const isExtendedLogin = !!args?.remember_me;

Expand Down
6 changes: 6 additions & 0 deletions packages/modules/auth/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Provider, Scope } from 'graphql-modules';
import { ModuleConfigToken } from '../providers';
import { AuthService } from './auth';
import { AuthTokenService } from './auth-tokens';
import { LoginService } from './login-service';
export * from './auth';
export * from './auth-tokens';
export * from './login-service';
Expand All @@ -20,5 +21,10 @@ export const getServices = (): Array<Provider> => {
deps: [ModuleConfigToken],
scope: Scope.Singleton,
},
{
useClass: LoginService,
provide: LoginService,
scope: Scope.Singleton,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tvhees I'd expect this update in combination as the ones in packages/modules/bigcommerce/src/services/index.ts and packages/modules/bigcommerce/src/services/login-service.ts to allow overriding classes.

];
};
2 changes: 1 addition & 1 deletion packages/modules/auth/src/services/login-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from 'graphql-modules';
import { Injectable, Scope } from 'graphql-modules';
import { CustomerId } from '../types';

/**
Expand Down
14 changes: 14 additions & 0 deletions packages/modules/bigcommerce/release-notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# BigCommerce GraphQl Module Release Notes

## bigcommerce-graphql-module-1.0.8

#### Changes:

- Updates the "generateMeshToken" function to return a "customer_id" property instead of "bc_customer_id".
Updates the "getBcCustomerIdFromMeshToken" function to look for a "customer_id" property instead of "bc_customer_id".
This is due to the Auth Module generating a JWT containing a "customer_id" property
but the BigCommerce Module decodes the JWT and looks for a "bc_customer_id" property.

#### Tickets

- MI-121: Auth module authentication issue
- https://aligent.atlassian.net/browse/MI-121

## bigcommerce-graphql-module-1.0.6

#### Changes:
Expand Down
1 change: 1 addition & 0 deletions packages/modules/bigcommerce/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export * from './plugins/add-ip-address-to-axios-headers';
export * from './apis/graphql';
export * from './apis/rest';
export * from './utils';
export * from './services';

// Export Globally accessible DI Tokens so other modules can use them
export { ModuleConfig } from './providers';
Expand Down
3 changes: 2 additions & 1 deletion packages/modules/bigcommerce/src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InjectionToken, Provider, Scope } from 'graphql-modules';
import { BigCommerceModuleConfig } from '../index';
import { BigCommerceModuleConfig, getServices } from '../index';
import { Get, Paths } from 'type-fest';
import { BigCommerceGraphQlClient } from '../clients';

Expand Down Expand Up @@ -38,5 +38,6 @@ export const getProviders = (config: BigCommerceModuleConfig): Array<Provider> =
global: true,
},
BigCommerceGraphQlClient,
...getServices(),
];
};
14 changes: 14 additions & 0 deletions packages/modules/bigcommerce/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Provider } from 'graphql-modules';
import { LoginService } from '@aligent/auth-module';
import { ClientLoginService } from './login-service';

export * from './login-service';

export const getServices = (): Array<Provider> => {
return [
{
useClass: ClientLoginService,
provide: LoginService,
},
];
};
16 changes: 16 additions & 0 deletions packages/modules/bigcommerce/src/services/login-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Injectable } from 'graphql-modules';
import { CustomerId, LoginService } from '@aligent/auth-module';

@Injectable()
export class ClientLoginService extends LoginService {
constructor() {
super();
}

override async login(args: { email: string; password: string }): Promise<CustomerId> {
console.dir('LoginService override is working');

// Implement your login logic here
return Promise.resolve(98);
}
}
2 changes: 1 addition & 1 deletion packages/modules/bigcommerce/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export interface DecodedCustomerImpersonationToken {
}

export interface MeshToken {
bc_customer_id: number;
customer_id: number;
iat: number;
exp: number;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/modules/bigcommerce/src/utils/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ export const getDecodedCustomerImpersonationToken = (
};

/**
* Attempts to extract "bc_customer_id" for the mesh token or throws an error
* Attempts to extract "customer_id" for the mesh token or throws an error
* @param meshToken
*/
export const getBcCustomerIdFromMeshToken = (meshToken: string): number => {
try {
if (meshToken?.toLowerCase().startsWith('bearer')) {
const splitMeshToken = meshToken.split(' ')[1];
const decodedMeshToken = verify(splitMeshToken, JWT_PRIVATE_KEY) as MeshToken;
return decodedMeshToken.bc_customer_id;
return decodedMeshToken.customer_id;
} else {
throw new Error(`Need to send Bearer token`);
}
Expand All @@ -48,13 +48,13 @@ export const getBcCustomerIdFromMeshToken = (meshToken: string): number => {
};

/**
* Creates a token when a user logs in also stores the bc_customer_id in the payload
* Creates a token when a user logs in also stores the customer_id in the payload
* which can be used for later request to the Mesh.
* @param {number} entityId - Bc User Id returned from logging in
*/
export const generateMeshToken = (entityId: number): string => {
const payload = {
bc_customer_id: entityId,
customer_id: entityId,
exp: getUnixTimeStampInSecondsForMidnightTonight(),
};

Expand Down
Loading