Skip to content

Instantly share code, notes, and snippets.

@frdnrdb
Last active April 8, 2021 06:15
Show Gist options
  • Save frdnrdb/afc6aa6ddc154d4c157f5f3cc176553f to your computer and use it in GitHub Desktop.
Save frdnrdb/afc6aa6ddc154d4c157f5f3cc176553f to your computer and use it in GitHub Desktop.
import { UserAgentApplication } from 'msal';
const app = new UserAgentApplication({
auth: {
clientId: process.env.AZURE_CLIENT_ID,
authority: `https://login.microsoftonline.com/${process.env.AZURE_TENANT_ID}`
},
cache: {
cacheLocation: 'localStorage'
}
});
app.handleRedirectCallback((err, payload) => {
if (err) return console.log('error', err.message);
return payload;
});
const requestObject = {
scopes: [ 'User.Read' ]
};
export default {
async signIn() {
return await app.loginRedirect(requestObject);
},
async signOut() {
return await app.logout();
},
async getUser() {
return await app.getAccount();
},
async getToken() {
try {
return await app.acquireTokenSilent(requestObject);
}
catch(err) {
return await app.acquireTokenRedirect(requestObject)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment