forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into rn-getDeviceName
- Loading branch information
Showing
82 changed files
with
2,420 additions
and
2,589 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ allow-licenses: | |
- 'Unlicense' | ||
- 'WTFPL' | ||
- 'Zlib' | ||
- 'CC0-1.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/auth/__tests__/providers/cognito/utils/dispatchSignedInHubEvent.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/auth/src/providers/cognito/utils/dispatchSignedInHubEvent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.