UNPKG

azure-ad-auth-kit

Version:

Microsoft Azure AD Auth Kit (OAuth2 login, no config)

39 lines (30 loc) 1.07 kB
const axios = require("axios"); async function getAccessTokenAndProfile(code, config) { const { TENANT_ID, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI } = config; const tokenRes = await axios.post( `https://login.microsoftonline.com/${TENANT_ID}/oauth2/v2.0/token`, new URLSearchParams({ grant_type: "authorization_code", code, client_id: CLIENT_ID, client_secret: CLIENT_SECRET, redirect_uri: REDIRECT_URI }), { headers: { "Content-Type": "application/x-www-form-urlencoded" } } ); const access_token = tokenRes.data.access_token; const userRes = await axios.get("https://graph.microsoft.com/v1.0/me", { headers: { Authorization: `Bearer ${access_token}` } }); return { userInfo: userRes.data, graphToken: access_token }; } async function getUserProfile(access_token) { const response = await axios.get("https://graph.microsoft.com/v1.0/me", { headers: { Authorization: `Bearer ${access_token}` } }); return response.data; } module.exports = { getAccessTokenAndProfile, getUserProfile };