@juniyadi/cognito 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Juni Yadi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # Cognito
2
+
3
+ ![npm](https://img.shields.io/npm/dm/@juniyadi/cognito)
4
+ ![npm](https://img.shields.io/npm/v/@juniyadi/cognito)
5
+ ![NPM](https://img.shields.io/npm/l/@juniyadi/cognito)
6
+ ![npm bundle size](https://img.shields.io/bundlephobia/min/@juniyadi/cognito)
@@ -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, AdminDisableUserCommandOutput, AdminDeleteUserCommandOutput, AdminAddUserToGroupCommandOutput } from '@aws-sdk/client-cognito-identity-provider';
2
+ import { CognitoIdentityProviderClient, AdminSetUserPasswordCommandOutput, AdminDisableUserCommandOutput, AdminEnableUserCommandOutput, AdminDeleteUserCommandOutput, AdminAddUserToGroupCommandOutput } from '@aws-sdk/client-cognito-identity-provider';
3
3
 
4
4
  interface ICognito {
5
5
  region?: string;
@@ -74,6 +74,18 @@ declare class Cognito {
74
74
  * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminDisableUser.html
75
75
  */
76
76
  disableUser: (username: string) => Promise<AdminDisableUserCommandOutput>;
77
+ /**
78
+ * Enable a user in Cognito User Pool
79
+ * @param username - Username of the user
80
+ * @returns Promise<AdminEnableUserCommandOutput>
81
+ * @example
82
+ * const cognito = new Cognito();
83
+ * const users = await cognito.enableUser("johndoe@example.com");
84
+ * console.log(users.Users);
85
+ *
86
+ * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminEnableUser.html
87
+ */
88
+ enableUser: (username: string) => Promise<AdminEnableUserCommandOutput>;
77
89
  /**
78
90
  * Delete a user in Cognito User Pool
79
91
  * @param username - Username of the user
package/dist/index.d.ts CHANGED
@@ -1,2 +1,126 @@
1
- export { Cognito, ICognito, ICognitoAttributes, ICognitoInviteUser } from './Cognito.js';
2
- import '@aws-sdk/client-cognito-identity-provider';
1
+ import * as _aws_sdk_client_cognito_identity_provider from '@aws-sdk/client-cognito-identity-provider';
2
+ import { CognitoIdentityProviderClient, AdminSetUserPasswordCommandOutput, AdminDisableUserCommandOutput, AdminEnableUserCommandOutput, AdminDeleteUserCommandOutput, AdminAddUserToGroupCommandOutput } from '@aws-sdk/client-cognito-identity-provider';
3
+
4
+ interface ICognito {
5
+ region?: string;
6
+ userPoolId?: string;
7
+ clientId?: string;
8
+ }
9
+ interface ICognitoAttributes {
10
+ Name: string;
11
+ Value: string;
12
+ }
13
+ interface ICognitoInviteUser {
14
+ name: string;
15
+ email: string;
16
+ password: string;
17
+ passwordTemporary?: string;
18
+ autoConfirm?: boolean;
19
+ phoneNumber?: string;
20
+ sendEmail?: boolean;
21
+ attributes?: ICognitoAttributes[];
22
+ group?: string;
23
+ }
24
+ declare class Cognito {
25
+ region: string;
26
+ userPoolId: string;
27
+ clientId: string;
28
+ cognitoIdentityProvider: CognitoIdentityProviderClient;
29
+ /**
30
+ * Create a new Cognito instance
31
+ * @param opts
32
+ * @example
33
+ * const cognito = new Cognito({
34
+ * region: "us-east-1",
35
+ * userPoolId: "us-east-1_123456789",
36
+ * clientId: "12345678901234567890",
37
+ * });
38
+ */
39
+ constructor(opts?: ICognito);
40
+ /**
41
+ * Generate a random password of a given length
42
+ * @param length
43
+ * @returns string
44
+ * @example
45
+ * const password = randomPassword(12);
46
+ * console.log(password);
47
+ */
48
+ randomPassword: (length?: number) => string;
49
+ /**
50
+ * Invite a user to the Cognito User Pool
51
+ * @param opts
52
+ */
53
+ inviteUser: (opts: ICognitoInviteUser) => Promise<_aws_sdk_client_cognito_identity_provider.AdminCreateUserCommandOutput>;
54
+ /**
55
+ * Change user password in Cognito User Pool
56
+ * @param username - Username of the user
57
+ * @param password - New password for the user
58
+ * @returns Promise<AdminSetUserPasswordCommandOutput>
59
+ * @example
60
+ * const cognito = new Cognito();
61
+ * const users = await cognito.changePassword("johndoe@example.com", "newPassword");
62
+ * console.log(users.Users);
63
+ * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminSetUserPassword.html
64
+ */
65
+ changePassword: (username: string, password: string) => Promise<AdminSetUserPasswordCommandOutput>;
66
+ /**
67
+ * Disable a user in Cognito User Pool
68
+ * @param username - Username of the user
69
+ * @returns Promise<AdminDisableUserCommandOutput>
70
+ * @example
71
+ * const cognito = new Cognito();
72
+ * const users = await cognito.disableUser("johndoe@example.com");
73
+ * console.log(users.Users);
74
+ * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminDisableUser.html
75
+ */
76
+ disableUser: (username: string) => Promise<AdminDisableUserCommandOutput>;
77
+ /**
78
+ * Enable a user in Cognito User Pool
79
+ * @param username - Username of the user
80
+ * @returns Promise<AdminEnableUserCommandOutput>
81
+ * @example
82
+ * const cognito = new Cognito();
83
+ * const users = await cognito.enableUser("johndoe@example.com");
84
+ * console.log(users.Users);
85
+ *
86
+ * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminEnableUser.html
87
+ */
88
+ enableUser: (username: string) => Promise<AdminEnableUserCommandOutput>;
89
+ /**
90
+ * Delete a user in Cognito User Pool
91
+ * @param username - Username of the user
92
+ * @returns Promise<AdminDeleteUserCommandOutput>
93
+ * @example
94
+ * const cognito = new Cognito();
95
+ * const users = await cognito.deleteUser("johndoe@example.com");
96
+ * console.log(users.Users);
97
+ * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminDeleteUser.html
98
+ */
99
+ deleteUser: (username: string) => Promise<AdminDeleteUserCommandOutput>;
100
+ /**
101
+ * List for users in Cognito User Pool
102
+ * @param opts
103
+ * @returns Promise<ListUsersCommandOutput>
104
+ * @example
105
+ * const cognito = new Cognito();
106
+ * const users = await cognito.listUsers({ email: "johndoe@example.com" });
107
+ * console.log(users.Users);
108
+ * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html
109
+ */
110
+ listUsers: (filter?: string) => Promise<_aws_sdk_client_cognito_identity_provider.ListUsersCommandOutput>;
111
+ /**
112
+ * Add a user to a group in Cognito User Pool
113
+ * @param username - Username of the user
114
+ * @param groupName - Name of the group
115
+ * @returns Promise<AdminAddUserToGroupCommandOutput>
116
+ * @example
117
+ * const cognito = new Cognito();
118
+ * const users = await cognito.addUserToGroup("johndoe@example.com", "Admin");
119
+ * console.log(users.Users);
120
+ *
121
+ * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminAddUserToGroup.html
122
+ */
123
+ addUserToGroup: (username: string, groupName: string) => Promise<AdminAddUserToGroupCommandOutput>;
124
+ }
125
+
126
+ export { Cognito, ICognito, ICognitoAttributes, ICognitoInviteUser };
package/dist/index.js CHANGED
@@ -1 +1,8 @@
1
- "use strict";var d=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var I=(n,e)=>{for(var r in e)d(n,r,{get:e[r],enumerable:!0})},P=(n,e,r,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of c(e))!g.call(n,o)&&o!==r&&d(n,o,{get:()=>e[o],enumerable:!(t=l(e,o))||t.enumerable});return n};var U=n=>P(d({},"__esModule",{value:!0}),n);var h={};I(h,{Cognito:()=>a});module.exports=U(h);var i=require("@aws-sdk/client-cognito-identity-provider"),u=require("crypto"),a=class{constructor(e){this.randomPassword=(e=12)=>(0,u.randomBytes)(256).toString("hex").slice(0,e);this.inviteUser=async e=>{var m;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 o=new i.AdminCreateUserCommand(t);try{let s=await this.cognitoIdentityProvider.send(o);return e.autoConfirm&&((m=s==null?void 0:s.User)==null?void 0:m.UserStatus)==="FORCE_CHANGE_PASSWORD"&&await this.changePassword(e.email,e.password),e.group&&await this.addUserToGroup(e.email,e.group),s}catch(s){throw s}};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)};this.addUserToGroup=async(e,r)=>{let t=new i.AdminAddUserToGroupCommand({UserPoolId:this.userPoolId,Username:e,GroupName:r});return this.cognitoIdentityProvider.send(t)};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});
1
+ 'use strict';
2
+
3
+ var clientCognitoIdentityProvider = require('@aws-sdk/client-cognito-identity-provider');
4
+ var crypto = require('crypto');
5
+
6
+ var n=class{constructor(e){this.randomPassword=(e=12)=>crypto.randomBytes(256).toString("hex").slice(0,e);this.inviteUser=async e=>{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 s=new clientCognitoIdentityProvider.AdminCreateUserCommand(i);try{let t=await this.cognitoIdentityProvider.send(s);return e.autoConfirm&&t?.User?.UserStatus==="FORCE_CHANGE_PASSWORD"&&await this.changePassword(e.email,e.password),e.group&&await this.addUserToGroup(e.email,e.group),t}catch(t){throw t}};this.changePassword=async(e,r)=>{let i=new clientCognitoIdentityProvider.AdminSetUserPasswordCommand({UserPoolId:this.userPoolId,Username:e,Password:r,Permanent:!0});return this.cognitoIdentityProvider.send(i)};this.disableUser=async e=>{let r=new clientCognitoIdentityProvider.AdminDisableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.enableUser=async e=>{let r=new clientCognitoIdentityProvider.AdminEnableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.deleteUser=async e=>{await this.disableUser(e);let r=new clientCognitoIdentityProvider.AdminDeleteUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.listUsers=async e=>{let r=new clientCognitoIdentityProvider.ListUsersCommand({UserPoolId:this.userPoolId,Filter:e||void 0});return this.cognitoIdentityProvider.send(r)};this.addUserToGroup=async(e,r)=>{let i=new clientCognitoIdentityProvider.AdminAddUserToGroupCommand({UserPoolId:this.userPoolId,Username:e,GroupName:r});return this.cognitoIdentityProvider.send(i)};if(this.region=e?.region||process.env.AWS_REGION||"",this.userPoolId=e?.userPoolId||process.env.COGNITO_USER_POOL_ID||"",this.clientId=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 clientCognitoIdentityProvider.CognitoIdentityProviderClient({region:this.region});}};
7
+
8
+ exports.Cognito = n;
package/dist/index.mjs CHANGED
@@ -1 +1,6 @@
1
- import{AdminCreateUserCommand as m,CognitoIdentityProviderClient as u,AdminSetUserPasswordCommand as l,AdminDisableUserCommand as c,AdminDeleteUserCommand as g,ListUsersCommand as I,AdminAddUserToGroupCommand as P}from"@aws-sdk/client-cognito-identity-provider";import{randomBytes as U}from"crypto";var o=class{constructor(e){this.randomPassword=(e=12)=>U(256).toString("hex").slice(0,e);this.inviteUser=async e=>{var n;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 s=new m(i);try{let t=await this.cognitoIdentityProvider.send(s);return e.autoConfirm&&((n=t==null?void 0:t.User)==null?void 0:n.UserStatus)==="FORCE_CHANGE_PASSWORD"&&await this.changePassword(e.email,e.password),e.group&&await this.addUserToGroup(e.email,e.group),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)};this.addUserToGroup=async(e,r)=>{let i=new P({UserPoolId:this.userPoolId,Username:e,GroupName:r});return this.cognitoIdentityProvider.send(i)};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{o as Cognito};
1
+ import { AdminCreateUserCommand, AdminSetUserPasswordCommand, AdminDisableUserCommand, AdminEnableUserCommand, AdminDeleteUserCommand, ListUsersCommand, AdminAddUserToGroupCommand, CognitoIdentityProviderClient } from '@aws-sdk/client-cognito-identity-provider';
2
+ import { randomBytes } from 'crypto';
3
+
4
+ var n=class{constructor(e){this.randomPassword=(e=12)=>randomBytes(256).toString("hex").slice(0,e);this.inviteUser=async e=>{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 s=new AdminCreateUserCommand(i);try{let t=await this.cognitoIdentityProvider.send(s);return e.autoConfirm&&t?.User?.UserStatus==="FORCE_CHANGE_PASSWORD"&&await this.changePassword(e.email,e.password),e.group&&await this.addUserToGroup(e.email,e.group),t}catch(t){throw t}};this.changePassword=async(e,r)=>{let i=new AdminSetUserPasswordCommand({UserPoolId:this.userPoolId,Username:e,Password:r,Permanent:!0});return this.cognitoIdentityProvider.send(i)};this.disableUser=async e=>{let r=new AdminDisableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.enableUser=async e=>{let r=new AdminEnableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.deleteUser=async e=>{await this.disableUser(e);let r=new AdminDeleteUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.listUsers=async e=>{let r=new ListUsersCommand({UserPoolId:this.userPoolId,Filter:e||void 0});return this.cognitoIdentityProvider.send(r)};this.addUserToGroup=async(e,r)=>{let i=new AdminAddUserToGroupCommand({UserPoolId:this.userPoolId,Username:e,GroupName:r});return this.cognitoIdentityProvider.send(i)};if(this.region=e?.region||process.env.AWS_REGION||"",this.userPoolId=e?.userPoolId||process.env.COGNITO_USER_POOL_ID||"",this.clientId=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 CognitoIdentityProviderClient({region:this.region});}};
5
+
6
+ export { n as Cognito };
package/package.json CHANGED
@@ -1,9 +1,20 @@
1
1
  {
2
2
  "name": "@juniyadi/cognito",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "main": "./dist/index.js",
5
- "module": "./dist/index.mjs",
6
5
  "types": "./dist/index.d.ts",
6
+ "exports": {
7
+ ".": {
8
+ "import": {
9
+ "default": "./dist/index.mjs",
10
+ "types": "./dist/index.d.mts"
11
+ },
12
+ "require": {
13
+ "default": "./dist/index.js",
14
+ "types": "./dist/index.d.ts"
15
+ }
16
+ }
17
+ },
7
18
  "sideEffects": false,
8
19
  "license": "MIT",
9
20
  "files": [
@@ -18,14 +29,14 @@
18
29
  "@aws-sdk/client-cognito-identity": "^3.388.0",
19
30
  "@aws-sdk/client-cognito-identity-provider": "^3.388.0",
20
31
  "@types/jest": "^29.5.3",
21
- "@types/node": "^20.4.9",
32
+ "@types/node": "^20.5.0",
22
33
  "aws-sdk-client-mock": "^3.0.0",
23
- "eslint": "^8.46.0",
34
+ "eslint": "^8.47.0",
24
35
  "jest": "^29.6.2",
25
36
  "ts-jest": "^29.1.1",
26
- "tsup": "^5.12.9",
37
+ "tsup": "^7.2.0",
27
38
  "typedoc": "^0.24.8",
28
- "typescript": "^4.9.5",
39
+ "typescript": "^5.1.6",
29
40
  "@juniyadi/tsconfig": "0.0.0",
30
41
  "@juniyadi/config-typedoc": "0.0.0",
31
42
  "eslint-config-juniyadi": "0.0.1"
package/dist/Cognito.js DELETED
@@ -1 +0,0 @@
1
- "use strict";var d=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var I=(t,e)=>{for(var r in e)d(t,r,{get:e[r],enumerable:!0})},P=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of c(e))!g.call(t,s)&&s!==r&&d(t,s,{get:()=>e[s],enumerable:!(n=l(e,s))||n.enumerable});return t};var U=t=>P(d({},"__esModule",{value:!0}),t);var C={};I(C,{Cognito:()=>a});module.exports=U(C);var i=require("@aws-sdk/client-cognito-identity-provider"),u=require("crypto"),a=class{constructor(e){this.randomPassword=(e=12)=>(0,u.randomBytes)(256).toString("hex").slice(0,e);this.inviteUser=async e=>{var m;let r=e.passwordTemporary||this.randomPassword(),n={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&&n.UserAttributes&&n.UserAttributes.push({Name:"phone_number",Value:e.phoneNumber}),e.attributes&&n.UserAttributes&&(n.UserAttributes=n.UserAttributes.concat(e.attributes));let s=new i.AdminCreateUserCommand(n);try{let o=await this.cognitoIdentityProvider.send(s);return e.autoConfirm&&((m=o==null?void 0:o.User)==null?void 0:m.UserStatus)==="FORCE_CHANGE_PASSWORD"&&await this.changePassword(e.email,e.password),e.group&&await this.addUserToGroup(e.email,e.group),o}catch(o){throw o}};this.changePassword=async(e,r)=>{let n=new i.AdminSetUserPasswordCommand({UserPoolId:this.userPoolId,Username:e,Password:r,Permanent:!0});return this.cognitoIdentityProvider.send(n)};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)};this.addUserToGroup=async(e,r)=>{let n=new i.AdminAddUserToGroupCommand({UserPoolId:this.userPoolId,Username:e,GroupName:r});return this.cognitoIdentityProvider.send(n)};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 DELETED
@@ -1 +0,0 @@
1
- import{AdminCreateUserCommand as d,CognitoIdentityProviderClient as a,AdminSetUserPasswordCommand as m,AdminDisableUserCommand as u,AdminDeleteUserCommand as l,ListUsersCommand as c,AdminAddUserToGroupCommand as g}from"@aws-sdk/client-cognito-identity-provider";import{randomBytes as I}from"crypto";var s=class{constructor(e){this.randomPassword=(e=12)=>I(256).toString("hex").slice(0,e);this.inviteUser=async e=>{var t;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 o=new d(i);try{let n=await this.cognitoIdentityProvider.send(o);return e.autoConfirm&&((t=n==null?void 0:n.User)==null?void 0:t.UserStatus)==="FORCE_CHANGE_PASSWORD"&&await this.changePassword(e.email,e.password),e.group&&await this.addUserToGroup(e.email,e.group),n}catch(n){throw n}};this.changePassword=async(e,r)=>{let i=new m({UserPoolId:this.userPoolId,Username:e,Password:r,Permanent:!0});return this.cognitoIdentityProvider.send(i)};this.disableUser=async e=>{let r=new u({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.deleteUser=async e=>{await this.disableUser(e);let r=new l({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.listUsers=async e=>{let r=new c({UserPoolId:this.userPoolId,Filter:e||void 0});return this.cognitoIdentityProvider.send(r)};this.addUserToGroup=async(e,r)=>{let i=new g({UserPoolId:this.userPoolId,Username:e,GroupName:r});return this.cognitoIdentityProvider.send(i)};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 a({region:this.region})}};export{s as Cognito};