Last active
November 26, 2016 22:06
-
-
Save arcseldon/5d53f9277093815fceae0a74ebceb405 to your computer and use it in GitHub Desktop.
auth0 get users endpoint invocation server side (trusted server)
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
function getManagementApiToken() { | |
const cachedToken = cache.get('managementApi'); | |
if (cachedToken) { | |
return Promise.resolve(cachedToken); | |
} else { | |
return request.post({ | |
url: `https://${process.env.AUTH0_DOMAIN}/oauth/token`, | |
body: { | |
client_id: process.env.CLIENT_ID, | |
client_secret: process.env.CLIENT_SECRET, | |
audience: `https://${process.env.AUTH0_DOMAIN}/api/v2/`, | |
grant_type: 'client_credentials' | |
}, | |
json: true | |
}).then(apiResponse => { | |
// Tokens should be cached based on the expires_in response from Auth0, | |
// but this is not currently implemented for client credentials. Instead, | |
// we hardcode the expiration time configured in the dashboard, because | |
// we cannot depend on the contents of this token for anything. | |
cache.put('managementApi', apiResponse.access_token, 86400 * 1000); | |
return apiResponse.access_token; | |
}); | |
} | |
} | |
// Given a user ID, fetches the full user profile from the Auth0 Management API. | |
// This request is authenticated with client credentials, and tokens are stored | |
// in an in-memory cache. | |
function getUser(id) { | |
return getManagementApiToken().then(token => { | |
return request.get({ | |
url: `https://${process.env.AUTH0_DOMAIN}/api/v2/users/${id}`, | |
headers: { | |
Authorization: `Bearer ${token}` | |
}, | |
json: true | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment