@whippsp/auth0-helper 1.1.0 → 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 CHANGED
@@ -1,33 +1,30 @@
1
1
  // index.d.ts
2
-
3
- /**
4
- * Configuration for the Auth0 Helper
5
- */
6
2
  export interface HelperConfig {
7
3
  ActionName?: string;
8
4
  debug?: boolean;
9
5
  }
10
6
 
11
- /**
12
- * Extend the global namespace to add our custom methods to the Auth0 objects
13
- */
14
7
  declare global {
15
- // We augment the 'api' object used in Post-Login Actions
8
+ // This covers Post-Login
16
9
  interface PostLoginAPI {
17
- /** * Sets a metadata flag that tells all subsequent Actions in this flow to exit immediately.
18
- */
19
10
  skipRemaining(): void;
11
+ setHelperConfig(config: HelperConfig): void;
12
+ user: {
13
+ hasRole(requestedRole: string): boolean;
14
+ };
15
+ }
20
16
 
21
- /** * Updates the helper's configuration (like debug mode or ActionName) at runtime.
22
- */
17
+ // This covers Post-User Registration
18
+ interface onContinuePostLogin {
19
+ skipRemaining(): void;
23
20
  setHelperConfig(config: HelperConfig): void;
21
+ user: {
22
+ hasRole(requestedRole: string): boolean;
23
+ };
24
24
  }
25
+
26
+ // You can add others like PreUserRegistrationAPI, etc.
25
27
  }
26
28
 
27
- /**
28
- * The initialization function exported by the library
29
- */
30
29
  declare function init(userModule: any, initialConfig?: HelperConfig): void;
31
-
32
- export default init;
33
30
  export = init;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whippsp/auth0-helper",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A helper to override Auth0 Actions handlers with skip logic.",
5
5
  "main": "Auth0Helper.js",
6
6
  "types": "index.d.ts",