Skip to content

Instantly share code, notes, and snippets.

@unaibamir
Created June 12, 2023 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unaibamir/fd3fd67197d5143bb1322fb77109965b to your computer and use it in GitHub Desktop.
Save unaibamir/fd3fd67197d5143bb1322fb77109965b to your computer and use it in GitHub Desktop.
Auth0 login script
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