@juniyadi/cognito 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/Cognito.d.ts CHANGED
@@ -25,11 +25,24 @@ declare class Cognito {
25
25
  userPoolId: string;
26
26
  clientId: string;
27
27
  cognitoIdentityProvider: CognitoIdentityProviderClient;
28
+ /**
29
+ * Create a new Cognito instance
30
+ * @param opts
31
+ * @example
32
+ * const cognito = new Cognito({
33
+ * region: "us-east-1",
34
+ * userPoolId: "us-east-1_123456789",
35
+ * clientId: "12345678901234567890",
36
+ * });
37
+ */
28
38
  constructor(opts?: ICognito);
29
39
  /**
30
40
  * Generate a random password of a given length
31
41
  * @param length
32
42
  * @returns string
43
+ * @example
44
+ * const password = randomPassword(12);
45
+ * console.log(password);
33
46
  */
34
47
  randomPassword: (length?: number) => string;
35
48
  /**
@@ -42,20 +55,46 @@ declare class Cognito {
42
55
  * @param username - Username of the user
43
56
  * @param password - New password for the user
44
57
  * @returns Promise<AdminSetUserPasswordCommandOutput>
58
+ * @example
59
+ * const cognito = new Cognito();
60
+ * const users = await cognito.changePassword("johndoe@example.com", "newPassword");
61
+ * console.log(users.Users);
62
+ * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminSetUserPassword.html
45
63
  */
46
64
  changePassword: (username: string, password: string) => Promise<AdminSetUserPasswordCommandOutput>;
47
65
  /**
48
66
  * Disable a user in Cognito User Pool
49
67
  * @param username - Username of the user
50
68
  * @returns Promise<AdminDisableUserCommandOutput>
69
+ * @example
70
+ * const cognito = new Cognito();
71
+ * const users = await cognito.disableUser("johndoe@example.com");
72
+ * console.log(users.Users);
73
+ * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminDisableUser.html
51
74
  */
52
75
  disableUser: (username: string) => Promise<AdminDisableUserCommandOutput>;
53
76
  /**
54
77
  * Delete a user in Cognito User Pool
55
78
  * @param username - Username of the user
56
79
  * @returns Promise<AdminDeleteUserCommandOutput>
80
+ * @example
81
+ * const cognito = new Cognito();
82
+ * const users = await cognito.deleteUser("johndoe@example.com");
83
+ * console.log(users.Users);
84
+ * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminDeleteUser.html
57
85
  */
58
86
  deleteUser: (username: string) => Promise<AdminDeleteUserCommandOutput>;
87
+ /**
88
+ * List for users in Cognito User Pool
89
+ * @param opts
90
+ * @returns Promise<ListUsersCommandOutput>
91
+ * @example
92
+ * const cognito = new Cognito();
93
+ * const users = await cognito.listUsers({ email: "johndoe@example.com" });
94
+ * console.log(users.Users);
95
+ * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html
96
+ */
97
+ listUsers: (filter?: string) => Promise<_aws_sdk_client_cognito_identity_provider.ListUsersCommandOutput>;
59
98
  }
60
99
 
61
100
  export { Cognito, ICognito, ICognitoAttributes, ICognitoInviteUser };
package/dist/Cognito.js CHANGED
@@ -1,118 +1 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var Cognito_exports = {};
20
- __export(Cognito_exports, {
21
- Cognito: () => Cognito
22
- });
23
- module.exports = __toCommonJS(Cognito_exports);
24
- var import_client_cognito_identity_provider = require("@aws-sdk/client-cognito-identity-provider");
25
- var import_crypto = require("crypto");
26
- class Cognito {
27
- constructor(opts) {
28
- this.randomPassword = (length = 12) => {
29
- return (0, import_crypto.randomBytes)(256).toString("hex").slice(0, length);
30
- };
31
- this.inviteUser = async (opts) => {
32
- var _a, _b;
33
- const temporaryPassword = opts.passwordTemporary || this.randomPassword();
34
- const items = {
35
- UserPoolId: this.userPoolId,
36
- Username: opts.email,
37
- UserAttributes: [
38
- {
39
- Name: "email",
40
- Value: opts.email
41
- },
42
- {
43
- Name: "name",
44
- Value: opts.name
45
- },
46
- {
47
- Name: "email_verified",
48
- Value: "true"
49
- }
50
- ],
51
- DesiredDeliveryMediums: ["EMAIL"],
52
- ForceAliasCreation: false,
53
- MessageAction: opts.sendEmail ? "RESEND" : "SUPPRESS",
54
- TemporaryPassword: temporaryPassword
55
- };
56
- if (opts.phoneNumber && items.UserAttributes) {
57
- items.UserAttributes.push({
58
- Name: "phone_number",
59
- Value: opts.phoneNumber
60
- });
61
- }
62
- if (opts.attributes && items.UserAttributes) {
63
- items.UserAttributes = items.UserAttributes.concat(opts.attributes);
64
- }
65
- const command = new import_client_cognito_identity_provider.AdminCreateUserCommand(items);
66
- try {
67
- const response = await this.cognitoIdentityProvider.send(command);
68
- if (opts.autoConfirm && ((_a = response == null ? void 0 : response.User) == null ? void 0 : _a.UserStatus) === "FORCE_CHANGE_PASSWORD") {
69
- const changePassword = await this.changePassword(
70
- opts.email,
71
- opts.password
72
- );
73
- if (((_b = changePassword == null ? void 0 : changePassword.$metadata) == null ? void 0 : _b.httpStatusCode) !== 200) {
74
- throw new Error("Error changing password");
75
- }
76
- }
77
- return response;
78
- } catch (error) {
79
- throw error;
80
- }
81
- };
82
- this.changePassword = async (username, password) => {
83
- const command = new import_client_cognito_identity_provider.AdminSetUserPasswordCommand({
84
- UserPoolId: this.userPoolId,
85
- Username: username,
86
- Password: password,
87
- Permanent: true
88
- });
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);
105
- };
106
- this.region = (opts == null ? void 0 : opts.region) || process.env.AWS_REGION || "";
107
- this.userPoolId = (opts == null ? void 0 : opts.userPoolId) || process.env.COGNITO_USER_POOL_ID || "";
108
- this.clientId = (opts == null ? void 0 : opts.clientId) || process.env.COGNITO_CLIENT_ID || "";
109
- this.cognitoIdentityProvider = new import_client_cognito_identity_provider.CognitoIdentityProviderClient({
110
- region: this.region
111
- });
112
- }
113
- }
114
- // Annotate the CommonJS export names for ESM import in node:
115
- 0 && (module.exports = {
116
- Cognito
117
- });
118
- //# sourceMappingURL=Cognito.js.map
1
+ "use strict";var d=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var U=(n,e)=>{for(var r in e)d(n,r,{get:e[r],enumerable:!0})},C=(n,e,r,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of I(e))!P.call(n,s)&&s!==r&&d(n,s,{get:()=>e[s],enumerable:!(t=g(e,s))||t.enumerable});return n};var h=n=>C(d({},"__esModule",{value:!0}),n);var b={};U(b,{Cognito:()=>m});module.exports=h(b);var i=require("@aws-sdk/client-cognito-identity-provider"),c=require("crypto"),m=class{constructor(e){this.randomPassword=(e=12)=>(0,c.randomBytes)(256).toString("hex").slice(0,e);this.inviteUser=async e=>{var u,l;let r=e.passwordTemporary||this.randomPassword(),t={UserPoolId:this.userPoolId,Username:e.email,UserAttributes:[{Name:"email",Value:e.email},{Name:"name",Value:e.name},{Name:"email_verified",Value:"true"}],DesiredDeliveryMediums:["EMAIL"],ForceAliasCreation:!1,MessageAction:e.sendEmail?"RESEND":"SUPPRESS",TemporaryPassword:r};e.phoneNumber&&t.UserAttributes&&t.UserAttributes.push({Name:"phone_number",Value:e.phoneNumber}),e.attributes&&t.UserAttributes&&(t.UserAttributes=t.UserAttributes.concat(e.attributes));let s=new i.AdminCreateUserCommand(t);try{let o=await this.cognitoIdentityProvider.send(s);if(e.autoConfirm&&((u=o==null?void 0:o.User)==null?void 0:u.UserStatus)==="FORCE_CHANGE_PASSWORD"){let a=await this.changePassword(e.email,e.password);if(((l=a==null?void 0:a.$metadata)==null?void 0:l.httpStatusCode)!==200)throw new Error("Error changing password")}return o}catch(o){throw o}};this.changePassword=async(e,r)=>{let t=new i.AdminSetUserPasswordCommand({UserPoolId:this.userPoolId,Username:e,Password:r,Permanent:!0});return this.cognitoIdentityProvider.send(t)};this.disableUser=async e=>{let r=new i.AdminDisableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.deleteUser=async e=>{await this.disableUser(e);let r=new i.AdminDeleteUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.listUsers=async e=>{let r=new i.ListUsersCommand({UserPoolId:this.userPoolId,Filter:e||void 0});return this.cognitoIdentityProvider.send(r)};if(this.region=(e==null?void 0:e.region)||process.env.AWS_REGION||"",this.userPoolId=(e==null?void 0:e.userPoolId)||process.env.COGNITO_USER_POOL_ID||"",this.clientId=(e==null?void 0:e.clientId)||process.env.COGNITO_CLIENT_ID||"",!this.region)throw new Error("AWS Region is required");if(!this.userPoolId)throw new Error("Cognito User Pool ID is required");if(!this.clientId)throw new Error("Cognito Client ID is required");this.cognitoIdentityProvider=new i.CognitoIdentityProviderClient({region:this.region})}};0&&(module.exports={Cognito});
package/dist/Cognito.mjs CHANGED
@@ -1,100 +1 @@
1
- import {
2
- AdminCreateUserCommand,
3
- CognitoIdentityProviderClient,
4
- AdminSetUserPasswordCommand,
5
- AdminDisableUserCommand,
6
- AdminDeleteUserCommand
7
- } from "@aws-sdk/client-cognito-identity-provider";
8
- import { randomBytes } from "crypto";
9
- class Cognito {
10
- constructor(opts) {
11
- this.randomPassword = (length = 12) => {
12
- return randomBytes(256).toString("hex").slice(0, length);
13
- };
14
- this.inviteUser = async (opts) => {
15
- var _a, _b;
16
- const temporaryPassword = opts.passwordTemporary || this.randomPassword();
17
- const items = {
18
- UserPoolId: this.userPoolId,
19
- Username: opts.email,
20
- UserAttributes: [
21
- {
22
- Name: "email",
23
- Value: opts.email
24
- },
25
- {
26
- Name: "name",
27
- Value: opts.name
28
- },
29
- {
30
- Name: "email_verified",
31
- Value: "true"
32
- }
33
- ],
34
- DesiredDeliveryMediums: ["EMAIL"],
35
- ForceAliasCreation: false,
36
- MessageAction: opts.sendEmail ? "RESEND" : "SUPPRESS",
37
- TemporaryPassword: temporaryPassword
38
- };
39
- if (opts.phoneNumber && items.UserAttributes) {
40
- items.UserAttributes.push({
41
- Name: "phone_number",
42
- Value: opts.phoneNumber
43
- });
44
- }
45
- if (opts.attributes && items.UserAttributes) {
46
- items.UserAttributes = items.UserAttributes.concat(opts.attributes);
47
- }
48
- const command = new AdminCreateUserCommand(items);
49
- try {
50
- const response = await this.cognitoIdentityProvider.send(command);
51
- if (opts.autoConfirm && ((_a = response == null ? void 0 : response.User) == null ? void 0 : _a.UserStatus) === "FORCE_CHANGE_PASSWORD") {
52
- const changePassword = await this.changePassword(
53
- opts.email,
54
- opts.password
55
- );
56
- if (((_b = changePassword == null ? void 0 : changePassword.$metadata) == null ? void 0 : _b.httpStatusCode) !== 200) {
57
- throw new Error("Error changing password");
58
- }
59
- }
60
- return response;
61
- } catch (error) {
62
- throw error;
63
- }
64
- };
65
- this.changePassword = async (username, password) => {
66
- const command = new AdminSetUserPasswordCommand({
67
- UserPoolId: this.userPoolId,
68
- Username: username,
69
- Password: password,
70
- Permanent: true
71
- });
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);
88
- };
89
- this.region = (opts == null ? void 0 : opts.region) || process.env.AWS_REGION || "";
90
- this.userPoolId = (opts == null ? void 0 : opts.userPoolId) || process.env.COGNITO_USER_POOL_ID || "";
91
- this.clientId = (opts == null ? void 0 : opts.clientId) || process.env.COGNITO_CLIENT_ID || "";
92
- this.cognitoIdentityProvider = new CognitoIdentityProviderClient({
93
- region: this.region
94
- });
95
- }
96
- }
97
- export {
98
- Cognito
99
- };
100
- //# sourceMappingURL=Cognito.mjs.map
1
+ import{AdminCreateUserCommand as m,CognitoIdentityProviderClient as u,AdminSetUserPasswordCommand as l,AdminDisableUserCommand as c,AdminDeleteUserCommand as g,ListUsersCommand as I}from"@aws-sdk/client-cognito-identity-provider";import{randomBytes as P}from"crypto";var a=class{constructor(e){this.randomPassword=(e=12)=>P(256).toString("hex").slice(0,e);this.inviteUser=async e=>{var s,o;let r=e.passwordTemporary||this.randomPassword(),i={UserPoolId:this.userPoolId,Username:e.email,UserAttributes:[{Name:"email",Value:e.email},{Name:"name",Value:e.name},{Name:"email_verified",Value:"true"}],DesiredDeliveryMediums:["EMAIL"],ForceAliasCreation:!1,MessageAction:e.sendEmail?"RESEND":"SUPPRESS",TemporaryPassword:r};e.phoneNumber&&i.UserAttributes&&i.UserAttributes.push({Name:"phone_number",Value:e.phoneNumber}),e.attributes&&i.UserAttributes&&(i.UserAttributes=i.UserAttributes.concat(e.attributes));let d=new m(i);try{let t=await this.cognitoIdentityProvider.send(d);if(e.autoConfirm&&((s=t==null?void 0:t.User)==null?void 0:s.UserStatus)==="FORCE_CHANGE_PASSWORD"){let n=await this.changePassword(e.email,e.password);if(((o=n==null?void 0:n.$metadata)==null?void 0:o.httpStatusCode)!==200)throw new Error("Error changing password")}return t}catch(t){throw t}};this.changePassword=async(e,r)=>{let i=new l({UserPoolId:this.userPoolId,Username:e,Password:r,Permanent:!0});return this.cognitoIdentityProvider.send(i)};this.disableUser=async e=>{let r=new c({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.deleteUser=async e=>{await this.disableUser(e);let r=new g({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.listUsers=async e=>{let r=new I({UserPoolId:this.userPoolId,Filter:e||void 0});return this.cognitoIdentityProvider.send(r)};if(this.region=(e==null?void 0:e.region)||process.env.AWS_REGION||"",this.userPoolId=(e==null?void 0:e.userPoolId)||process.env.COGNITO_USER_POOL_ID||"",this.clientId=(e==null?void 0:e.clientId)||process.env.COGNITO_CLIENT_ID||"",!this.region)throw new Error("AWS Region is required");if(!this.userPoolId)throw new Error("Cognito User Pool ID is required");if(!this.clientId)throw new Error("Cognito Client ID is required");this.cognitoIdentityProvider=new u({region:this.region})}};export{a as Cognito};
package/dist/index.js CHANGED
@@ -1,19 +1 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
- var src_exports = {};
17
- module.exports = __toCommonJS(src_exports);
18
- __reExport(src_exports, require("./Cognito"), module.exports);
19
- //# sourceMappingURL=index.js.map
1
+ "use strict";var d=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var U=(n,e)=>{for(var r in e)d(n,r,{get:e[r],enumerable:!0})},C=(n,e,r,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of I(e))!P.call(n,s)&&s!==r&&d(n,s,{get:()=>e[s],enumerable:!(t=g(e,s))||t.enumerable});return n};var h=n=>C(d({},"__esModule",{value:!0}),n);var w={};U(w,{Cognito:()=>m});module.exports=h(w);var i=require("@aws-sdk/client-cognito-identity-provider"),c=require("crypto"),m=class{constructor(e){this.randomPassword=(e=12)=>(0,c.randomBytes)(256).toString("hex").slice(0,e);this.inviteUser=async e=>{var u,l;let r=e.passwordTemporary||this.randomPassword(),t={UserPoolId:this.userPoolId,Username:e.email,UserAttributes:[{Name:"email",Value:e.email},{Name:"name",Value:e.name},{Name:"email_verified",Value:"true"}],DesiredDeliveryMediums:["EMAIL"],ForceAliasCreation:!1,MessageAction:e.sendEmail?"RESEND":"SUPPRESS",TemporaryPassword:r};e.phoneNumber&&t.UserAttributes&&t.UserAttributes.push({Name:"phone_number",Value:e.phoneNumber}),e.attributes&&t.UserAttributes&&(t.UserAttributes=t.UserAttributes.concat(e.attributes));let s=new i.AdminCreateUserCommand(t);try{let o=await this.cognitoIdentityProvider.send(s);if(e.autoConfirm&&((u=o==null?void 0:o.User)==null?void 0:u.UserStatus)==="FORCE_CHANGE_PASSWORD"){let a=await this.changePassword(e.email,e.password);if(((l=a==null?void 0:a.$metadata)==null?void 0:l.httpStatusCode)!==200)throw new Error("Error changing password")}return o}catch(o){throw o}};this.changePassword=async(e,r)=>{let t=new i.AdminSetUserPasswordCommand({UserPoolId:this.userPoolId,Username:e,Password:r,Permanent:!0});return this.cognitoIdentityProvider.send(t)};this.disableUser=async e=>{let r=new i.AdminDisableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.deleteUser=async e=>{await this.disableUser(e);let r=new i.AdminDeleteUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.listUsers=async e=>{let r=new i.ListUsersCommand({UserPoolId:this.userPoolId,Filter:e||void 0});return this.cognitoIdentityProvider.send(r)};if(this.region=(e==null?void 0:e.region)||process.env.AWS_REGION||"",this.userPoolId=(e==null?void 0:e.userPoolId)||process.env.COGNITO_USER_POOL_ID||"",this.clientId=(e==null?void 0:e.clientId)||process.env.COGNITO_CLIENT_ID||"",!this.region)throw new Error("AWS Region is required");if(!this.userPoolId)throw new Error("Cognito User Pool ID is required");if(!this.clientId)throw new Error("Cognito Client ID is required");this.cognitoIdentityProvider=new i.CognitoIdentityProviderClient({region:this.region})}};0&&(module.exports={Cognito});
package/dist/index.mjs CHANGED
@@ -1,2 +1 @@
1
- export * from "./Cognito";
2
- //# sourceMappingURL=index.mjs.map
1
+ import{AdminCreateUserCommand as l,CognitoIdentityProviderClient as c,AdminSetUserPasswordCommand as g,AdminDisableUserCommand as I,AdminDeleteUserCommand as P,ListUsersCommand as U}from"@aws-sdk/client-cognito-identity-provider";import{randomBytes as C}from"crypto";var a=class{constructor(e){this.randomPassword=(e=12)=>C(256).toString("hex").slice(0,e);this.inviteUser=async e=>{var s,o;let r=e.passwordTemporary||this.randomPassword(),i={UserPoolId:this.userPoolId,Username:e.email,UserAttributes:[{Name:"email",Value:e.email},{Name:"name",Value:e.name},{Name:"email_verified",Value:"true"}],DesiredDeliveryMediums:["EMAIL"],ForceAliasCreation:!1,MessageAction:e.sendEmail?"RESEND":"SUPPRESS",TemporaryPassword:r};e.phoneNumber&&i.UserAttributes&&i.UserAttributes.push({Name:"phone_number",Value:e.phoneNumber}),e.attributes&&i.UserAttributes&&(i.UserAttributes=i.UserAttributes.concat(e.attributes));let d=new l(i);try{let t=await this.cognitoIdentityProvider.send(d);if(e.autoConfirm&&((s=t==null?void 0:t.User)==null?void 0:s.UserStatus)==="FORCE_CHANGE_PASSWORD"){let n=await this.changePassword(e.email,e.password);if(((o=n==null?void 0:n.$metadata)==null?void 0:o.httpStatusCode)!==200)throw new Error("Error changing password")}return t}catch(t){throw t}};this.changePassword=async(e,r)=>{let i=new g({UserPoolId:this.userPoolId,Username:e,Password:r,Permanent:!0});return this.cognitoIdentityProvider.send(i)};this.disableUser=async e=>{let r=new I({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.deleteUser=async e=>{await this.disableUser(e);let r=new P({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.listUsers=async e=>{let r=new U({UserPoolId:this.userPoolId,Filter:e||void 0});return this.cognitoIdentityProvider.send(r)};if(this.region=(e==null?void 0:e.region)||process.env.AWS_REGION||"",this.userPoolId=(e==null?void 0:e.userPoolId)||process.env.COGNITO_USER_POOL_ID||"",this.clientId=(e==null?void 0:e.clientId)||process.env.COGNITO_CLIENT_ID||"",!this.region)throw new Error("AWS Region is required");if(!this.userPoolId)throw new Error("Cognito User Pool ID is required");if(!this.clientId)throw new Error("Cognito Client ID is required");this.cognitoIdentityProvider=new c({region:this.region})}};export{a as Cognito};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juniyadi/cognito",
3
- "version": "0.0.2",
3
+ "version": "0.1.0",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
@@ -9,6 +9,11 @@
9
9
  "files": [
10
10
  "dist/**"
11
11
  ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/JuniYadi/nodejs",
15
+ "directory": "packages/cognito"
16
+ },
12
17
  "devDependencies": {
13
18
  "@aws-sdk/client-cognito-identity": "^3.388.0",
14
19
  "@aws-sdk/client-cognito-identity-provider": "^3.388.0",
@@ -19,9 +24,11 @@
19
24
  "jest": "^29.6.2",
20
25
  "ts-jest": "^29.1.1",
21
26
  "tsup": "^5.12.9",
27
+ "typedoc": "^0.24.8",
22
28
  "typescript": "^4.9.5",
23
29
  "@juniyadi/tsconfig": "0.0.0",
24
- "eslint-config-juniyadi": "0.0.1"
30
+ "eslint-config-juniyadi": "0.0.1",
31
+ "@juniyadi/config-typedoc": "0.0.0"
25
32
  },
26
33
  "publishConfig": {
27
34
  "access": "public"
@@ -31,7 +38,8 @@
31
38
  "@aws-sdk/client-cognito-identity-provider": "^3.388.0"
32
39
  },
33
40
  "scripts": {
34
- "build": "tsup",
41
+ "docs": "typedoc --out docs src/index.ts",
42
+ "build": "cross-env NODE_ENV=production tsup",
35
43
  "dev": "tsup --watch",
36
44
  "lint": "eslint \"src/**/*.ts*\"",
37
45
  "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
@@ -1 +0,0 @@
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,YACE,MAAM;AAAA,YACN,OAAO;AAAA,UACT;AAAA,QACF;AAAA,QACA,wBAAwB,CAAC,OAAO;AAAA,QAChC,oBAAoB;AAAA,QACpB,eAAe,KAAK,YAAY,WAAW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C;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 +0,0 @@
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,OAEK;AACP,SAAS,mBAAmB;AAwBrB,MAAM,QAAQ;AAAA,EAMnB,YAAY,MAAiB;AAgB7B,SAAO,iBAAiB,CAAC,SAAS,OAAe;AAC/C,aAAO,YAAY,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,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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AAQ,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":[]}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from \"./Cognito\";\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,sBAAd;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from \"./Cognito\";\n"],"mappings":"AAAA,cAAc;","names":[]}