Created
October 2, 2024 16:43
-
-
Save Tanver-Hasan/7e4a8e1ad5fba6b0ed9cf806b37d3b32 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Post Password Change Trigger: https://auth0.com/docs/customize/actions/explore-triggers/password-reset-triggers/post-change-password-trigger
Dependency :
Add dependency in action : https://auth0.com/docs/customize/actions/manage-dependencies
Create an M2M client and authorize Management API with
delete:sessions
scopes : https://auth0.com/docs/get-started/auth0-overview/create-applications/machine-to-machine-appsSecret :
https://auth0.com/docs/customize/actions/write-your-first-action#add-a-secret