Skip to content

Commit

Permalink
fix: resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sridhar committed Sep 13, 2023
2 parents 85ff181 + 7c21708 commit cd83dda
Show file tree
Hide file tree
Showing 63 changed files with 810 additions and 450 deletions.
44 changes: 32 additions & 12 deletions packages/adapter-nextjs/__tests__/utils/getAmplifyConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { NextConfig } from 'next';
import getConfig from 'next/config';
import { getAmplifyConfig } from '../../src/utils/getAmplifyConfig';
import { AmplifyError } from '@aws-amplify/core/internals/utils';

jest.mock('next/config');

const mockGetConfig = getConfig as jest.Mock;

describe('getAmplifyConfig', () => {
const mockAmplifyConfig = {
Auth: {
identityPoolId: '123',
userPoolId: 'abc',
userPoolWebClientId: 'def',
},
Storage: {
bucket: 'bucket',
region: 'us-east-1',
},
};

beforeEach(() => {
mockGetConfig.mockReturnValue({});
delete process.env.amplifyConfig;
});

it('should return amplifyConfig from env vars', () => {
const mockAmplifyConfig = {
Auth: {
identityPoolId: '123',
userPoolId: 'abc',
userPoolWebClientId: 'def',
},
Storage: {
bucket: 'bucket',
region: 'us-east-1',
},
};
process.env.amplifyConfig = JSON.stringify(mockAmplifyConfig);

const result = getAmplifyConfig();
expect(result).toEqual(mockAmplifyConfig);
});

it('should attempt to get amplifyConfig via getConfig provided by Next.js as a fallback', () => {
mockGetConfig.mockReturnValueOnce({
serverRuntimeConfig: {
amplifyConfig: JSON.stringify(mockAmplifyConfig),
},
});

const result = getAmplifyConfig();
expect(result).toEqual(mockAmplifyConfig);
});

it('should throw error when amplifyConfig is not found from env vars', () => {
expect(() => getAmplifyConfig()).toThrowError();
expect(() => getAmplifyConfig()).toThrow(AmplifyError);
});
});
10 changes: 10 additions & 0 deletions packages/adapter-nextjs/__tests__/withAmplify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe('withAmplify', () => {
env: {
amplifyConfig: JSON.stringify(mockAmplifyConfig),
},
serverRuntimeConfig: {
amplifyConfig: JSON.stringify(mockAmplifyConfig),
},
});
});

Expand All @@ -37,6 +40,9 @@ describe('withAmplify', () => {
env: {
existingKey: '123',
},
serverRuntimeConfig: {
myKey: 'myValue',
},
};
const result = withAmplify(nextConfig, mockAmplifyConfig);

Expand All @@ -45,6 +51,10 @@ describe('withAmplify', () => {
existingKey: '123',
amplifyConfig: JSON.stringify(mockAmplifyConfig),
},
serverRuntimeConfig: {
myKey: 'myValue',
amplifyConfig: JSON.stringify(mockAmplifyConfig),
},
});
});
});
12 changes: 11 additions & 1 deletion packages/adapter-nextjs/src/utils/getAmplifyConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@

import { ResourcesConfig } from 'aws-amplify';
import { AmplifyServerContextError } from '@aws-amplify/core/internals/adapter-core';
import getConfig from 'next/config';

export const getAmplifyConfig = (): ResourcesConfig => {
const configStr = process.env.amplifyConfig;
let configStr = process.env.amplifyConfig;

// With a Next.js app that uses the Pages Router, the key-value pairs
// listed under the `env` field in the `next.config.js` is not accessible
// via process.env.<key> at some occasion. Using the following as a fallback
// See: https://github.com/vercel/next.js/issues/39299
if (!configStr) {
const { serverRuntimeConfig } = getConfig();
configStr = serverRuntimeConfig?.amplifyConfig;
}

if (!configStr) {
throw new AmplifyServerContextError({
Expand Down
8 changes: 7 additions & 1 deletion packages/adapter-nextjs/src/withAmplify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ export const withAmplify = (
nextConfig: NextConfig,
amplifyConfig: ResourcesConfig
) => {
const configStr = JSON.stringify(amplifyConfig);
nextConfig.env = {
...nextConfig.env,
amplifyConfig: JSON.stringify(amplifyConfig),
amplifyConfig: configStr,
};

nextConfig.serverRuntimeConfig = {
...nextConfig.serverRuntimeConfig,
amplifyConfig: configStr,
};

return nextConfig;
Expand Down
7 changes: 7 additions & 0 deletions packages/auth/cognito/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@aws-amplify/auth/cognito/server",
"main": "../../lib/providers/cognito/apis/server/index.js",
"browser": "../../lib-esm/providers/cognito/apis/server/index.js",
"module": "../../lib-esm/providers/cognito/apis/server/index.js",
"typings": "../../lib-esm/providers/cognito/apis/server/index.d.ts"
}
18 changes: 17 additions & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
],
"cognito": [
"./lib-esm/providers/cognito/index.d.ts"
],
"cognito/server": [
"./lib-esm/providers/cognito/apis/server/index.d.ts"
],
"server": [
"./lib-esm/server.d.ts"
],
"internals": [
"./lib-esm/lib-esm/internals/index.d.ts"
]
}
},
Expand All @@ -48,6 +57,11 @@
"import": "./lib-esm/providers/cognito/index.js",
"require": "./lib/providers/cognito/index.js"
},
"./cognito/server": {
"types": "./lib-esm/providers/cognito/apis/server/index.d.ts",
"import": "./lib-esm/providers/cognito/apis/server/index.js",
"require": "./lib/providers/cognito/apis/server/index.js"
},
"./server": {
"types": "./lib-esm/server.d.ts",
"import": "./lib-esm/server.js",
Expand All @@ -74,7 +88,9 @@
"lib",
"lib-esm",
"src",
"internals"
"internals",
"cognito",
"server"
],
"dependencies": {
"@smithy/util-base64": "2.0.0",
Expand Down
76 changes: 40 additions & 36 deletions packages/auth/src/providers/cognito/apis/signInWithRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function signInWithRedirect(
const authConfig = Amplify.getConfig().Auth?.Cognito;
assertTokenProviderConfig(authConfig);
assertOAuthConfig(authConfig);

store.setAuthConfig(authConfig);
let provider = 'COGNITO'; // Default

if (typeof signInWithRedirectRequest?.provider === 'string') {
Expand Down Expand Up @@ -347,45 +347,49 @@ function validateState(state?: string | null): asserts state {
}
}

async function parseRedirectURL() {
const authConfig = Amplify.getConfig().Auth?.Cognito;
try {
assertTokenProviderConfig(authConfig);
store.setAuthConfig(authConfig);
} catch (_err) {
// Token provider not configure nothing to do
return;
}

// No OAuth inflight doesnt need to parse the url
if (!(await store.loadOAuthInFlight())) {
return;
}
try {
assertOAuthConfig(authConfig);
} catch (err) {
// TODO(v6): this should warn you have signInWithRedirect but is not configured
return;
}

try {
const url = window.location.href;

handleAuthResponse({
currentUrl: url,
clientId: authConfig.userPoolClientId,
domain: authConfig.loginWith.oauth.domain,
redirectUri: authConfig.loginWith.oauth.redirectSignIn[0],
responseType: authConfig.loginWith.oauth.responseType,
userAgentValue: getAmplifyUserAgent(),
});
} catch (err) {
// is ok if there is not OAuthConfig
}
}

function urlListener() {
// Listen configure to parse url
// TODO(v6): what happens if configure gets called multiple times during code exchange
parseRedirectURL();
Hub.listen('core', async capsule => {
if (capsule.payload.event === 'configure') {
const authConfig = Amplify.getConfig().Auth?.Cognito;
try {
assertTokenProviderConfig(authConfig);
store.setAuthConfig(authConfig);
} catch (_err) {
// Token provider not configure nothing to do
return;
}

// No OAuth inflight doesnt need to parse the url
if (!(await store.loadOAuthInFlight())) {
return;
}
try {
assertOAuthConfig(authConfig);
} catch (err) {
// TODO(v6): this should warn you have signInWithRedirect but is not configured
return;
}

try {
const url = window.location.href;

handleAuthResponse({
currentUrl: url,
clientId: authConfig.userPoolClientId,
domain: authConfig.loginWith.oauth.domain,
redirectUri: authConfig.loginWith.oauth.redirectSignIn[0],
responseType: authConfig.loginWith.oauth.responseType,
userAgentValue: getAmplifyUserAgent(),
});
} catch (err) {
// is ok if there is not OAuthConfig
}
parseRedirectURL();
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/src/providers/cognito/apis/verifyTOTPSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { AuthValidationErrorCode } from '../../../errors/types/validation';
import { assertValidationError } from '../../../errors/utils/assertValidationError';
import { VerifyTOTPSetupRequest } from '../../../types/requests';
import { CogntioVerifyTOTPSetupOptions } from '../types/options';
import { CognitoVerifyTOTPSetupOptions } from '../types/options';
import { verifySoftwareToken } from '../utils/clients/CognitoIdentityProvider';
import { VerifySoftwareTokenException } from '../types/errors';
import { Amplify } from '@aws-amplify/core';
Expand All @@ -27,7 +27,7 @@ import { assertAuthTokens } from '../utils/types';
* @throws AuthTokenConfigException - Thrown when the token provider config is invalid.
*/
export async function verifyTOTPSetup(
verifyTOTPSetupRequest: VerifyTOTPSetupRequest<CogntioVerifyTOTPSetupOptions>
verifyTOTPSetupRequest: VerifyTOTPSetupRequest<CognitoVerifyTOTPSetupOptions>
): Promise<void> {
const authConfig = Amplify.getConfig().Auth?.Cognito;
assertTokenProviderConfig(authConfig);
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/providers/cognito/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export {
CognitoConfirmSignUpOptions,
CognitoConfirmSignInOptions,
CognitoUpdateUserAttributesOptions,
CogntioVerifyTOTPSetupOptions,
CognitoVerifyTOTPSetupOptions,
} from './options';

export { UpdateMFAPreferenceRequest } from './requests';
2 changes: 1 addition & 1 deletion packages/auth/src/providers/cognito/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export type CognitoConfirmSignInOptions<
/**
* Options specific to a Cognito Verify TOTP Setup request.
*/
export type CogntioVerifyTOTPSetupOptions = {
export type CognitoVerifyTOTPSetupOptions = {
friendlyDeviceName?: string;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ const mockKeyValueStorage: KeyValueStorageInterface = {
clear: jest.fn(),
};
const mockAuthConfig: AuthConfig = {
identityPoolId: '123',
userPoolId: 'abc',
userPoolWebClientId: 'def',
Cognito: {
identityPoolId: '123',
userPoolId: 'abc',
userPoolClientId: 'def',
},
};
const MockDefaultTokenStore = DefaultTokenStore as jest.Mock;
const MockTokenOrchestrator = TokenOrchestrator as jest.Mock;
Expand Down
3 changes: 3 additions & 0 deletions packages/aws-amplify/__tests__/exports.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import * as exported from '../src';

describe('aws-amplify', () => {
Expand Down
Loading

0 comments on commit cd83dda

Please sign in to comment.