@juniyadi/cognito 0.0.1 → 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.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _aws_sdk_client_cognito_identity_provider from '@aws-sdk/client-cognito-identity-provider';
2
- import { CognitoIdentityProviderClient, AdminSetUserPasswordCommandOutput } from '@aws-sdk/client-cognito-identity-provider';
2
+ import { CognitoIdentityProviderClient, AdminSetUserPasswordCommandOutput, AdminDisableUserCommandOutput, AdminDeleteUserCommandOutput } from '@aws-sdk/client-cognito-identity-provider';
3
3
 
4
4
  interface ICognito {
5
5
  region?: string;
@@ -41,8 +41,21 @@ declare class Cognito {
41
41
  * Change user password in Cognito User Pool
42
42
  * @param username - Username of the user
43
43
  * @param password - New password for the user
44
+ * @returns Promise<AdminSetUserPasswordCommandOutput>
44
45
  */
45
46
  changePassword: (username: string, password: string) => Promise<AdminSetUserPasswordCommandOutput>;
47
+ /**
48
+ * Disable a user in Cognito User Pool
49
+ * @param username - Username of the user
50
+ * @returns Promise<AdminDisableUserCommandOutput>
51
+ */
52
+ disableUser: (username: string) => Promise<AdminDisableUserCommandOutput>;
53
+ /**
54
+ * Delete a user in Cognito User Pool
55
+ * @param username - Username of the user
56
+ * @returns Promise<AdminDeleteUserCommandOutput>
57
+ */
58
+ deleteUser: (username: string) => Promise<AdminDeleteUserCommandOutput>;
46
59
  }
47
60
 
48
61
  export { Cognito, ICognito, ICognitoAttributes, ICognitoInviteUser };
package/dist/Cognito.js CHANGED
@@ -22,16 +22,11 @@ __export(Cognito_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(Cognito_exports);
24
24
  var import_client_cognito_identity_provider = require("@aws-sdk/client-cognito-identity-provider");
25
+ var import_crypto = require("crypto");
25
26
  class Cognito {
26
27
  constructor(opts) {
27
28
  this.randomPassword = (length = 12) => {
28
- const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
29
- let pass = "";
30
- for (let x = 0; x < length; x++) {
31
- const i = Math.floor(Math.random() * chars.length);
32
- pass += chars.charAt(i);
33
- }
34
- return pass;
29
+ return (0, import_crypto.randomBytes)(256).toString("hex").slice(0, length);
35
30
  };
36
31
  this.inviteUser = async (opts) => {
37
32
  var _a, _b;
@@ -91,16 +86,32 @@ class Cognito {
91
86
  Password: password,
92
87
  Permanent: true
93
88
  });
94
- try {
95
- const response = await this.cognitoIdentityProvider.send(command);
96
- return response;
97
- } catch (error) {
98
- throw error;
99
- }
89
+ return this.cognitoIdentityProvider.send(command);
90
+ };
91
+ this.disableUser = async (username) => {
92
+ const command = new import_client_cognito_identity_provider.AdminDisableUserCommand({
93
+ UserPoolId: this.userPoolId,
94
+ Username: username
95
+ });
96
+ return this.cognitoIdentityProvider.send(command);
97
+ };
98
+ this.deleteUser = async (username) => {
99
+ await this.disableUser(username);
100
+ const command = new import_client_cognito_identity_provider.AdminDeleteUserCommand({
101
+ UserPoolId: this.userPoolId,
102
+ Username: username
103
+ });
104
+ return this.cognitoIdentityProvider.send(command);
100
105
  };
101
106
  this.region = (opts == null ? void 0 : opts.region) || process.env.AWS_REGION || "";
102
107
  this.userPoolId = (opts == null ? void 0 : opts.userPoolId) || process.env.COGNITO_USER_POOL_ID || "";
103
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");
104
115
  this.cognitoIdentityProvider = new import_client_cognito_identity_provider.CognitoIdentityProviderClient({
105
116
  region: this.region
106
117
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/Cognito.ts"],"sourcesContent":["import {\n AdminCreateUserCommand,\n AdminCreateUserCommandInput,\n CognitoIdentityProviderClient,\n AdminSetUserPasswordCommand,\n AdminSetUserPasswordCommandOutput,\n} from \"@aws-sdk/client-cognito-identity-provider\";\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 const chars =\n \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\";\n let pass = \"\";\n for (let x = 0; x < length; x++) {\n const i = Math.floor(Math.random() * chars.length);\n pass += chars.charAt(i);\n }\n return pass;\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 */\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 try {\n const response = await this.cognitoIdentityProvider.send(command);\n return response;\n } catch (error) {\n throw error;\n }\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,8CAMO;AAwBA,MAAM,QAAQ;AAAA,EAMnB,YAAY,MAAiB;AAgB7B,SAAO,iBAAiB,CAAC,SAAS,OAAe;AAC/C,YAAM,QACJ;AACF,UAAI,OAAO;AACX,eAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,cAAM,IAAI,KAAK,MAAM,KAAK,OAAO,IAAI,MAAM,MAAM;AACjD,gBAAQ,MAAM,OAAO,CAAC;AAAA,MACxB;AACA,aAAO;AAAA,IACT;AAMA,SAAO,aAAa,OAAO,SAA6B;AAnE1D;AAoEI,YAAM,oBAAoB,KAAK,qBAAqB,KAAK,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ACF,cAAM,WAAW,MAAM,KAAK,wBAAwB,KAAK,OAAO;AAChE,eAAO;AAAA,MACT,SAAS,OAAP;AACA,cAAM;AAAA,MACR;AAAA,IACF;AAvHE,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;AAgHF;","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,MAAM;AAAA,YACN,OAAO;AAAA,UACT;AAAA,QACF;AAAA,QACA,wBAAwB,CAAC,OA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;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
@@ -1,18 +1,15 @@
1
1
  import {
2
2
  AdminCreateUserCommand,
3
3
  CognitoIdentityProviderClient,
4
- AdminSetUserPasswordCommand
4
+ AdminSetUserPasswordCommand,
5
+ AdminDisableUserCommand,
6
+ AdminDeleteUserCommand
5
7
  } from "@aws-sdk/client-cognito-identity-provider";
8
+ import { randomBytes } from "crypto";
6
9
  class Cognito {
7
10
  constructor(opts) {
8
11
  this.randomPassword = (length = 12) => {
9
- const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
10
- let pass = "";
11
- for (let x = 0; x < length; x++) {
12
- const i = Math.floor(Math.random() * chars.length);
13
- pass += chars.charAt(i);
14
- }
15
- return pass;
12
+ return randomBytes(256).toString("hex").slice(0, length);
16
13
  };
17
14
  this.inviteUser = async (opts) => {
18
15
  var _a, _b;
@@ -72,16 +69,32 @@ class Cognito {
72
69
  Password: password,
73
70
  Permanent: true
74
71
  });
75
- try {
76
- const response = await this.cognitoIdentityProvider.send(command);
77
- return response;
78
- } catch (error) {
79
- throw error;
80
- }
72
+ return this.cognitoIdentityProvider.send(command);
73
+ };
74
+ this.disableUser = async (username) => {
75
+ const command = new AdminDisableUserCommand({
76
+ UserPoolId: this.userPoolId,
77
+ Username: username
78
+ });
79
+ return this.cognitoIdentityProvider.send(command);
80
+ };
81
+ this.deleteUser = async (username) => {
82
+ await this.disableUser(username);
83
+ const command = new AdminDeleteUserCommand({
84
+ UserPoolId: this.userPoolId,
85
+ Username: username
86
+ });
87
+ return this.cognitoIdentityProvider.send(command);
81
88
  };
82
89
  this.region = (opts == null ? void 0 : opts.region) || process.env.AWS_REGION || "";
83
90
  this.userPoolId = (opts == null ? void 0 : opts.userPoolId) || process.env.COGNITO_USER_POOL_ID || "";
84
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");
85
98
  this.cognitoIdentityProvider = new CognitoIdentityProviderClient({
86
99
  region: this.region
87
100
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/Cognito.ts"],"sourcesContent":["import {\n AdminCreateUserCommand,\n AdminCreateUserCommandInput,\n CognitoIdentityProviderClient,\n AdminSetUserPasswordCommand,\n AdminSetUserPasswordCommandOutput,\n} from \"@aws-sdk/client-cognito-identity-provider\";\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 const chars =\n \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\";\n let pass = \"\";\n for (let x = 0; x < length; x++) {\n const i = Math.floor(Math.random() * chars.length);\n pass += chars.charAt(i);\n }\n return pass;\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 */\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 try {\n const response = await this.cognitoIdentityProvider.send(command);\n return response;\n } catch (error) {\n throw error;\n }\n };\n}\n"],"mappings":"AAAA;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,OAEK;AAwBA,MAAM,QAAQ;AAAA,EAMnB,YAAY,MAAiB;AAgB7B,SAAO,iBAAiB,CAAC,SAAS,OAAe;AAC/C,YAAM,QACJ;AACF,UAAI,OAAO;AACX,eAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,cAAM,IAAI,KAAK,MAAM,KAAK,OAAO,IAAI,MAAM,MAAM;AACjD,gBAAQ,MAAM,OAAO,CAAC;AAAA,MACxB;AACA,aAAO;AAAA,IACT;AAMA,SAAO,aAAa,OAAO,SAA6B;AAnE1D;AAoEI,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,MAAM;AAAA,YACN,OAAO;AAAA,UACT;AAAA,QACF;AAAA,QACA,wBAAwB,CAAC,OAAO;AAAA,QAChC,oBAAoB;AAAA,QACpB,eAAe,KAAK,YAAY,WAAW;AAAA,QAC3C,mBAAmB;AAAA,MACrB;AAGA,UAAI,KA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Content":["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,EAEA;AAAA,OAEK;AACP,SAAS,mBAAmB;AAwBrB,MAAM,QAAQ;AAAA,EAMnB,YAAY,MAAiB;AAoB7B,SAAO,iBAAiB,CAAC,SAAS,OAAe;AAC/C,aAAO,YAAY,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,MAAM;AAAA,YACN,OAAO;AAAA,UACT;AAAA,QACF;AAAA,QACA,wBAAwB,CAAC,OAAO;AAAA,QAChC,oBAAoB;AAAA,QACpB,eAAe,KAAK,YAAY,WAAW;AAAA,QAC3C,mBAAmB;AAAA,MACrB;AAGA,UAAI,KAAK,eAAe,MAAM,gBAAgB;AAC5C,cAAM,eAAe,KAAK;AAAA,UACxB,MAAM;AAAA,UACN,OAAO,KAAK;AAAA,QACd,CAAC;AAAA,MACH;AAGA,UAAI,KAAK,cAAc,MAAM,gBAAgB;AAC3C,cAAM,iBAAiB,MAAM,eAAe,OAAO,KAAK,UAAU;AAAA,MACpE;AAEA,YAAM,UAAU,IAAI,uBAAuB,KAAK;AAChD,UAAI;AACF,cAAM,WAAW,MAAM,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,KAAK;AAAU,YAAM,IAAI,MAAM,+BAA+B;AAEnE,SAAK,0BAA0B,IAAI,8BAA8B;AAAA,MAC/D,QAAQ,KAAK;AAAA,IACf,CAAC;AAAA,EACH;AAwIF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juniyadi/cognito",
3
- "version": "0.0.1",
3
+ "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"