Modified version of Microsoft Authentication Library Preview (MSAL) for JS supporting multiple target
npm install msalx
Multitarget version of Microsoft Authentication Library Preview for JavaScript
=========================================================
Msal.js is in preview and does not support CommonJS or ES6 targets. This is a temporary solution to be used in react projects until the original repository supports CommonJS.
| Getting Started| Docs | API Reference | Support | Samples
| --- | --- | --- | --- | --- |
The MSAL library preview for JavaScript enables your app to authorize enterprise users using Microsoft Azure Active Directory (AAD), Microsoft account users (MSA), users using social identity providers like Facebook, Google, LinkedIn etc. and get access to Microsoft Cloud OR Microsoft Graph.
The identity management services that the library interacts with are Microsoft Azure Active Directory, Microsoft Azure B2C and Microsoft Accounts.


npm install msalx
TypeScript
import * as React from 'react';
import { render } from 'react-dom';
import { UserAgentApplication } from 'msalx';
var applicationConfig = {
clientID: 'e760cab2-b9a1-4c0d-86fb-ff7084abd902',
authority: "https://login.microsoftonline.com/tfp/fabrikamb2c.onmicrosoft.com/b2c_1_susi",
b2cScopes: ["https://fabrikamb2c.onmicrosoft.com/demoapi/demo.read"],
webApi: 'https://fabrikamb2chello.azurewebsites.net/hello',
};
class App extends React.Component<{}, { isLoggedIn: boolean, message: string }> {
clientApplication = new UserAgentApplication(applicationConfig.clientID, applicationConfig.authority,
(errorDesc, token, error, tokenType) => {
// Called after loginRedirect or acquireTokenPopup
if (tokenType == "id_token") {
var userName = this.clientApplication.getUser().name;
this.setState({ isLoggedIn: true });
this.logMessage("User '" + userName + "' logged-in");
} else {
this.logMessage("Error during login:\n" + error);
}
});
state = {
isLoggedIn: false,
message: ""
}
logMessage(message: string) {
this.setState({ message: this.state.message + "\n" + message });
}
loginRedirect() {
this.clientApplication.loginRedirect(applicationConfig.b2cScopes);
}
logout() {
this.clientApplication.logout();
}
loginPopup() {
this.clientApplication.loginPopup(applicationConfig.b2cScopes).then((idToken) => {
this.clientApplication.acquireTokenSilent(applicationConfig.b2cScopes).then((accessToken) => {
var userName = this.clientApplication.getUser().name;
this.setState({ isLoggedIn: true });
this.logMessage("User '" + userName + "' logged-in");
}, (error) => {
this.clientApplication.acquireTokenPopup(applicationConfig.b2cScopes).then((accessToken) => {
var userName = this.clientApplication.getUser().name;
this.setState({ isLoggedIn: true });
this.logMessage("User '" + userName + "' logged-in");
}, (error) => {
this.logMessage("Error acquiring the popup:\n" + error);
});
})
}, (error) => {
this.logMessage("Error during login:\n" + error);
});
}
callApi() {
this.clientApplication.acquireTokenSilent(applicationConfig.b2cScopes).then((accessToken) => {
this.callApiWithAccessToken(accessToken);
}, (error) => {
this.clientApplication.acquireTokenPopup(applicationConfig.b2cScopes).then((accessToken) => {
this.callApiWithAccessToken(accessToken);
}, (error) => {
this.logMessage("Error acquiring the access token to call the Web api:\n" + error);
});
})
}
callApiWithAccessToken(accessToken: string) {
// Call the Web API with the AccessToken
fetch(applicationConfig.webApi, {
method: "GET",
headers: { 'Authorization': 'Bearer ' + accessToken }
}).then(response => {
response.text().then(text => this.logMessage("Web APi returned:\n" + JSON.stringify(text)));
}).catch(result => {
this.logMessage("Error calling the Web api:\n" + result);
});
}
render() {
return (
Hello World!
{this.state.message}
);
}
}
render( , document.getElementById('root') as HTMLElement);
``