Skip to content

Commit

Permalink
Merge branch 'main' into rn-getDeviceName
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinkumar6 authored Feb 17, 2024
2 parents 5255143 + 0d2aa5d commit b388b66
Show file tree
Hide file tree
Showing 82 changed files with 2,420 additions and 2,589 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = {
'packages/react-native',
'packages/rtn-push-notification',
'packages/rtn-web-browser',
'packages/storage',
// 'packages/storage',
],
rules: {
camelcase: [
Expand Down
1 change: 1 addition & 0 deletions .github/dependency-review/dependecy-review-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ allow-licenses:
- 'Unlicense'
- 'WTFPL'
- 'Zlib'
- 'CC0-1.0'
45 changes: 2 additions & 43 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ yarn run test --scope @aws-amplify/auth
**Yarn Linking**
The best way to develop locally and test is to link the individual package you’re working on and run lerna in watch mode.

Note: to test using the react-native framework you will need to use [Verdaccio](#verdaccio)

Run watch mode while editing (auth for example):

```
Expand All @@ -126,49 +128,6 @@ yarn link @aws-amplify/auth

Passing unit tests are only necessary if you’re looking to contribute a pull request. If you’re just playing locally, you don’t need them. However, if you’re contributing a pull request for anything other than making a change to the documentation, fixing a formatting issue in the code (i.e., white space, missing semi-colons) or another task that does not impact the functionality of the code, you will need to validate your proposed changes with passing unit tests.

**Using the setup-dev:react-native script to work with React-Native apps**

> Note: All the below commands to be run from the local amplify-js library root
To develop locally alongside a React-Native app, make sure to,

1. Finish the build steps mentioned in the section: `Setting up for local development` to set up your local `amplify-js` repository for development.

> Note: To set up a sample React-Native app -- configure your [development environment](https://reactnative.dev/docs/environment-setup) and [create an app](https://reactnative.dev/docs/environment-setup#creating-a-new-application). Note the path to the app as it is required in the next step.
2. Run the below command in the root of the amplify-js local repository with a package name (auth for example):
> Make sure to have [watchman](https://facebook.github.io/watchman/docs/install.html) installed before running the command below
```
npm run setup-dev:react-native -- --packages @aws-amplify/auth --target ~/path/to/your/rn/app/root
```

> Note: This script runs a continuous job in the newly opened tabs to watch, build and copy the changes unlike the usual linking method.
The options `--packages` is used to specify single or multiple package names and the `--target` option is used to specify the path to your sample React-Native app.
Optionally, you can use the shorthands flags `-p` and `-t` for packages and target path respectively.

> All scoped packages must be prefixed by `@aws-amplify/` . For example: `@aws-amplify/auth`
To develop multiple/all packages, provide the package names separated by a comma or the flag `--all` or `-a`:

```
npm run setup-dev:react-native -- --packages @aws-amplify/auth --target ~/path/to/your/rn/app/root
npm run setup-dev:react-native -- --all --target ~/path/to/your/rn/app/root
```

> Note: `--` right after the script name is important to provide the flags with their values.
**Debugging problems with the `setup-dev:react-native` script**

- If the WML command does not do anything after adding the links, watch its src file using watchman. Run the below from the root of this repository:

```
watchman watch node_modules/wml/src
```

- When using VScode, for the script to open a new terminal and tabs you will need to provide the editor accessiblity permissions.

#### Verdaccio

Verdaccio is a lightweight private npm proxy registry built in Node.js. Install [Verdaccio](https://verdaccio.org/docs/en/installation). You can setup Verdaccio to publish packages locally to test the changes.
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"description": "",
"scripts": {
"setup-dev": "yarn && yarn bootstrap && yarn link-all && yarn build",
"setup-dev:react-native": "node ./scripts/setup-dev-rn",
"bootstrap": "lerna bootstrap",
"test": "yarn test:no-datastore && yarn test:datastore && yarn test:license && yarn test:github-actions && yarn test:tsc-compliance",
"test:no-datastore": "lerna run test --stream --ignore @aws-amplify/datastore",
Expand Down Expand Up @@ -125,9 +124,7 @@
"uuid-validate": "^0.0.3",
"webpack": "^5.75.0",
"webpack-bundle-analyzer": "^4.7.0",
"webpack-cli": "^5.0.0",
"wml": "0.0.83",
"winston": "3.11.0"
"webpack-cli": "^5.0.0"
},
"resolutions": {
"@types/babel__traverse": "7.20.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { Hub } from '@aws-amplify/core';
import { AMPLIFY_SYMBOL } from '@aws-amplify/core/internals/utils';
import {
dispatchSignedInHubEvent,
ERROR_MESSAGE,
} from '../../../../src/providers/cognito/utils/dispatchSignedInHubEvent';
import { getCurrentUser } from '../../../../src/providers/cognito/apis/getCurrentUser';
import { assertAuthTokens } from '../../../../src/providers/cognito/utils/types';

jest.mock('../../../../src/providers/cognito/apis/getCurrentUser', () => ({
getCurrentUser: jest.fn(),
}));
jest.mock('@aws-amplify/core', () => ({
Hub: {
dispatch: jest.fn(),
},
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
AMPLIFY_SYMBOL: Symbol('AMPLIFY_SYMBOL'),
}));

const mockGetCurrentUser = getCurrentUser as jest.Mock;
const mockDispatch = Hub.dispatch as jest.Mock;

describe('dispatchSignedInHubEvent()', () => {
it('dispatches Hub event `signedIn` with `getCurrentUser()` returned data', async () => {
const mockGetCurrentUserPayload = {
username: 'hello',
userId: 'userId',
};
mockGetCurrentUser.mockResolvedValueOnce(mockGetCurrentUserPayload);

await dispatchSignedInHubEvent();

expect(mockDispatch).toHaveBeenCalledWith(
'auth',
{
event: 'signedIn',
data: mockGetCurrentUserPayload,
},
'Auth',
AMPLIFY_SYMBOL,
);
});

it('throws error when `getCurrentUser()` throws `USER_UNAUTHENTICATED_EXCEPTION`', () => {
mockGetCurrentUser.mockImplementationOnce(() => {
assertAuthTokens(null);
});

expect(() => dispatchSignedInHubEvent()).rejects.toThrow(ERROR_MESSAGE);
});

it('rethrows error if the error is not handled by itself', () => {
const mockError = new Error('some other error');

mockGetCurrentUser.mockImplementationOnce(() => {
throw mockError;
});

expect(() => dispatchSignedInHubEvent()).rejects.toThrow(mockError);
});
});
1 change: 1 addition & 0 deletions packages/auth/src/errors/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export const invalidOriginException = new AuthError({
});
export const OAUTH_SIGNOUT_EXCEPTION = 'OAuthSignOutException';
export const TOKEN_REFRESH_EXCEPTION = 'TokenRefreshException';
export const UNEXPECTED_SIGN_IN_INTERRUPTION_EXCEPTION = 'UnexpectedSignInInterruptionException';
13 changes: 4 additions & 9 deletions packages/auth/src/providers/cognito/apis/confirmSignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from '../utils/clients/CognitoIdentityProvider/types';
import { tokenOrchestrator } from '../tokenProvider';
import { getCurrentUser } from './getCurrentUser';
import { dispatchSignedInHubEvent } from '../utils/dispatchSignedInHubEvent';

/**
* Continues or completes the sign in process when required by the initial call to `signIn`.
Expand Down Expand Up @@ -122,15 +123,9 @@ export async function confirmSignIn(
),
signInDetails,
});
Hub.dispatch(
'auth',
{
event: 'signedIn',
data: await getCurrentUser(),
},
'Auth',
AMPLIFY_SYMBOL,
);

await dispatchSignedInHubEvent();

return {
isSignedIn: true,
nextStep: { signInStep: 'DONE' },
Expand Down
10 changes: 4 additions & 6 deletions packages/auth/src/providers/cognito/apis/signInWithCustomAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from '../utils/clients/CognitoIdentityProvider/types';
import { tokenOrchestrator } from '../tokenProvider';
import { getCurrentUser } from './getCurrentUser';
import { dispatchSignedInHubEvent } from '../utils/dispatchSignedInHubEvent';

/**
* Signs a user in using a custom authentication flow without password
Expand Down Expand Up @@ -98,12 +99,9 @@ export async function signInWithCustomAuth(
),
signInDetails,
});
Hub.dispatch(
'auth',
{ event: 'signedIn', data: await getCurrentUser() },
'Auth',
AMPLIFY_SYMBOL,
);

await dispatchSignedInHubEvent();

return {
isSignedIn: true,
nextStep: { signInStep: 'DONE' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
} from '../utils/clients/CognitoIdentityProvider/types';
import { tokenOrchestrator } from '../tokenProvider';
import { getCurrentUser } from './getCurrentUser';
import { dispatchSignedInHubEvent } from '../utils/dispatchSignedInHubEvent';

/**
* Signs a user in using a custom authentication flow with SRP
Expand Down Expand Up @@ -102,15 +103,9 @@ export async function signInWithCustomSRPAuth(
signInDetails,
});
cleanActiveSignInState();
Hub.dispatch(
'auth',
{
event: 'signedIn',
data: await getCurrentUser(),
},
'Auth',
AMPLIFY_SYMBOL,
);

await dispatchSignedInHubEvent();

return {
isSignedIn: true,
nextStep: { signInStep: 'DONE' },
Expand Down
13 changes: 4 additions & 9 deletions packages/auth/src/providers/cognito/apis/signInWithSRP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
import { cacheCognitoTokens } from '../tokenProvider/cacheTokens';
import { tokenOrchestrator } from '../tokenProvider';
import { getCurrentUser } from './getCurrentUser';
import { dispatchSignedInHubEvent } from '../utils/dispatchSignedInHubEvent';

/**
* Signs a user in
Expand Down Expand Up @@ -102,15 +103,9 @@ export async function signInWithSRP(
),
signInDetails,
});
Hub.dispatch(
'auth',
{
event: 'signedIn',
data: await getCurrentUser(),
},
'Auth',
AMPLIFY_SYMBOL,
);

await dispatchSignedInHubEvent();

return {
isSignedIn: true,
nextStep: { signInStep: 'DONE' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
import { cacheCognitoTokens } from '../tokenProvider/cacheTokens';
import { tokenOrchestrator } from '../tokenProvider';
import { getCurrentUser } from './getCurrentUser';
import { dispatchSignedInHubEvent } from '../utils/dispatchSignedInHubEvent';

/**
* Signs a user in using USER_PASSWORD_AUTH AuthFlowType
Expand Down Expand Up @@ -97,15 +98,9 @@ export async function signInWithUserPassword(
signInDetails,
});
cleanActiveSignInState();
Hub.dispatch(
'auth',
{
event: 'signedIn',
data: await getCurrentUser(),
},
'Auth',
AMPLIFY_SYMBOL,
);

await dispatchSignedInHubEvent();

return {
isSignedIn: true,
nextStep: { signInStep: 'DONE' },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { Hub } from '@aws-amplify/core';
import { getCurrentUser } from '../apis/getCurrentUser';
import { AMPLIFY_SYMBOL } from '@aws-amplify/core/internals/utils';
import {
UNEXPECTED_SIGN_IN_INTERRUPTION_EXCEPTION,
USER_UNAUTHENTICATED_EXCEPTION,
} from '../../../errors/constants';
import { AuthError } from '../../../errors/AuthError';

export const ERROR_MESSAGE =
'Unable to get user session following successful sign-in.';

export const dispatchSignedInHubEvent = async () => {
try {
Hub.dispatch(
'auth',
{
event: 'signedIn',
data: await getCurrentUser(),
},
'Auth',
AMPLIFY_SYMBOL,
);
} catch (error) {
if ((error as AuthError).name === USER_UNAUTHENTICATED_EXCEPTION) {
throw new AuthError({
name: UNEXPECTED_SIGN_IN_INTERRUPTION_EXCEPTION,
message: ERROR_MESSAGE,
recoverySuggestion:
'This most likely is due to auth tokens not being persisted. If you are using cookie store, please ensure cookies can be correctly set from your server.',
});
}

throw error;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { cacheCognitoTokens } from '../../tokenProvider/cacheTokens';
import { getCurrentUser } from '../../apis/getCurrentUser';
import { createOAuthError } from './createOAuthError';
import { cognitoUserPoolsTokenProvider } from '../../tokenProvider';
import { dispatchSignedInHubEvent } from '../dispatchSignedInHubEvent';

export const completeOAuthFlow = async ({
currentUrl,
Expand Down Expand Up @@ -250,12 +251,7 @@ const completeFlow = async ({
);
}
Hub.dispatch('auth', { event: 'signInWithRedirect' }, 'Auth', AMPLIFY_SYMBOL);
Hub.dispatch(
'auth',
{ event: 'signedIn', data: await getCurrentUser() },
'Auth',
AMPLIFY_SYMBOL,
);
await dispatchSignedInHubEvent();
clearHistory(redirectUri);
};

Expand Down
Loading

0 comments on commit b388b66

Please sign in to comment.