Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
fix: queryparams clean f nextjs + move async logic from constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
einaralex committed Nov 6, 2023
1 parent b7ae1b4 commit 9304373
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
42 changes: 29 additions & 13 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ export class MoneriumClient {
/** Constructor with {@link ClassOptions} */
constructor(options: ClassOptions);
constructor(envOrOptions?: ENV | ClassOptions) {
// TODO: CHECK HERE IF REFRESH TOKEN EXISTS
// TODO: CHECK HERE IF REFRESH TOKEN EXISTS
// TODO: CHECK HERE IF REFRESH TOKEN EXISTS
// TODO: CHECK HERE IF REFRESH TOKEN EXISTS
// No arguments, default to sandbox
if (!envOrOptions) {
this.#env = MONERIUM_CONFIG.environments['sandbox'];
Expand All @@ -92,14 +88,12 @@ export class MoneriumClient {
clientId: clientId as string,
redirectUrl: redirectUrl as string,
};
this.connect(this.#client);
} else {
const { clientId, clientSecret } = envOrOptions as ClientCredentials;
this.#client = {
clientId: clientId as string,
clientSecret: clientSecret as string,
};
this.#clientCredentialsAuthorization(this.#client);
}
}
}
Expand Down Expand Up @@ -136,12 +130,23 @@ export class MoneriumClient {
}

// TODO: TEST auto link & manual link + address
async connect(client: AuthorizationCodeCredentials) {
const clientId =
client?.clientId ||
(this.#client as AuthorizationCodeCredentials)?.clientId;
async connect(client: AuthorizationCodeCredentials | ClientCredentials) {
const clientId = client?.clientId || this.#client?.clientId;
const clientSecret =
(client as ClientCredentials)?.clientSecret ||
(this.#client as ClientCredentials)?.clientSecret;

if (clientSecret) {
if (!isServer) {
throw new Error('Only use client credentials on server side');
}
return this.#clientCredentialsAuthorization(
this.#client as ClientCredentials,
);
}

const redirectUrl =
client?.redirectUrl ||
(client as AuthorizationCodeCredentials)?.redirectUrl ||
(this.#client as AuthorizationCodeCredentials)?.redirectUrl;

if (!clientId) {
Expand Down Expand Up @@ -226,6 +231,15 @@ export class MoneriumClient {
// }
});

/**
* Remove auth code from URL.
* Make sure this is the last action before returning the bearer profile
* NextJS seems to overwrite this if there is data fetching in the background
*/
if (isAuthCode(args)) {
cleanQueryString();
}

return this.bearerProfile as BearerProfile;
}

Expand Down Expand Up @@ -359,13 +373,15 @@ export class MoneriumClient {
) => {
const codeVerifier = sessionStorage.getItem(STORAGE_CODE_VERIFIER) || '';

if (!codeVerifier) {
throw new Error('Code verifier not found');
}

/** @deprecated, use sessionStorage */
this.codeVerifier = codeVerifier;

sessionStorage.removeItem(STORAGE_CODE_VERIFIER);
// Remove auth code from URL.
cleanQueryString();

return await this.getBearerToken({
code: authCode,
redirect_uri: redirectUrl as string,
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/auth.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
PKCERequest,
PKCERequestArgs,
RefreshTokenRequest,
} from 'types';
import { getChain, getNetwork, urlEncoded } from 'utils';
} from '../types';
import { getChain, getNetwork, urlEncoded } from '../utils';
import encodeBase64Url from 'crypto-js/enc-base64url';
import SHA256 from 'crypto-js/sha256';
import { STORAGE_CODE_VERIFIER } from '../constants';
Expand Down
37 changes: 19 additions & 18 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,25 @@ describe('MoneriumClient', () => {
replaceMock.mockRestore();
});

test('class instance with refresh_token', async () => {
const connectMock = jest
.spyOn(MoneriumClient.prototype, 'connect')
.mockImplementation();
const client = new MoneriumClient({
env: 'production',
clientId: 'testClientId',
clientSecret: 'testSecret',
});

expect(connectMock).toHaveBeenCalled();
expect(client.getEnvironment()).toEqual({
api: 'https://api.monerium.app',
web: 'https://monerium.app',
wss: 'wss://api.monerium.app',
});
connectMock.mockRestore();
});
// This is no longer valid, we were calling connect on the constructor, but there was no way to wait for the promise to resolve
// test('class instance with refresh_token', async () => {
// const connectMock = jest
// .spyOn(MoneriumClient.prototype, 'connect')
// .mockImplementation();
// const client = new MoneriumClient({
// env: 'production',
// clientId: 'testClientId',
// clientSecret: 'testSecret',
// });

// expect(connectMock).toHaveBeenCalled();
// expect(client.getEnvironment()).toEqual({
// api: 'https://api.monerium.app',
// web: 'https://monerium.app',
// wss: 'wss://api.monerium.app',
// });
// connectMock.mockRestore();
// });

test('authenticate with client credentials', async () => {
const client = new MoneriumClient();
Expand Down

0 comments on commit 9304373

Please sign in to comment.