@whippsp/auth0-helper 1.0.9 → 1.1.1

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.
package/Auth0Helper.js CHANGED
@@ -1,10 +1,21 @@
1
1
  // Auth0Helper.js
2
2
  module.exports = function(userModule, initialConfig = {}) {
3
- // We don't wait for a getter/setter.
4
- // We wrap the user's function immediately when they assign it.
5
- const originalHandler = userModule.exports.onExecutePostLogin;
3
+
4
+ // List of Auth0 Actions
5
+ const handlers = [
6
+ 'onExecutePostLogin',
7
+ 'onContinuePostLogin'
8
+ // Implement these later as transaction metadata is a login thing
9
+ // 'onExecutePostUserRegistration',
10
+ // 'onExecutePreUserRegistration',
11
+ // 'onExecutePostChangePassword',
12
+ // 'onExecuteCredentialsExchange'
13
+ ];
6
14
 
7
- Object.defineProperty(userModule.exports, 'onExecutePostLogin', {
15
+ handlers.forEach(handlerName => {
16
+ const originalHandler = userModule.exports[handlerName];
17
+
18
+ Object.defineProperty(userModule.exports, handlerName, {
8
19
  configurable: true,
9
20
  enumerable: true,
10
21
  get: function() {
@@ -15,15 +26,23 @@ module.exports = function(userModule, initialConfig = {}) {
15
26
  api.setHelperConfig = (newConfig) => {
16
27
  runtimeConfig = { ...runtimeConfig, ...newConfig };
17
28
  };
29
+
30
+ api.user.hasRole = (requestedRole) => {
31
+ const roles = event.authorization?.roles || [];
32
+ const hasIt = roles.includes(requestedRole);
33
+
34
+ log(`Checking role [${requestedRole}]: ${hasIt ? 'YES' : 'NO'}`);
35
+ return hasIt;
36
+ };
18
37
 
19
38
  const log = (message) => {
20
39
  if (runtimeConfig.debug === true) {
21
- console.log(`[${runtimeConfig.ActionName || "Auth0Action"}] ${message}`);
40
+ console.log(`[${runtimeConfig.ActionName || handlerName }] ${message}`);
22
41
  }
23
42
  };
24
43
 
25
44
  if (event.transaction?.metadata?.skipremainingactions === 'true') {
26
- log(`Auth0Helper: Skipping execution ${runtimeConfig.ActionName}.`);
45
+ log(`Skipping Action execution.`);
27
46
  return;
28
47
  }
29
48
 
@@ -42,4 +61,5 @@ module.exports = function(userModule, initialConfig = {}) {
42
61
  this._rawHandler = fn;
43
62
  }
44
63
  });
64
+ });
45
65
  };
package/index.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ // index.d.ts
2
+ export interface HelperConfig {
3
+ ActionName?: string;
4
+ debug?: boolean;
5
+ }
6
+
7
+ declare global {
8
+ // This covers Post-Login
9
+ interface PostLoginAPI {
10
+ skipRemaining(): void;
11
+ setHelperConfig(config: HelperConfig): void;
12
+ user: {
13
+ hasRole(requestedRole: string): boolean;
14
+ };
15
+ }
16
+
17
+ // This covers Post-User Registration
18
+ interface onContinuePostLogin {
19
+ skipRemaining(): void;
20
+ setHelperConfig(config: HelperConfig): void;
21
+ user: {
22
+ hasRole(requestedRole: string): boolean;
23
+ };
24
+ }
25
+
26
+ // You can add others like PreUserRegistrationAPI, etc.
27
+ }
28
+
29
+ declare function init(userModule: any, initialConfig?: HelperConfig): void;
30
+ export = init;
package/package.json CHANGED
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "name": "@whippsp/auth0-helper",
3
- "version": "1.0.9",
3
+ "version": "1.1.1",
4
4
  "description": "A helper to override Auth0 Actions handlers with skip logic.",
5
5
  "main": "Auth0Helper.js",
6
+ "types": "index.d.ts",
7
+ "files": [
8
+ "Auth0Helper.js",
9
+ "index.d.ts"
10
+ ],
6
11
  "publishConfig": {
7
12
  "access": "public"
8
13
  },