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.
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
.
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:
{
"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?