@@ -24,11 +24,13 @@ import { timeout } from 'ember-concurrency';
2424
2525interface 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+
3234export 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 ( ) ;
0 commit comments