Created
June 12, 2023 13:06
-
-
Save unaibamir/fd3fd67197d5143bb1322fb77109965b to your computer and use it in GitHub Desktop.
Auth0 login script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function lookupEmailFromWP(wpUrl, nationalRegisterId) { | |
const axios = require('axios'); | |
const url = `${wpUrl}/wp-json/avia/v1/user/ssn?ssn=${nationalRegisterId}`; | |
const response = await axios.get(url); | |
const data = response.data; | |
if (Array.isArray(data) && data.length === 1) return data[0].user_email; | |
else return null | |
} | |
/** | |
* Handler that will be called during the execution of a PostLogin flow. | |
* | |
* @param {Event} event - Details about the user and the context in which they are logging in. | |
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login. | |
*/ | |
exports.onExecutePostLogin = async (event, api) => { | |
if (event.authorization) { | |
const wpUserRequired = event.client.metadata["wp-user-required-for-audkenni-eid"] | |
const connection = event.connection.name | |
let email = event.user.app_metadata.wp_email | |
if (wpUserRequired === "true" && connection === 'Rafraen-skilriki' && !email) { | |
const nationalRegisterId = event.user.app_metadata.nationalRegisterId; | |
if (!nationalRegisterId) throw Error('Exists in LMS: nationalRegisterId is undefined'); | |
const wpUrl = event.client.metadata["wp-url"]; | |
if (!wpUrl) throw Error('WP url missing in app configuration'); | |
email = await lookupEmailFromWP(wpUrl, nationalRegisterId); | |
if (!email) throw Error('unknown user'); | |
// update email for user | |
api.user.setAppMetadata("wp_email", email) | |
} | |
event.user.email = email | |
event.user.email_verified = true | |
} | |
}; | |
/** | |
* Handler that will be invoked when this action is resuming after an external redirect. If your | |
* onExecutePostLogin function does not perform a redirect, this function can be safely ignored. | |
* | |
* @param {Event} event - Details about the user and the context in which they are logging in. | |
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login. | |
*/ | |
// exports.onContinuePostLogin = async (event, api) => { | |
// }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment