Skip to content

Instantly share code, notes, and snippets.

@Tanver-Hasan
Created October 2, 2024 16:43
Show Gist options
  • Save Tanver-Hasan/7e4a8e1ad5fba6b0ed9cf806b37d3b32 to your computer and use it in GitHub Desktop.
Save Tanver-Hasan/7e4a8e1ad5fba6b0ed9cf806b37d3b32 to your computer and use it in GitHub Desktop.
/**
* Handler that will be called during the execution of a PostChangePassword flow.
*
* @param {Event} event - Details about the user and the context in which the change password is happening.
* @param {PostChangePasswordAPI} api - Methods and utilities to help change the behavior after a user changes their password.
*/
exports.onExecutePostChangePassword = async (event, api) => {
const { AuthenticationClient } = require('auth0');
const axios = require('axios');
const { DOMAIN } = event.secrets || {};
let { value: token } = api.cache.get('management-api-token') || {};
if (!token) {
const { CLIENT_ID, CLIENT_SECRET } = event.secrets || {};
console.log(DOMAIN, CLIENT_ID, CLIENT_SECRET);
const cc = new AuthenticationClient({ domain: DOMAIN, clientId: CLIENT_ID, clientSecret: CLIENT_SECRET });
try {
const { data } = await cc.oauth.clientCredentialsGrant({ audience: `https://${DOMAIN}/api/v2/` });
token = data?.access_token;
if (!token) {
console.log('failed get api v2 cc token');
return;
}
console.log('cache MIS for m2m token');
const result = api.cache.set('silent-management-token', token, { ttl: data.expires_in * 1000 });
if (result?.type === 'error') {
console.log('failed to set the token in the cache with error code', result.code);
}
} catch (err) {
console.log('failed calling cc grant', err);
return;
}
}
let config = {
method: 'delete',
url: `https://${DOMAIN}/api/v2/users/${event.user.user_id}/sessions`,
headers: {
'Authorization': `Bearer ${token}`
}
};
axios.request(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});
return;
};
@Tanver-Hasan
Copy link
Author

Tanver-Hasan commented Oct 2, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment