@juniyadi/cognito 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Cognito.js CHANGED
@@ -106,6 +106,12 @@ class Cognito {
106
106
  this.region = (opts == null ? void 0 : opts.region) || process.env.AWS_REGION || "";
107
107
  this.userPoolId = (opts == null ? void 0 : opts.userPoolId) || process.env.COGNITO_USER_POOL_ID || "";
108
108
  this.clientId = (opts == null ? void 0 : opts.clientId) || process.env.COGNITO_CLIENT_ID || "";
109
+ if (!this.region)
110
+ throw new Error("AWS Region is required");
111
+ if (!this.userPoolId)
112
+ throw new Error("Cognito User Pool ID is required");
113
+ if (!this.clientId)
114
+ throw new Error("Cognito Client ID is required");
109
115
  this.cognitoIdentityProvider = new import_client_cognito_identity_provider.CognitoIdentityProviderClient({
110
116
  region: this.region
111
117
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/Cognito.ts"],"sourcesContent":["import {\n AdminCreateUserCommand,\n AdminCreateUserCommandInput,\n CognitoIdentityProviderClient,\n AdminSetUserPasswordCommand,\n AdminSetUserPasswordCommandOutput,\n AdminDisableUserCommand,\n AdminDisableUserCommandOutput,\n AdminDeleteUserCommand,\n AdminDeleteUserCommandOutput,\n} from \"@aws-sdk/client-cognito-identity-provider\";\nimport { randomBytes } from \"crypto\";\n\nexport interface ICognito {\n region?: string;\n userPoolId?: string;\n clientId?: string;\n}\n\nexport interface ICognitoAttributes {\n Name: string;\n Value: string;\n}\n\nexport interface ICognitoInviteUser {\n name: string;\n email: string;\n password: string;\n passwordTemporary?: string;\n autoConfirm?: boolean;\n phoneNumber?: string;\n sendEmail?: boolean;\n attributes?: ICognitoAttributes[];\n}\n\nexport class Cognito {\n public region: string;\n public userPoolId: string;\n public clientId: string;\n public cognitoIdentityProvider: CognitoIdentityProviderClient;\n\n constructor(opts?: ICognito) {\n this.region = opts?.region || process.env.AWS_REGION || \"\";\n this.userPoolId =\n opts?.userPoolId || process.env.COGNITO_USER_POOL_ID || \"\";\n this.clientId = opts?.clientId || process.env.COGNITO_CLIENT_ID || \"\";\n\n this.cognitoIdentityProvider = new CognitoIdentityProviderClient({\n region: this.region,\n });\n }\n\n /**\n * Generate a random password of a given length\n * @param length\n * @returns string\n */\n public randomPassword = (length = 12): string => {\n return randomBytes(256).toString(\"hex\").slice(0, length);\n };\n\n /**\n * Invite a user to the Cognito User Pool\n * @param opts\n */\n public inviteUser = async (opts: ICognitoInviteUser) => {\n const temporaryPassword = opts.passwordTemporary || this.randomPassword();\n\n const items: AdminCreateUserCommandInput = {\n UserPoolId: this.userPoolId,\n Username: opts.email,\n UserAttributes: [\n {\n Name: \"email\",\n Value: opts.email,\n },\n {\n Name: \"name\",\n Value: opts.name,\n },\n {\n Name: \"email_verified\",\n Value: \"true\",\n },\n ],\n DesiredDeliveryMediums: [\"EMAIL\"],\n ForceAliasCreation: false,\n MessageAction: opts.sendEmail ? \"RESEND\" : \"SUPPRESS\",\n TemporaryPassword: temporaryPassword,\n };\n\n // Add Phone Number to User Attributes if provided\n if (opts.phoneNumber && items.UserAttributes) {\n items.UserAttributes.push({\n Name: \"phone_number\",\n Value: opts.phoneNumber,\n });\n }\n\n // Add Other Attributes to User Attributes if provided\n if (opts.attributes && items.UserAttributes) {\n items.UserAttributes = items.UserAttributes.concat(opts.attributes);\n }\n\n const command = new AdminCreateUserCommand(items);\n try {\n const response = await this.cognitoIdentityProvider.send(command);\n\n /**\n * If the user is FORCE_CHANGE_PASSWORD, then change the password to the one provided\n * So that the user can login with the password provided\n */\n if (\n opts.autoConfirm &&\n response?.User?.UserStatus === \"FORCE_CHANGE_PASSWORD\"\n ) {\n const changePassword = await this.changePassword(\n opts.email,\n opts.password\n );\n\n if (changePassword?.$metadata?.httpStatusCode !== 200) {\n throw new Error(\"Error changing password\");\n }\n }\n\n return response;\n } catch (error) {\n throw error;\n }\n };\n\n /**\n * Change user password in Cognito User Pool\n * @param username - Username of the user\n * @param password - New password for the user\n * @returns Promise<AdminSetUserPasswordCommandOutput>\n */\n public changePassword = async (\n username: string,\n password: string\n ): Promise<AdminSetUserPasswordCommandOutput> => {\n const command = new AdminSetUserPasswordCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n Password: password,\n Permanent: true,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n\n /**\n * Disable a user in Cognito User Pool\n * @param username - Username of the user\n * @returns Promise<AdminDisableUserCommandOutput>\n */\n public disableUser = async (\n username: string\n ): Promise<AdminDisableUserCommandOutput> => {\n const command = new AdminDisableUserCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n\n /**\n * Delete a user in Cognito User Pool\n * @param username - Username of the user\n * @returns Promise<AdminDeleteUserCommandOutput>\n */\n public deleteUser = async (\n username: string\n ): Promise<AdminDeleteUserCommandOutput> => {\n // Disable the user first\n await this.disableUser(username);\n\n const command = new AdminDeleteUserCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,8CAUO;AACP,oBAA4B;AAwBrB,MAAM,QAAQ;AAAA,EAMnB,YAAY,MAAiB;AAgB7B,SAAO,iBAAiB,CAAC,SAAS,OAAe;AAC/C,iBAAO,2BAAY,GAAG,EAAE,SAAS,KAAK,EAAE,MAAM,GAAG,MAAM;AAAA,IACzD;AAMA,SAAO,aAAa,OAAO,SAA6B;AAjE1D;AAkEI,YAAM,oBAAoB,KAAK,qBAAqB,KAAK,eAAe;AAExE,YAAM,QAAqC;AAAA,QACzC,YAAY,KAAK;AAAA,QACjB,UAAU,KAAK;AAAA,QACf,gBAAgB;AAAA,UACd;AAAA,YACE,MAAM;AAAA,YACN,OAAO,KAAK;AAAA,UACd;AAAA,UACA;AAAA,YACE,MAAM;AAAA,YACN,OAAO,KAAK;AAAA,UACd;AAAA,UACA;AAAA,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AClB,aAC0C;AAE1C,YAAM,KAAK,YAAY,QAAQ;AAE/B,YAAM,UAAU,IAAI,+DAAuB;AAAA,QACzC,YAAY,KAAK;AAAA,QACjB,UAAU;AAAA,MACZ,CAAC;AAED,aAAO,KAAK,wBAAwB,KAAK,OAAO;AAAA,IAClD;AA/IE,SAAK,UAAS,6BAAM,WAAU,QAAQ,IAAI,cAAc;AACxD,SAAK,cACH,6BAAM,eAAc,QAAQ,IAAI,wBAAwB;AAC1D,SAAK,YAAW,6BAAM,aAAY,QAAQ,IAAI,qBAAqB;AAEnE,SAAK,0BAA0B,IAAI,sEAA8B;AAAA,MAC/D,QAAQ,KAAK;AAAA,IACf,CAAC;AAAA,EACH;AAwIF;","names":[]}
1
+ {"version":3,"sources":["../src/Cognito.ts"],"sourcesContent":["import {\n AdminCreateUserCommand,\n AdminCreateUserCommandInput,\n CognitoIdentityProviderClient,\n AdminSetUserPasswordCommand,\n AdminSetUserPasswordCommandOutput,\n AdminDisableUserCommand,\n AdminDisableUserCommandOutput,\n AdminDeleteUserCommand,\n AdminDeleteUserCommandOutput,\n} from \"@aws-sdk/client-cognito-identity-provider\";\nimport { randomBytes } from \"crypto\";\n\nexport interface ICognito {\n region?: string;\n userPoolId?: string;\n clientId?: string;\n}\n\nexport interface ICognitoAttributes {\n Name: string;\n Value: string;\n}\n\nexport interface ICognitoInviteUser {\n name: string;\n email: string;\n password: string;\n passwordTemporary?: string;\n autoConfirm?: boolean;\n phoneNumber?: string;\n sendEmail?: boolean;\n attributes?: ICognitoAttributes[];\n}\n\nexport class Cognito {\n public region: string;\n public userPoolId: string;\n public clientId: string;\n public cognitoIdentityProvider: CognitoIdentityProviderClient;\n\n constructor(opts?: ICognito) {\n this.region = opts?.region || process.env.AWS_REGION || \"\";\n this.userPoolId =\n opts?.userPoolId || process.env.COGNITO_USER_POOL_ID || \"\";\n this.clientId = opts?.clientId || process.env.COGNITO_CLIENT_ID || \"\";\n\n if (!this.region) throw new Error(\"AWS Region is required\");\n if (!this.userPoolId) throw new Error(\"Cognito User Pool ID is required\");\n if (!this.clientId) throw new Error(\"Cognito Client ID is required\");\n\n this.cognitoIdentityProvider = new CognitoIdentityProviderClient({\n region: this.region,\n });\n }\n\n /**\n * Generate a random password of a given length\n * @param length\n * @returns string\n */\n public randomPassword = (length = 12): string => {\n return randomBytes(256).toString(\"hex\").slice(0, length);\n };\n\n /**\n * Invite a user to the Cognito User Pool\n * @param opts\n */\n public inviteUser = async (opts: ICognitoInviteUser) => {\n const temporaryPassword = opts.passwordTemporary || this.randomPassword();\n\n const items: AdminCreateUserCommandInput = {\n UserPoolId: this.userPoolId,\n Username: opts.email,\n UserAttributes: [\n {\n Name: \"email\",\n Value: opts.email,\n },\n {\n Name: \"name\",\n Value: opts.name,\n },\n {\n Name: \"email_verified\",\n Value: \"true\",\n },\n ],\n DesiredDeliveryMediums: [\"EMAIL\"],\n ForceAliasCreation: false,\n MessageAction: opts.sendEmail ? \"RESEND\" : \"SUPPRESS\",\n TemporaryPassword: temporaryPassword,\n };\n\n // Add Phone Number to User Attributes if provided\n if (opts.phoneNumber && items.UserAttributes) {\n items.UserAttributes.push({\n Name: \"phone_number\",\n Value: opts.phoneNumber,\n });\n }\n\n // Add Other Attributes to User Attributes if provided\n if (opts.attributes && items.UserAttributes) {\n items.UserAttributes = items.UserAttributes.concat(opts.attributes);\n }\n\n const command = new AdminCreateUserCommand(items);\n try {\n const response = await this.cognitoIdentityProvider.send(command);\n\n /**\n * If the user is FORCE_CHANGE_PASSWORD, then change the password to the one provided\n * So that the user can login with the password provided\n */\n if (\n opts.autoConfirm &&\n response?.User?.UserStatus === \"FORCE_CHANGE_PASSWORD\"\n ) {\n const changePassword = await this.changePassword(\n opts.email,\n opts.password\n );\n\n if (changePassword?.$metadata?.httpStatusCode !== 200) {\n throw new Error(\"Error changing password\");\n }\n }\n\n return response;\n } catch (error) {\n throw error;\n }\n };\n\n /**\n * Change user password in Cognito User Pool\n * @param username - Username of the user\n * @param password - New password for the user\n * @returns Promise<AdminSetUserPasswordCommandOutput>\n */\n public changePassword = async (\n username: string,\n password: string\n ): Promise<AdminSetUserPasswordCommandOutput> => {\n const command = new AdminSetUserPasswordCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n Password: password,\n Permanent: true,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n\n /**\n * Disable a user in Cognito User Pool\n * @param username - Username of the user\n * @returns Promise<AdminDisableUserCommandOutput>\n */\n public disableUser = async (\n username: string\n ): Promise<AdminDisableUserCommandOutput> => {\n const command = new AdminDisableUserCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n\n /**\n * Delete a user in Cognito User Pool\n * @param username - Username of the user\n * @returns Promise<AdminDeleteUserCommandOutput>\n */\n public deleteUser = async (\n username: string\n ): Promise<AdminDeleteUserCommandOutput> => {\n // Disable the user first\n await this.disableUser(username);\n\n const command = new AdminDeleteUserCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,8CAUO;AACP,oBAA4B;AAwBrB,MAAM,QAAQ;AAAA,EAMnB,YAAY,MAAiB;AAoB7B,SAAO,iBAAiB,CAAC,SAAS,OAAe;AAC/C,iBAAO,2BAAY,GAAG,EAAE,SAAS,KAAK,EAAE,MAAM,GAAG,MAAM;AAAA,IACzD;AAMA,SAAO,aAAa,OAAO,SAA6B;AArE1D;AAsEI,YAAM,oBAAoB,KAAK,qBAAqB,KAAK,eAAe;AAExE,YAAM,QAAqC;AAAA,QACzC,YAAY,KAAK;AAAA,QACjB,UAAU,KAAK;AAAA,QACf,gBAAgB;AAAA,UACd;AAAA,YACE,MAAM;AAAA,YACN,OAAO,KAAK;AAAA,UACd;AAAA,UACA;AAAA,YACE,MAAM;AAAA,YACN,OAAO,KAAK;AAAA,UACd;AAAA,UACA;AAAA,YACE,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aAC0C;AAE1C,YAAM,KAAK,YAAY,QAAQ;AAE/B,YAAM,UAAU,IAAI,+DAAuB;AAAA,QACzC,YAAY,KAAK;AAAA,QACjB,UAAU;AAAA,MACZ,CAAC;AAED,aAAO,KAAK,wBAAwB,KAAK,OAAO;AAAA,IAClD;AAnJE,SAAK,UAAS,6BAAM,WAAU,QAAQ,IAAI,cAAc;AACxD,SAAK,cACH,6BAAM,eAAc,QAAQ,IAAI,wBAAwB;AAC1D,SAAK,YAAW,6BAAM,aAAY,QAAQ,IAAI,qBAAqB;AAEnE,QAAI,CAAC,KAAK;AAAQ,YAAM,IAAI,MAAM,wBAAwB;AAC1D,QAAI,CAAC,KAAK;AAAY,YAAM,IAAI,MAAM,kCAAkC;AACxE,QAAI,CAAC,KAAK;AAAU,YAAM,IAAI,MAAM,+BAA+B;AAEnE,SAAK,0BAA0B,IAAI,sEAA8B;AAAA,MAC/D,QAAQ,KAAK;AAAA,IACf,CAAC;AAAA,EACH;AAwIF;","names":[]}
package/dist/Cognito.mjs CHANGED
@@ -89,6 +89,12 @@ class Cognito {
89
89
  this.region = (opts == null ? void 0 : opts.region) || process.env.AWS_REGION || "";
90
90
  this.userPoolId = (opts == null ? void 0 : opts.userPoolId) || process.env.COGNITO_USER_POOL_ID || "";
91
91
  this.clientId = (opts == null ? void 0 : opts.clientId) || process.env.COGNITO_CLIENT_ID || "";
92
+ if (!this.region)
93
+ throw new Error("AWS Region is required");
94
+ if (!this.userPoolId)
95
+ throw new Error("Cognito User Pool ID is required");
96
+ if (!this.clientId)
97
+ throw new Error("Cognito Client ID is required");
92
98
  this.cognitoIdentityProvider = new CognitoIdentityProviderClient({
93
99
  region: this.region
94
100
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/Cognito.ts"],"sourcesContent":["import {\n AdminCreateUserCommand,\n AdminCreateUserCommandInput,\n CognitoIdentityProviderClient,\n AdminSetUserPasswordCommand,\n AdminSetUserPasswordCommandOutput,\n AdminDisableUserCommand,\n AdminDisableUserCommandOutput,\n AdminDeleteUserCommand,\n AdminDeleteUserCommandOutput,\n} from \"@aws-sdk/client-cognito-identity-provider\";\nimport { randomBytes } from \"crypto\";\n\nexport interface ICognito {\n region?: string;\n userPoolId?: string;\n clientId?: string;\n}\n\nexport interface ICognitoAttributes {\n Name: string;\n Value: string;\n}\n\nexport interface ICognitoInviteUser {\n name: string;\n email: string;\n password: string;\n passwordTemporary?: string;\n autoConfirm?: boolean;\n phoneNumber?: string;\n sendEmail?: boolean;\n attributes?: ICognitoAttributes[];\n}\n\nexport class Cognito {\n public region: string;\n public userPoolId: string;\n public clientId: string;\n public cognitoIdentityProvider: CognitoIdentityProviderClient;\n\n constructor(opts?: ICognito) {\n this.region = opts?.region || process.env.AWS_REGION || \"\";\n this.userPoolId =\n opts?.userPoolId || process.env.COGNITO_USER_POOL_ID || \"\";\n this.clientId = opts?.clientId || process.env.COGNITO_CLIENT_ID || \"\";\n\n this.cognitoIdentityProvider = new CognitoIdentityProviderClient({\n region: this.region,\n });\n }\n\n /**\n * Generate a random password of a given length\n * @param length\n * @returns string\n */\n public randomPassword = (length = 12): string => {\n return randomBytes(256).toString(\"hex\").slice(0, length);\n };\n\n /**\n * Invite a user to the Cognito User Pool\n * @param opts\n */\n public inviteUser = async (opts: ICognitoInviteUser) => {\n const temporaryPassword = opts.passwordTemporary || this.randomPassword();\n\n const items: AdminCreateUserCommandInput = {\n UserPoolId: this.userPoolId,\n Username: opts.email,\n UserAttributes: [\n {\n Name: \"email\",\n Value: opts.email,\n },\n {\n Name: \"name\",\n Value: opts.name,\n },\n {\n Name: \"email_verified\",\n Value: \"true\",\n },\n ],\n DesiredDeliveryMediums: [\"EMAIL\"],\n ForceAliasCreation: false,\n MessageAction: opts.sendEmail ? \"RESEND\" : \"SUPPRESS\",\n TemporaryPassword: temporaryPassword,\n };\n\n // Add Phone Number to User Attributes if provided\n if (opts.phoneNumber && items.UserAttributes) {\n items.UserAttributes.push({\n Name: \"phone_number\",\n Value: opts.phoneNumber,\n });\n }\n\n // Add Other Attributes to User Attributes if provided\n if (opts.attributes && items.UserAttributes) {\n items.UserAttributes = items.UserAttributes.concat(opts.attributes);\n }\n\n const command = new AdminCreateUserCommand(items);\n try {\n const response = await this.cognitoIdentityProvider.send(command);\n\n /**\n * If the user is FORCE_CHANGE_PASSWORD, then change the password to the one provided\n * So that the user can login with the password provided\n */\n if (\n opts.autoConfirm &&\n response?.User?.UserStatus === \"FORCE_CHANGE_PASSWORD\"\n ) {\n const changePassword = await this.changePassword(\n opts.email,\n opts.password\n );\n\n if (changePassword?.$metadata?.httpStatusCode !== 200) {\n throw new Error(\"Error changing password\");\n }\n }\n\n return response;\n } catch (error) {\n throw error;\n }\n };\n\n /**\n * Change user password in Cognito User Pool\n * @param username - Username of the user\n * @param password - New password for the user\n * @returns Promise<AdminSetUserPasswordCommandOutput>\n */\n public changePassword = async (\n username: string,\n password: string\n ): Promise<AdminSetUserPasswordCommandOutput> => {\n const command = new AdminSetUserPasswordCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n Password: password,\n Permanent: true,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n\n /**\n * Disable a user in Cognito User Pool\n * @param username - Username of the user\n * @returns Promise<AdminDisableUserCommandOutput>\n */\n public disableUser = async (\n username: string\n ): Promise<AdminDisableUserCommandOutput> => {\n const command = new AdminDisableUserCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n\n /**\n * Delete a user in Cognito User Pool\n * @param username - Username of the user\n * @returns Promise<AdminDeleteUserCommandOutput>\n */\n public deleteUser = async (\n username: string\n ): Promise<AdminDeleteUserCommandOutput> => {\n // Disable the user first\n await this.disableUser(username);\n\n const command = new AdminDeleteUserCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n}\n"],"mappings":"AAAA;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,EAEA;AAAA,EAEA;AAAA,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AAyB;AAAA,UAC3C;AAAA,QACF;AAEA,eAAO;AAAA,MACT,SAAS,OAAP;AACA,cAAM;AAAA,MACR;AAAA,IACF;AAQA,SAAO,iBAAiB,OACtB,UACA,aAC+C;AAC/C,YAAM,UAAU,IAAI,4BAA4B;AAAA,QAC9C,YAAY,KAAK;AAAA,QACjB,UAAU;AAAA,QACV,UAAU;AAAA,QACV,WAAW;AAAA,MACb,CAAC;AAED,aAAO,KAAK,wBAAwB,KAAK,OAAO;AAAA,IAClD;AAOA,SAAO,cAAc,OACnB,aAC2C;AAC3C,YAAM,UAAU,IAAI,wBAAwB;AAAA,QAC1C,YAAY,KAAK;AAAA,QACjB,UAAU;AAAA,MACZ,CAAC;AAED,aAAO,KAAK,wBAAwB,KAAK,OAAO;AAAA,IAClD;AAOA,SAAO,aAAa,OAClB,aAC0C;AAE1C,YAAM,KAAK,YAAY,QAAQ;AAE/B,YAAM,UAAU,IAAI,uBAAuB;AAAA,QACzC,YAAY,KAAK;AAAA,QACjB,UAAU;AAAA,MACZ,CAAC;AAED,aAAO,KAAK,wBAAwB,KAAK,OAAO;AAAA,IAClD;AA/IE,SAAK,UAAS,6BAAM,WAAU,QAAQ,IAAI,cAAc;AACxD,SAAK,cACH,6BAAM,eAAc,QAAQ,IAAI,wBAAwB;AAC1D,SAAK,YAAW,6BAAM,aAAY,QAAQ,IAAI,qBAAqB;AAEnE,SAAK,0BAA0B,IAAI,8BAA8B;AAAA,MAC/D,QAAQ,KAAK;AAAA,IACf,CAAC;AAAA,EACH;AAwIF;","names":[]}
1
+ {"version":3,"sources":["../src/Cognito.ts"],"sourcesContent":["import {\n AdminCreateUserCommand,\n AdminCreateUserCommandInput,\n CognitoIdentityProviderClient,\n AdminSetUserPasswordCommand,\n AdminSetUserPasswordCommandOutput,\n AdminDisableUserCommand,\n AdminDisableUserCommandOutput,\n AdminDeleteUserCommand,\n AdminDeleteUserCommandOutput,\n} from \"@aws-sdk/client-cognito-identity-provider\";\nimport { randomBytes } from \"crypto\";\n\nexport interface ICognito {\n region?: string;\n userPoolId?: string;\n clientId?: string;\n}\n\nexport interface ICognitoAttributes {\n Name: string;\n Value: string;\n}\n\nexport interface ICognitoInviteUser {\n name: string;\n email: string;\n password: string;\n passwordTemporary?: string;\n autoConfirm?: boolean;\n phoneNumber?: string;\n sendEmail?: boolean;\n attributes?: ICognitoAttributes[];\n}\n\nexport class Cognito {\n public region: string;\n public userPoolId: string;\n public clientId: string;\n public cognitoIdentityProvider: CognitoIdentityProviderClient;\n\n constructor(opts?: ICognito) {\n this.region = opts?.region || process.env.AWS_REGION || \"\";\n this.userPoolId =\n opts?.userPoolId || process.env.COGNITO_USER_POOL_ID || \"\";\n this.clientId = opts?.clientId || process.env.COGNITO_CLIENT_ID || \"\";\n\n if (!this.region) throw new Error(\"AWS Region is required\");\n if (!this.userPoolId) throw new Error(\"Cognito User Pool ID is required\");\n if (!this.clientId) throw new Error(\"Cognito Client ID is required\");\n\n this.cognitoIdentityProvider = new CognitoIdentityProviderClient({\n region: this.region,\n });\n }\n\n /**\n * Generate a random password of a given length\n * @param length\n * @returns string\n */\n public randomPassword = (length = 12): string => {\n return randomBytes(256).toString(\"hex\").slice(0, length);\n };\n\n /**\n * Invite a user to the Cognito User Pool\n * @param opts\n */\n public inviteUser = async (opts: ICognitoInviteUser) => {\n const temporaryPassword = opts.passwordTemporary || this.randomPassword();\n\n const items: AdminCreateUserCommandInput = {\n UserPoolId: this.userPoolId,\n Username: opts.email,\n UserAttributes: [\n {\n Name: \"email\",\n Value: opts.email,\n },\n {\n Name: \"name\",\n Value: opts.name,\n },\n {\n Name: \"email_verified\",\n Value: \"true\",\n },\n ],\n DesiredDeliveryMediums: [\"EMAIL\"],\n ForceAliasCreation: false,\n MessageAction: opts.sendEmail ? \"RESEND\" : \"SUPPRESS\",\n TemporaryPassword: temporaryPassword,\n };\n\n // Add Phone Number to User Attributes if provided\n if (opts.phoneNumber && items.UserAttributes) {\n items.UserAttributes.push({\n Name: \"phone_number\",\n Value: opts.phoneNumber,\n });\n }\n\n // Add Other Attributes to User Attributes if provided\n if (opts.attributes && items.UserAttributes) {\n items.UserAttributes = items.UserAttributes.concat(opts.attributes);\n }\n\n const command = new AdminCreateUserCommand(items);\n try {\n const response = await this.cognitoIdentityProvider.send(command);\n\n /**\n * If the user is FORCE_CHANGE_PASSWORD, then change the password to the one provided\n * So that the user can login with the password provided\n */\n if (\n opts.autoConfirm &&\n response?.User?.UserStatus === \"FORCE_CHANGE_PASSWORD\"\n ) {\n const changePassword = await this.changePassword(\n opts.email,\n opts.password\n );\n\n if (changePassword?.$metadata?.httpStatusCode !== 200) {\n throw new Error(\"Error changing password\");\n }\n }\n\n return response;\n } catch (error) {\n throw error;\n }\n };\n\n /**\n * Change user password in Cognito User Pool\n * @param username - Username of the user\n * @param password - New password for the user\n * @returns Promise<AdminSetUserPasswordCommandOutput>\n */\n public changePassword = async (\n username: string,\n password: string\n ): Promise<AdminSetUserPasswordCommandOutput> => {\n const command = new AdminSetUserPasswordCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n Password: password,\n Permanent: true,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n\n /**\n * Disable a user in Cognito User Pool\n * @param username - Username of the user\n * @returns Promise<AdminDisableUserCommandOutput>\n */\n public disableUser = async (\n username: string\n ): Promise<AdminDisableUserCommandOutput> => {\n const command = new AdminDisableUserCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n\n /**\n * Delete a user in Cognito User Pool\n * @param username - Username of the user\n * @returns Promise<AdminDeleteUserCommandOutput>\n */\n public deleteUser = async (\n username: string\n ): Promise<AdminDeleteUserCommandOutput> => {\n // Disable the user first\n await this.disableUser(username);\n\n const command = new AdminDeleteUserCommand({\n UserPoolId: this.userPoolId,\n Username: username,\n });\n\n return this.cognitoIdentityProvider.send(command);\n };\n}\n"],"mappings":"AAAA;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,EAEA;AAAA,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version": "0.0.3",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
@@ -20,8 +20,8 @@
20
20
  "ts-jest": "^29.1.1",
21
21
  "tsup": "^5.12.9",
22
22
  "typescript": "^4.9.5",
23
- "@juniyadi/tsconfig": "0.0.0",
24
- "eslint-config-juniyadi": "0.0.1"
23
+ "eslint-config-juniyadi": "0.0.1",
24
+ "@juniyadi/tsconfig": "0.0.0"
25
25
  },
26
26
  "publishConfig": {
27
27
  "access": "public"