Skip to content

Commit 5c8bcc5

Browse files
authored
Merge pull request #153 from ModusCreateOrg/NO-TICKET-FIX-COGNITO
fix: update type definitions and improve user data extraction from Cognito
2 parents ee4df6c + 19b3d96 commit 5c8bcc5

File tree

8 files changed

+60
-23
lines changed

8 files changed

+60
-23
lines changed

frontend/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@
8989
"@types/eslint": "^9.6.1",
9090
"@types/eslint__js": "9.14.0",
9191
"@types/lodash": "4.17.16",
92-
"@types/react": "19.1.2",
93-
"@types/react-dom": "19.1.3",
92+
"@types/react": "^19.1.2",
93+
"@types/react-dom": "^19.1.3",
9494
"@types/react-router": "5.1.20",
9595
"@types/react-router-dom": "5.3.3",
9696
"@types/uuid": "^10.0.0",

frontend/src/common/api/useGetCurrentUser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const useGetCurrentUser = () => {
2828
username: cognitoUser.username || '',
2929
attributes: {
3030
// Extract whatever attributes are available from the user object
31-
email: cognitoUser.signInDetails?.loginId || '',
31+
email: cognitoUser.attributes.email || '',
3232
},
3333
};
3434

frontend/src/common/hooks/__tests__/useAuthOperations.test.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import * as AuthErrorUtils from 'common/utils/auth-errors';
66
import { AuthError, UserTokens } from 'common/models/auth';
77
import { CognitoUser } from 'common/models/user';
88
import * as UserMapper from 'common/utils/user-mapper';
9+
import {
10+
ConfirmSignUpCommandOutput,
11+
ResendConfirmationCodeCommandOutput,
12+
SignUpCommandOutput,
13+
} from '@aws-sdk/client-cognito-identity-provider';
914

1015
// Mock the DirectCognitoAuthService
1116
vi.mock('common/services/auth/direct-cognito-auth-service', () => ({
@@ -110,12 +115,11 @@ describe('useAuthOperations', () => {
110115

111116
describe('signUp', () => {
112117
it('should call DirectCognitoAuthService.signUp with correct parameters', async () => {
113-
const mockSignUpResult = {
114-
username: '[email protected]',
115-
userConfirmed: false,
116-
isSignUpComplete: false,
117-
nextStep: { signUpStep: 'CONFIRM_SIGN_UP' },
118-
} as unknown;
118+
const mockSignUpResult: SignUpCommandOutput = {
119+
UserConfirmed: false,
120+
UserSub: 'test-sub',
121+
$metadata: {},
122+
};
119123

120124
vi.mocked(DirectCognitoAuthService.signUp).mockResolvedValueOnce(mockSignUpResult);
121125

@@ -174,10 +178,9 @@ describe('useAuthOperations', () => {
174178

175179
describe('confirmSignUp', () => {
176180
it('should call DirectCognitoAuthService.confirmSignUp with correct parameters', async () => {
177-
const mockConfirmResult = {
178-
isSignUpComplete: true,
179-
nextStep: { signUpStep: 'DONE' },
180-
} as unknown;
181+
const mockConfirmResult: ConfirmSignUpCommandOutput = {
182+
$metadata: {},
183+
};
181184

182185
vi.mocked(DirectCognitoAuthService.confirmSignUp).mockResolvedValueOnce(mockConfirmResult);
183186

@@ -198,11 +201,14 @@ describe('useAuthOperations', () => {
198201

199202
describe('resendConfirmationCode', () => {
200203
it('should call DirectCognitoAuthService.resendConfirmationCode with correct email', async () => {
201-
const mockResult = {
202-
deliveryMedium: 'EMAIL',
203-
destination: '[email protected]',
204-
attributeName: 'email',
205-
} as unknown;
204+
const mockResult: ResendConfirmationCodeCommandOutput = {
205+
CodeDeliveryDetails: {
206+
AttributeName: 'email',
207+
DeliveryMedium: 'EMAIL',
208+
Destination: '[email protected]',
209+
},
210+
$metadata: {},
211+
};
206212

207213
vi.mocked(DirectCognitoAuthService.resendConfirmationCode).mockResolvedValueOnce(mockResult);
208214

@@ -231,7 +237,16 @@ describe('useAuthOperations', () => {
231237
const mockUser = { id: 'mock-id', username: '[email protected]', email: '[email protected]' };
232238

233239
// First, we need to set up the user by simulating a successful sign-in
234-
vi.mocked(DirectCognitoAuthService.signIn).mockResolvedValueOnce({});
240+
const mockTokens: UserTokens = {
241+
access_token: 'token',
242+
id_token: 'id-token',
243+
refresh_token: 'refresh-token',
244+
token_type: 'bearer',
245+
expires_in: 3600,
246+
expires_at: new Date().toISOString(),
247+
};
248+
249+
vi.mocked(DirectCognitoAuthService.signIn).mockResolvedValueOnce(mockTokens);
235250
vi.mocked(UserMapper.mapCognitoUserToAppUser).mockReturnValueOnce(mockUser as CognitoUser);
236251

237252
await act(async () => {

frontend/src/common/models/user.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ export type User = {
3535
company: Company;
3636
};
3737

38+
/**
39+
* Login details from Cognito
40+
*/
41+
export type SignInDetails = {
42+
loginId?: string;
43+
// Add any other properties that might be needed
44+
};
45+
3846
/**
3947
* Cognito User type aligned with AWS Cognito attributes
4048
*/
@@ -63,4 +71,6 @@ export type CognitoUser = {
6371
createdAt?: string;
6472
// Updated date
6573
updatedAt?: string;
74+
// Sign-in details (optional)
75+
signInDetails?: SignInDetails;
6676
};

frontend/src/common/providers/AuthProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ const AuthProvider = ({ children }: PropsWithChildren): JSX.Element => {
126126
const userData = {
127127
username: currentUser.username || '',
128128
attributes: {
129-
email: currentUser.signInDetails?.loginId || '',
129+
email: currentUser.attributes.email || '',
130130
given_name: currentUser.username?.split('@')[0] || '',
131131
family_name: '',
132132
},

frontend/src/types/react.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference types="react" />
2+
3+
declare namespace JSX {
4+
// Added a property to avoid the "interface declaring no members is equivalent to its supertype" error
5+
interface Element
6+
extends React.ReactElement<React.ComponentProps<React.ElementType>, React.ElementType> {
7+
_jsx_element?: true;
8+
}
9+
interface IntrinsicElements {
10+
[elemName: string]: React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
11+
}
12+
}

frontend/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"isolatedModules": true,
1616
"noEmit": true,
1717
"jsx": "react-jsx",
18-
"types": ["eslint"],
18+
"types": ["eslint", "react", "react-dom"],
1919

2020
/* Absolute imports */
2121
"paths": {

0 commit comments

Comments
 (0)