1
+ import { CognitoUserPoolTriggerEvent } from 'aws-lambda' ;
1
2
import { AWSError , CognitoIdentityServiceProvider } from 'aws-sdk' ;
2
3
import { AdminInitiateAuthRequest } from 'aws-sdk/clients/cognitoidentityserviceprovider' ;
3
4
4
- import { CognitoEvent , UserMigrationAuthenticationEvent , UserMigrationForgotPasswordEvent } from './types/event' ;
5
-
6
5
/**
7
6
* AWS region in which your User Pools are deployed
8
7
*/
@@ -76,19 +75,19 @@ async function lookupUser(username: string): Promise<User | undefined> {
76
75
return user ;
77
76
}
78
77
79
- async function onUserMigrationAuthentication ( event : UserMigrationAuthenticationEvent ) {
78
+ async function onUserMigrationAuthentication ( event : CognitoUserPoolTriggerEvent ) {
80
79
// authenticate the user with your existing user directory service
81
- const user = await authenticateUser ( event . userName , event . request . password ) ;
80
+ const user = await authenticateUser ( event . userName ! , event . request . password ! ) ;
82
81
if ( ! user ) {
83
82
throw new Error ( 'Bad credentials' ) ;
84
83
}
85
84
86
85
event . response . userAttributes = {
87
86
// old_username: user.userName,
88
87
// 'custom:tenant': user.userAttributes['custom:tenant'],
89
- email : user . userAttributes . email ,
88
+ email : user . userAttributes . email ! ,
90
89
email_verified : 'true' ,
91
- preferred_username : user . userAttributes . preferred_username ,
90
+ preferred_username : user . userAttributes . preferred_username ! ,
92
91
} ;
93
92
event . response . finalUserStatus = 'CONFIRMED' ;
94
93
event . response . messageAction = 'SUPPRESS' ;
@@ -97,19 +96,19 @@ async function onUserMigrationAuthentication(event: UserMigrationAuthenticationE
97
96
return event ;
98
97
}
99
98
100
- async function onUserMigrationForgotPassword ( event : UserMigrationForgotPasswordEvent ) {
99
+ async function onUserMigrationForgotPassword ( event : CognitoUserPoolTriggerEvent ) {
101
100
// Lookup the user in your existing user directory service
102
- const user = await lookupUser ( event . userName ) ;
101
+ const user = await lookupUser ( event . userName ! ) ;
103
102
if ( ! user ) {
104
103
throw new Error ( 'Bad credentials' ) ;
105
104
}
106
105
107
106
event . response . userAttributes = {
108
107
// old_username: user.userName,
109
108
// 'custom:tenant': user.userAttributes['custom:tenant'],
110
- email : user . userAttributes . email ,
109
+ email : user . userAttributes . email ! ,
111
110
email_verified : 'true' ,
112
- preferred_username : user . userAttributes . preferred_username ,
111
+ preferred_username : user . userAttributes . preferred_username ! ,
113
112
} ;
114
113
event . response . messageAction = 'SUPPRESS' ;
115
114
@@ -118,7 +117,7 @@ async function onUserMigrationForgotPassword(event: UserMigrationForgotPasswordE
118
117
return event ;
119
118
}
120
119
121
- export const handler = async ( event : CognitoEvent ) : Promise < CognitoEvent > => {
120
+ export const handler = async ( event : CognitoUserPoolTriggerEvent ) : Promise < CognitoUserPoolTriggerEvent > => {
122
121
switch ( event . triggerSource ) {
123
122
case 'UserMigration_Authentication' :
124
123
return onUserMigrationAuthentication ( event ) ;
0 commit comments