How to send custom information to Rules during user registration on Auth0 SPA?

CONTEXT:
Using:

  • Auth0 SDK for SPA
  • Auth0 Rules where I process the new users.

I’m trying to send custom information from Auth0 SDK in React app during the registration.

loginWithRedirect (with its options) is called when the React app doesn’t have any user authenticated on Auth0 SDK. Then I use this method to sign up the user and I’m trying to send custom information to Auth0 Rules:


CODE:

loginWithRedirect({
  appState: { targetUrl: path },
  customOption: 'agent'
});

My rule:

async function myRule(user, context, cb) {
  console.log("Testing", {context})
}

PROBLEM:

In my Rule, I don’t receive the customOption in the context.


QUESTION:
How can I pass custom information from Auth0 SDK SPA to Rules?

SOLUTION:
My Reac app use this code to redirect the unregistered user for signing up with loginWithRedirect. I pass some options to this method:

  • appState (to redirect the user on the React app after the registration, it needs more code to make the redirection on the app; I recommend to check the SPA sample by Auth0).
  • customLogin: it’s my custom information the the Rule will get.

You can watch on the photo the options for loginWithRedirect (Authorization Server is in Auth0, and Rules are also in it). The key is to read that you can add your custom parameter:

My SPA code to sign up is:

await loginWithRedirect({
 appState: {
  // Used to redirect to an specific url after the signup.
  targetUrl: path
 },
 customLogin: 'agent'
});

Then, after the user has registered, the Rule is called.

async function myRule(user, context, cb) {
 ...
}

The Rule has a context object with requestquery object. The query contains your custom data (in my case is customLogin) that was passed before via loginWithRedirect.

3 Likes

Thanks a lot for sharing such a detailed solution with the rest of community!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.