triangle-utils 1.4.24 → 1.4.25

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,6 +1,7 @@
1
1
  export declare class UtilsCognito {
2
2
  private readonly cognito;
3
3
  constructor(region: string);
4
+ get_username(token: string): Promise<string | undefined>;
4
5
  admin_get_user(user_pool_id: string, username: string): Promise<import("@aws-sdk/client-cognito-identity-provider").AdminGetUserCommandOutput | undefined>;
5
6
  admin_create_user(user_pool_id: string, username: string, user_id: string): Promise<Error | import("@aws-sdk/client-cognito-identity-provider").AdminCreateUserCommandOutput>;
6
7
  admin_reset_password(user_pool_id: string, username: string): Promise<Error | import("@aws-sdk/client-cognito-identity-provider").AdminCreateUserCommandOutput>;
@@ -4,6 +4,18 @@ export class UtilsCognito {
4
4
  constructor(region) {
5
5
  this.cognito = new CognitoIdentityProvider({ region: region });
6
6
  }
7
+ async get_username(token) {
8
+ try {
9
+ const user_info = await this.cognito.getUser({
10
+ AccessToken: token
11
+ });
12
+ const username = user_info.Username;
13
+ return username;
14
+ }
15
+ catch (error) {
16
+ return undefined;
17
+ }
18
+ }
7
19
  async admin_get_user(user_pool_id, username) {
8
20
  try {
9
21
  return await this.cognito.adminGetUser({
@@ -62,6 +74,19 @@ export class UtilsCognito {
62
74
  return undefined;
63
75
  }
64
76
  const user_attribute = user_attributes.filter(attribute => attribute.Name === "preferred_username")[0];
77
+ if (user_attribute === undefined) {
78
+ const user_id = crypto.randomUUID();
79
+ await this.cognito.updateUserAttributes({
80
+ AccessToken: token,
81
+ UserAttributes: [
82
+ {
83
+ Name: "preferred_username",
84
+ Value: user_id
85
+ }
86
+ ]
87
+ });
88
+ return user_id;
89
+ }
65
90
  const user_id = user_attribute.Value;
66
91
  return user_id;
67
92
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.24",
3
+ "version": "1.4.25",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
@@ -8,6 +8,18 @@ export class UtilsCognito {
8
8
  this.cognito = new CognitoIdentityProvider({ region : region })
9
9
  }
10
10
 
11
+ async get_username(token : string) {
12
+ try {
13
+ const user_info = await this.cognito.getUser({
14
+ AccessToken : token
15
+ })
16
+ const username = user_info.Username
17
+ return username
18
+ } catch (error) {
19
+ return undefined
20
+ }
21
+ }
22
+
11
23
  async admin_get_user(user_pool_id : string, username : string) {
12
24
  try {
13
25
  return await this.cognito.adminGetUser({
@@ -66,6 +78,19 @@ export class UtilsCognito {
66
78
  return undefined
67
79
  }
68
80
  const user_attribute = user_attributes.filter(attribute => attribute.Name === "preferred_username")[0]
81
+ if (user_attribute === undefined) {
82
+ const user_id = crypto.randomUUID()
83
+ await this.cognito.updateUserAttributes({
84
+ AccessToken : token,
85
+ UserAttributes : [
86
+ {
87
+ Name : "preferred_username",
88
+ Value : user_id
89
+ }
90
+ ]
91
+ })
92
+ return user_id
93
+ }
69
94
  const user_id = user_attribute.Value
70
95
  return user_id
71
96
  } catch (error) {