I can't seem to get the MSAL library to import properly into my typescript code. I'm using the MSAL for JS library (which is supposed to have typings) in a simple typescript/react project scaffolded using the create-react-app with react-typescript scripts. I'm new to typescript and not sure if I'm missing something obvious or if there is a problem with the MSAL package when using it with typescript projects.
npm install --save msal
.import {Msal} from 'msal';
Could not find a declaration file for module 'msal'. '<path>/node_modules/msal/out/msal.js' implicitly has an 'any' type.
npm install --save-dev @types/msal
, but it doesn't exist.let Msal = require('Msal');
, but get an error that the Msal.UserAgentApplication isn't a constructor.import { observable, action, computed } from 'mobx'; import * as Msal from 'msal'; // <-- This line gives the error class ExampleMsal{ @observable private _isLoggedIn: boolean; constructor() { this._isLoggedIn = false; } @computed get isLoggedIn(): boolean { return this._isLoggedIn; } @action signIn() { let userAgentApplication = new Msal.UserAgentApplication('<client-id>', null, function (errorDes: string, token: string, error: string, tokenType: string) { // this callback is called after loginRedirect OR acquireTokenRedirect // (not used for loginPopup/aquireTokenPopup) } ); userAgentApplication.loginPopup(['user.read']).then(function(token: string) { let user = userAgentApplication.getUser(); if (user) { // signin successful alert('success'); } else { // signin failure alert('fail'); } }, function (error: string) { // handle error alert('Error' + error); }); this._isLoggedIn = true; } @action signOut() { this._isLoggedIn = false; } } export default ExampleMsal;
{ "compilerOptions": { "outDir": "build/dist", "module": "commonjs", "target": "es5", "lib": ["es6", "dom"], "sourceMap": true, "allowJs": true, "jsx": "react", "moduleResolution": "node", "rootDir": "src", "forceConsistentCasingInFileNames": true, "noImplicitReturns": true, "noImplicitThis": true, "noImplicitAny": true, "strictNullChecks": true, "suppressImplicitAnyIndexErrors": true, "noUnusedLocals": true, "experimentalDecorators": true }, "exclude": [ "node_modules", "build", "scripts", "acceptance-tests", "webpack", "jest", "src/setupTests.ts" ], "types": [ "typePatches" ] }
The Microsoft Authentication Library (MSAL) enables developers to acquire tokens from the Microsoft identity platform in order to authenticate users and access secured web APIs. It can be used to provide secure access to Microsoft Graph, other Microsoft APIs, third-party web APIs, or your own web API.
Acquiring an access token When using @azure/msal-react and @azure/msal-browser you will call acquireTokenSilent on the PublicClientApplication instance. If you need to obtain an access token inside a component or hook that lives under MsalProvider you can use the useMsal hook to get the objects you need.
The MSAL library for JavaScript enables client-side JavaScript web applications, running in a web browser, to authenticate users using Azure AD work and school accounts (AAD), Microsoft personal accounts (MSA) and social identity providers like Facebook, Google, LinkedIn, Microsoft accounts, etc.
It looks like the latest version of MSAL.js does have a CommonJS export. You can now just do the following in TypeScript (tested with version 2.3.3 of TypeScript and 0.1.3 of MSAL.js):
import * as Msal from 'msal';
Now in your .ts
(or in my case .tsx
file) you can, for instance, setup a click event handler and create a UserAgentApplication object:
// In you class somewhere private userAgentApplication: any = undefined; // The login button click handler handleLoginClick = (event: any): void => { if (!this.userAgentApplication) { this.userAgentApplication = new Msal.UserAgentApplication( 'clientID string', 'authority string or empty', this.authCallback, { cacheLocation: 'localStorage'}); } // Other login stuff... } // In React render() public render() { return ( <Button bsStyle="warning" type="button" onClick={(e) => this.handleLoginClick(e)} > Log in </Button> ); }
As you have correctly mentioned - in the msal.d.ts there are no exports - its not a module, and therefore you should not try importing.
Instead you can use it like this:
/// <reference path="./node_modules/msal/out/msal.d.ts" /> const userAgentApplication = new Msal.UserAgentApplication("your_client_id", null, (errorDes, token, error, tokenType) => { });
Note that even in readme they specify only one way of using their library - by including script tag, not by importing module. And further looking into their source code shows they are not using modules as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With