# Store referral code for later during signup (for later retreival)?

**URL:** <https://community.auth0.com/t/store-referral-code-for-later-during-signup-for-later-retreival/121592>\
**Category:** Get Help\
**Tags:** new-universal-login-experience, login-experience\
**Created:** [November 24, 2023, 2:00pm UTC](https://community.auth0.com/t/store-referral-code-for-later-during-signup-for-later-retreival/121592 "2023-11-24T14:00:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shea](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/shea/32/8903_2.png) [@shea](https://community.auth0.com/u/shea)\
**Post date:** [November 24, 2023, 2:00pm UTC](https://community.auth0.com/t/store-referral-code-for-later-during-signup-for-later-retreival/121592/1 "2023-11-24T14:00:57Z")

</div>

I want to store a referral field when a user signs up.

- When we send a referral email to a user, it contains a link directs them to our univeral login signup page.
- After a user signs up, the are directed to a page that tells them to check their inbox and verify their email.
- Upon verifying their email, they are redirected to our account setup page, which contains the referral code field.
- I would like to pre-populate this referral code.
- I tried putting the code as a query param for the redirect\_uri. But this doesn’t work because at the time of the redirect, the user has not verified their email yet.

I am directing the users to:  
`https://my-tenant.auth0.com/authorize?response_type=code&screen_hint=signup&client_id=my-client-id&redirect_uri=https://myapp.com/verify-email-page&referralCode=rf-123-456-789`

In the `onExecutePreUserRegistration ` event, I could store the code in the meta data. But I am not sure where to grab the code from. The following doesn’t seem to work, as referralCode seems to be undefined.

```typescript
exports.onExecutePreUserRegistration = async (event, api) => {
  const referralCode = event.request['referralCode'];
  if(referralCode) {
    api.user.setAppMetadata('referral_code', referralCode);
  }
};

```

**EDIT** :  
I don’t seem to be able to make this work using my custom query parameter. But if I _hijack_ the `state` variable, I can pull the code from there. Is that bad?

In otherwords, I redirect the user to `https://my-tenant.auth0.com/authorize?response_type=code&screen_hint=signup&client_id=my-client-id&redirect_uri=https://myapp.com/verify-email-page&state=rf-123-456-789`.

```typescript
exports.onExecutePreUserRegistration = async (event, api) => {
  const referralCode = event.transaction?.state;
  console.log(`referral_code='${referralCode}'`);
  if(referralCode) {
    api.user.setAppMetadata('referral_code', referralCode);
  }
};

```

**EDIT2** :  
The above only seems to work on the test run when editing the application. When I do an actual login, for some reason `event.transaction?.state` is undefined. Even though in the logs it shows that the state does exist:

```json
{
    "actions": {
      "executions": [
        "jqj1Xa_7TK-VOYv8idJ722jIwMjMxMTI0"
      ]
    },
    "body": {
      "connection": "Username-Password-Authentication",
      "client_id": "asdfasdfasdfasdfas",
      "email": "someguy@domain.com",
      "password": " *****",
      "is_signup": true,
      "tenant": "test-tenant",
      "transaction": {
        "id": "dZYS-jK0ldnD5WS7Yk3kxM8TmgxyVd-PC",
        "locale": "en",
        "protocol": "oidc-basic-profile",
        "requested_scopes": [],
        "acr_values": [],
        "ui_locales": [],
        "redirect_uri": "https://my-application.com/new-account",
        "prompt": [],
        "state": "test-referral-code-8888",
        "login_hint": null,
        "response_mode": null,
        "response_type": [
          "code"
        ]
      },
      "form": {},
      "request_language": "en-US,en;q=0.9"
    },
    "authentication_methods": [
      "pwd"
    ]
  }

```

Why is the state undefined in my event handler?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [November 29, 2023, 8:23pm UTC](https://community.auth0.com/t/store-referral-code-for-later-during-signup-for-later-retreival/121592/3 "2023-11-29T20:23:44Z")

</div>

Hi @shea,

It’s not possible to send a param with the request to signup and retrieve it in a pre reg action. [More info here](https://community.auth0.com/t/passing-custom-parameters-to-pre-registration-action-from-new-universal-login/71594/8). Even more info [here](https://community.auth0.com/t/pre-registration-action-with-event-request-query/114549).

Hijacking the state could be a security risk. I can’t suggest that strategy either.

This may be something you want to handle in the background before sending the user to the registration page.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [December 13, 2023, 8:24pm UTC](https://community.auth0.com/t/store-referral-code-for-later-during-signup-for-later-retreival/121592/4 "2023-12-13T20:24:03Z")

</div>

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