Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit 66b5550

Browse files
Francesco Novymydea
authored andcommitted
feat: Ensure better type safety
1 parent b8b84d5 commit 66b5550

7 files changed

Lines changed: 53 additions & 30 deletions

File tree

‎addon/errors/cognito.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UserAttributes } from "ember-cognito-identity/services/cognito";
2+
13
export interface AmazonCognitoIdentityJsError {
24
message: string;
35
name: string;
@@ -36,10 +38,10 @@ export class CognitoError extends Error {
3638

3739
// Note: This error is never thrown by dispatchError, but only manually in cognito._authenticate()
3840
export class NewPasswordRequiredError extends CognitoError {
39-
userAttributes: Object;
40-
requiredAttributes: Object;
41+
userAttributes: UserAttributes;
42+
requiredAttributes: UserAttributes;
4143

42-
constructor(userAttributes: Object, requiredAttributes: Object) {
44+
constructor(userAttributes: UserAttributes, requiredAttributes: UserAttributes) {
4345
super('A new password must be set.');
4446

4547
this.name = 'NewPasswordRequiredError';

‎addon/services/cognito.ts‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ import { timeout } from 'ember-concurrency';
2424

2525
interface CognitoData {
2626
cognitoUser: CognitoUser;
27-
userAttributes: Object;
27+
userAttributes: UserAttributes;
2828
cognitoUserSession: CognitoUserSession;
2929
jwtToken: string;
3030
}
3131

32+
export type UserAttributes = { [key: string]: any };
33+
3234
export default class CognitoService extends Service {
3335
@service router: RouterService;
3436

@@ -127,7 +129,7 @@ export default class CognitoService extends Service {
127129
}
128130

129131
let promise = new RSVPPromise((resolve, reject) => {
130-
this.cognitoData.cognitoUser.refreshSession(refreshToken, (error) => {
132+
this.cognitoData!.cognitoUser.refreshSession(refreshToken, (error) => {
131133
if (error) {
132134
return reject(dispatchError(error));
133135
}
@@ -140,26 +142,32 @@ export default class CognitoService extends Service {
140142
return promise;
141143
}
142144

143-
async _loadUserDataAndAccessToken(): Promise<any> {
145+
async _loadUserDataAndAccessToken(): Promise<CognitoData | null> {
144146
let { userPool } = this;
145147

146-
let promise = new RSVPPromise((resolve, reject) => {
147-
let cognitoUser = userPool.getCurrentUser();
148-
if (!cognitoUser) {
149-
return reject(null);
150-
}
148+
let cognitoUser = userPool.getCurrentUser();
149+
if (!cognitoUser) {
150+
return null;
151+
}
152+
153+
return this._loadCognitoUserSession(cognitoUser);
154+
}
151155

156+
async _loadCognitoUserSession(
157+
cognitoUser: CognitoUser
158+
): Promise<CognitoData> {
159+
let promise: Promise<CognitoData> = new RSVPPromise((resolve, reject) => {
152160
cognitoUser.getSession(
153161
(
154162
error: AmazonCognitoIdentityJsError | null,
155163
cognitoUserSession: CognitoUserSession
156164
) => {
157165
if (error) {
158-
cognitoUser.signOut();
166+
cognitoUser!.signOut();
159167
return reject(dispatchError(error));
160168
}
161169

162-
this._getUserAttributes(cognitoUser).then(
170+
this._getUserAttributes(cognitoUser!).then(
163171
(userAttributes) => {
164172
let jwtToken = cognitoUserSession.getAccessToken().getJwtToken();
165173

@@ -206,7 +214,7 @@ export default class CognitoService extends Service {
206214
await this.restoreAndLoad();
207215
await this.onAuthenticated();
208216

209-
return this.cognitoData;
217+
return this.cognitoData!;
210218
}
211219

212220
/*
@@ -228,7 +236,10 @@ export default class CognitoService extends Service {
228236
}): Promise<any> {
229237
assert('cognitoData is already setup', !this.cognitoData);
230238

231-
cognitoUser = cognitoUser || this._createCognitoUser({ username });
239+
let actualCognitoUser =
240+
typeof cognitoUser === 'undefined'
241+
? this._createCognitoUser({ username })
242+
: cognitoUser;
232243

233244
let authenticationData = {
234245
Username: username,
@@ -237,7 +248,7 @@ export default class CognitoService extends Service {
237248
let authenticationDetails = new AuthenticationDetails(authenticationData);
238249

239250
let promise = new RSVPPromise((resolve, reject) => {
240-
cognitoUser.authenticateUser(authenticationDetails, {
251+
actualCognitoUser.authenticateUser(authenticationDetails, {
241252
onSuccess: () => {
242253
resolve(cognitoUser);
243254
},
@@ -275,6 +286,10 @@ export default class CognitoService extends Service {
275286

276287
invalidateAccessTokens(): Promise<any> {
277288
let promise = new RSVPPromise((resolve, reject) => {
289+
if (!this.cognitoData) {
290+
return reject();
291+
}
292+
278293
this.cognitoData.cognitoUser.globalSignOut({
279294
onSuccess: () => {
280295
this.onUnauthenticated();
@@ -401,7 +416,7 @@ export default class CognitoService extends Service {
401416
!!this.cognitoData
402417
);
403418

404-
let { cognitoUser } = this.cognitoData;
419+
let { cognitoUser } = this.cognitoData!;
405420

406421
let promise = new RSVPPromise((resolve, reject) => {
407422
cognitoUser.changePassword(oldPassword, newPassword, function (error) {
@@ -423,7 +438,7 @@ export default class CognitoService extends Service {
423438
!!this.cognitoData
424439
);
425440

426-
let { cognitoUser } = this.cognitoData;
441+
let { cognitoUser } = this.cognitoData!;
427442

428443
let attributeList: ICognitoUserAttributeData[] = Object.keys(
429444
attributes
@@ -444,7 +459,7 @@ export default class CognitoService extends Service {
444459

445460
this._getUserAttributes(cognitoUser)
446461
.then((userAttributes) => {
447-
this.cognitoData.userAttributes = userAttributes;
462+
this.cognitoData!.userAttributes = userAttributes;
448463
resolve(userAttributes);
449464
})
450465
.catch(reject);
@@ -482,6 +497,10 @@ export default class CognitoService extends Service {
482497
return reject(dispatchError(error));
483498
}
484499

500+
if (!cognitoUserAttributes) {
501+
return resolve({});
502+
}
503+
485504
let userAttributes: { [index: string]: string } = {};
486505
cognitoUserAttributes.forEach((cognitoUserAttribute) => {
487506
let name = cognitoUserAttribute.getName();

‎tests/dummy/app/components/cognito-login-form.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
} from 'ember-cognito-identity/errors/cognito';
88
import { dropTask } from 'ember-concurrency-decorators';
99
import { tracked } from '@glimmer/tracking';
10-
import CognitoService from 'ember-cognito-identity/services/cognito';
10+
import CognitoService, {
11+
UserAttributes,
12+
} from 'ember-cognito-identity/services/cognito';
1113
import RouterService from '@ember/routing/router-service';
1214

1315
export default class CognitoLoginForm extends Component {
@@ -66,7 +68,9 @@ export default class CognitoLoginForm extends Component {
6668

6769
// This can be overwritten
6870
// eslint-disable-next-line no-unused-vars
69-
_getNewPasswordAttributes(_options: Object): undefined | Object {
71+
_getNewPasswordAttributes(
72+
_options: UserAttributes
73+
): undefined | UserAttributes {
7074
return undefined;
7175
}
7276

‎tests/dummy/app/components/cognito-reset-password-form.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export default class CognitoResetPasswordForm extends Component<Args> {
1818
@service router: RouterService;
1919

2020
// Properties
21-
@tracked password: string;
22-
@tracked selectedUsername: string;
23-
@tracked selectedVerificationCode: string;
21+
@tracked password?: string;
22+
@tracked selectedUsername?: string;
23+
@tracked selectedVerificationCode?: string;
2424
@tracked error: CognitoError | null;
2525
@tracked showPasswordForm: boolean = false;
2626

‎tests/dummy/app/components/cognito-reset-password-form/select-username.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class CognitoResetPasswordFormSelectUsername extends Component<
1111
Args
1212
> {
1313
// Properties
14-
@tracked currentUsername: string;
14+
@tracked currentUsername?: string;
1515

1616
constructor(owner: any, args: Args) {
1717
super(owner, args);

‎tests/dummy/app/components/cognito-reset-password-form/update-password.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export default class CognitoResetPasswordFormUpdatePassword extends Component<
1212
Args
1313
> {
1414
// Properties
15-
@tracked currentVerificationCode: string;
16-
@tracked password: string;
15+
@tracked currentVerificationCode?: string;
16+
@tracked password?: string;
1717

1818
constructor(owner: any, args: Args) {
1919
super(owner, args);

‎tsconfig.json‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"noImplicitAny": true,
88
"noImplicitThis": true,
99
"alwaysStrict": true,
10-
"strictNullChecks": false,
10+
"strictNullChecks": true,
1111
"strictPropertyInitialization": false,
1212
"noFallthroughCasesInSwitch": true,
1313
"noUnusedLocals": true,
@@ -19,9 +19,7 @@
1919
"inlineSources": true,
2020
"baseUrl": ".",
2121
"module": "es6",
22-
"esModuleInterop": true,
2322
"experimentalDecorators": true,
24-
"skipLibCheck": true,
2523
"paths": {
2624
"dummy/tests/*": ["tests/*"],
2725
"dummy/*": ["tests/dummy/app/*", "app/*"],

0 commit comments

Comments
 (0)