@adtrackify/at-service-common 1.1.17 → 1.1.19

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.
@@ -1,10 +1,6 @@
1
1
  /* eslint-disable no-useless-escape */
2
2
  import { AdminConfirmSignUpCommandInput, AdminDeleteUserCommandInput, AdminUpdateUserAttributesCommandInput, CognitoIdentityProvider, ConfirmSignUpCommandInput, ForgotPasswordCommandInput, ListUsersCommandInput, ResendConfirmationCodeCommandInput, SignUpCommandInput } from '@aws-sdk/client-cognito-identity-provider';
3
3
  import * as log from 'lambda-log';
4
- const cognitoClient = new CognitoIdentityProvider({});
5
-
6
- let USER_POOL_NO_SECRET_CLIENT_ID: string;
7
- let USER_POOL_ID: string;
8
4
 
9
5
  export function dictToAwsAttributes(input: any) {
10
6
  delete input.email;
@@ -26,50 +22,55 @@ const buildAttributes = (data: any) => {
26
22
  return dictToAwsAttributes(attributes);
27
23
  };
28
24
 
29
- export class CognitoService {
30
- static setCredentials = (userPoolId: string, userPoolNoSecretClientId: string) => {
31
- USER_POOL_ID = userPoolId;
32
- USER_POOL_NO_SECRET_CLIENT_ID = userPoolNoSecretClientId;
25
+ export class CognitoClient {
26
+ public cognitoClient: any
27
+ public USER_POOL_ID: string;
28
+ public USER_POOL_NO_SECRET_CLIENT_ID: string;
29
+
30
+ constructor(userPoolId: string, userPoolNoSecretClientId: string) {
31
+ this.cognitoClient = new CognitoIdentityProvider({});
32
+ this.USER_POOL_ID = userPoolId;
33
+ this.USER_POOL_NO_SECRET_CLIENT_ID = userPoolNoSecretClientId;
33
34
  }
34
35
 
35
- static signupUser = async (data: any) => {
36
+ public signupUser = async (data: any) => {
36
37
  const params: SignUpCommandInput = {
37
- ClientId: USER_POOL_NO_SECRET_CLIENT_ID,
38
+ ClientId: this.USER_POOL_NO_SECRET_CLIENT_ID,
38
39
  Password: data.password,
39
40
  Username: data.email,
40
41
  UserAttributes: buildAttributes(data),
41
42
  };
42
- const cognitoResponse = await cognitoClient.signUp(params);
43
+ const cognitoResponse = await this.cognitoClient.signUp(params);
43
44
  log.debug('Successfully Registered User', { cognitoResponse });
44
45
  return cognitoResponse;
45
46
  }
46
47
 
47
- static forgotPassword = async (email: string) => {
48
- await CognitoService.adminEmailVerify(email);
48
+ public forgotPassword = async (email: string) => {
49
+ await this.adminEmailVerify(email);
49
50
  const params: ForgotPasswordCommandInput = {
50
- ClientId: USER_POOL_NO_SECRET_CLIENT_ID,
51
+ ClientId: this.USER_POOL_NO_SECRET_CLIENT_ID,
51
52
  Username: email,
52
53
  };
53
- const cognitoResponse = await cognitoClient.forgotPassword(params);
54
+ const cognitoResponse = await this.cognitoClient.forgotPassword(params);
54
55
  log.debug('Sent Forgot Password', { cognitoResponse });
55
56
  return cognitoResponse;
56
57
  }
57
58
 
58
- static adminEmailVerify = async (email: string) => {
59
+ public adminEmailVerify = async (email: string) => {
59
60
  try {
60
- const user: any = await CognitoService.getUserByEmail(email);
61
+ const user: any = await this.getUserByEmail(email);
61
62
  if (user && user?.UserStatus === 'CONFIRMED' && user?.email_verified !== 'true') {
62
- await CognitoService.forceValidateEmail(email);
63
+ await this.forceValidateEmail(email);
63
64
  }
64
65
  } catch (error) {
65
66
  log.error('Failed admin email verify', { error });
66
67
  }
67
68
  }
68
69
 
69
- static forceValidateEmail = async (email: string) => {
70
+ public forceValidateEmail = async (email: string) => {
70
71
  try {
71
72
  const params: AdminUpdateUserAttributesCommandInput = {
72
- UserPoolId: USER_POOL_ID,
73
+ UserPoolId: this.USER_POOL_ID,
73
74
  Username: email,
74
75
  UserAttributes: [
75
76
  {
@@ -78,60 +79,60 @@ export class CognitoService {
78
79
  },
79
80
  ],
80
81
  };
81
- await cognitoClient.adminUpdateUserAttributes(params);
82
+ await this.cognitoClient.adminUpdateUserAttributes(params);
82
83
  } catch (error) {
83
84
  log.error('Failed force validate email', { email, error });
84
85
  }
85
86
  }
86
87
 
87
- static adminConfirmUser = async (data: any) => {
88
+ public adminConfirmUser = async (data: any) => {
88
89
  const params: AdminConfirmSignUpCommandInput = {
89
- UserPoolId: USER_POOL_ID,
90
+ UserPoolId: this.USER_POOL_ID,
90
91
  Username: data.email,
91
92
  };
92
- const cognitoResponse = await cognitoClient.adminConfirmSignUp(params);
93
- await CognitoService.forceValidateEmail(data.email);
93
+ const cognitoResponse = await this.cognitoClient.adminConfirmSignUp(params);
94
+ await this.forceValidateEmail(data.email);
94
95
  log.debug('Admin Successfully Confirmed User', { cognitoResponse });
95
96
  return cognitoResponse;
96
97
  }
97
98
 
98
- static confirmUser = async (data: any) => {
99
+ public confirmUser = async (data: any) => {
99
100
  const params: ConfirmSignUpCommandInput = {
100
- ClientId: USER_POOL_NO_SECRET_CLIENT_ID,
101
+ ClientId: this.USER_POOL_NO_SECRET_CLIENT_ID,
101
102
  ConfirmationCode: data.confirmationCode,
102
103
  Username: data.email,
103
104
  };
104
- const cognitoResponse = await cognitoClient.confirmSignUp(params);
105
+ const cognitoResponse = await this.cognitoClient.confirmSignUp(params);
105
106
  log.debug('Successfully Confirmed User', { cognitoResponse });
106
107
  return cognitoResponse;
107
108
  }
108
109
 
109
- static resendCode = async (email: string) => {
110
+ public resendCode = async (email: string) => {
110
111
  const params: ResendConfirmationCodeCommandInput = {
111
- ClientId: USER_POOL_NO_SECRET_CLIENT_ID,
112
+ ClientId: this.USER_POOL_NO_SECRET_CLIENT_ID,
112
113
  Username: email,
113
114
  };
114
- await cognitoClient.resendConfirmationCode(params);
115
+ await this.cognitoClient.resendConfirmationCode(params);
115
116
  log.debug('Successfully Resend Confirmation Code', { email });
116
117
  return;
117
118
  }
118
119
 
119
- static adminDeleteUser = async (userId: string) => {
120
+ public adminDeleteUser = async (userId: string) => {
120
121
  const params: AdminDeleteUserCommandInput = {
121
- UserPoolId: USER_POOL_ID,
122
+ UserPoolId: this.USER_POOL_ID,
122
123
  Username: userId,
123
124
  };
124
- await cognitoClient.adminDeleteUser(params);
125
+ await this.cognitoClient.adminDeleteUser(params);
125
126
  return true;
126
127
  }
127
128
 
128
- static getUserByEmail = async (email: string) => {
129
+ public getUserByEmail = async (email: string) => {
129
130
  const params: ListUsersCommandInput = {
130
- UserPoolId: USER_POOL_ID,
131
+ UserPoolId: this.USER_POOL_ID,
131
132
  Filter: `email=\"${email}\"`,
132
133
  Limit: 1,
133
134
  };
134
- const cognitoResponse: any = await cognitoClient.listUsers(params);
135
+ const cognitoResponse: any = await this.cognitoClient.listUsers(params);
135
136
  log.debug('Get Users by Email', { cognitoResponse });
136
137
 
137
138
  if (cognitoResponse?.Users && cognitoResponse?.Users.length > 0) {
@@ -2,3 +2,4 @@ export * from './dynamodb-client';
2
2
  export * from './eventbridge-client';
3
3
  export * from './http-client';
4
4
  export * from './s3-client';
5
+ export * from './cognito-client';
@@ -1,21 +1,17 @@
1
- import { S3 } from '@aws-sdk/client-s3';
1
+ import { ObjectCannedACL, S3 } from '@aws-sdk/client-s3';
2
2
  import * as log from 'lambda-log';
3
3
 
4
4
  export class S3Client {
5
5
  s3: S3;
6
6
 
7
- constructor(accessKeyId: string, secretAccessKey: string){
8
- this.s3 = new S3({
9
- credentials: {
10
- accessKeyId,
11
- secretAccessKey
12
- }
13
- })
7
+ constructor (region = 'us-west-2') {
8
+ this.s3 = new S3({ region });
14
9
  }
15
10
 
16
- async uploadJson(path: string, bucket: string, jsonData: any){
11
+ async uploadJson(path: string, bucket: string, jsonData: any, ACL: ObjectCannedACL | string = ObjectCannedACL.private) {
17
12
  try {
18
13
  const res = await this.s3.putObject({
14
+ ACL,
19
15
  Bucket: bucket,
20
16
  ContentType: 'application/json; charset=utf-8',
21
17
  Body: JSON.stringify(jsonData),