@juniyadi/cognito 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.mts CHANGED
@@ -13,7 +13,7 @@ interface ICognitoAttributes {
13
13
  interface ICognitoInviteUser {
14
14
  name: string;
15
15
  email: string;
16
- password: string;
16
+ password?: string;
17
17
  passwordTemporary?: string;
18
18
  autoConfirm?: boolean;
19
19
  phoneNumber?: string;
package/dist/index.d.ts CHANGED
@@ -13,7 +13,7 @@ interface ICognitoAttributes {
13
13
  interface ICognitoInviteUser {
14
14
  name: string;
15
15
  email: string;
16
- password: string;
16
+ password?: string;
17
17
  passwordTemporary?: string;
18
18
  autoConfirm?: boolean;
19
19
  phoneNumber?: string;
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var clientCognitoIdentityProvider = require('@aws-sdk/client-cognito-identity-provider');
4
- var crypto = require('crypto');
4
+ var randomString = require('@juniyadi/random-string');
5
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});}};
6
+ var s=class{constructor(e){this.randomPassword=(e=12)=>randomString.generate(e,{lowercase:!0,uppercase:!0,numbers:!0});this.inviteUser=async e=>{let r=e.password||this.randomPassword(10),i=e.passwordTemporary||this.randomPassword(12),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:i};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 clientCognitoIdentityProvider.AdminCreateUserCommand(t);try{let n=await this.cognitoIdentityProvider.send(o);return e.autoConfirm&&n?.User?.UserStatus==="FORCE_CHANGE_PASSWORD"&&await this.changePassword(e.email,r),e.group&&await this.addUserToGroup(e.email,e.group),n}catch(n){throw n}};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
7
 
8
- exports.Cognito = n;
8
+ exports.Cognito = s;
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AdminCreateUserCommand, AdminSetUserPasswordCommand, AdminDisableUserCommand, AdminEnableUserCommand, AdminDeleteUserCommand, ListUsersCommand, AdminAddUserToGroupCommand, CognitoIdentityProviderClient } from '@aws-sdk/client-cognito-identity-provider';
2
- import { randomBytes } from 'crypto';
2
+ import { generate } from '@juniyadi/random-string';
3
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});}};
4
+ var s=class{constructor(e){this.randomPassword=(e=12)=>generate(e,{lowercase:!0,uppercase:!0,numbers:!0});this.inviteUser=async e=>{let r=e.password||this.randomPassword(10),i=e.passwordTemporary||this.randomPassword(12),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:i};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 AdminCreateUserCommand(t);try{let n=await this.cognitoIdentityProvider.send(o);return e.autoConfirm&&n?.User?.UserStatus==="FORCE_CHANGE_PASSWORD"&&await this.changePassword(e.email,r),e.group&&await this.addUserToGroup(e.email,e.group),n}catch(n){throw n}};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
5
 
6
- export { n as Cognito };
6
+ export { s as Cognito };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juniyadi/cognito",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "exports": {
@@ -37,16 +37,20 @@
37
37
  "tsup": "^7.2.0",
38
38
  "typedoc": "^0.24.8",
39
39
  "typescript": "^5.1.6",
40
- "@juniyadi/tsconfig": "0.0.0",
41
40
  "@juniyadi/config-typedoc": "0.0.0",
41
+ "@juniyadi/tsconfig": "0.0.0",
42
42
  "eslint-config-juniyadi": "0.0.1"
43
43
  },
44
44
  "publishConfig": {
45
45
  "access": "public"
46
46
  },
47
+ "dependencies": {
48
+ "@juniyadi/random-string": "0.1.0"
49
+ },
47
50
  "peerDependencies": {
48
51
  "@aws-sdk/client-cognito-identity": "^3.388.0",
49
- "@aws-sdk/client-cognito-identity-provider": "^3.388.0"
52
+ "@aws-sdk/client-cognito-identity-provider": "^3.388.0",
53
+ "@juniyadi/random-string": "0.1.0"
50
54
  },
51
55
  "scripts": {
52
56
  "docs": "typedoc --out docs src/index.ts",