pnp-auth adds additional authentication options for PnPjs library via implementing custom NodeFetchClient
!Important: as library implements NodeFetchClient and depends on node-sp-auth module, you can use pnp-authonly in nodejs environment
pnp-auth uses node-sp-auth as authentication library, thus making all authentication options from node-sp-auth available for pnp-auth.
Supported versions:
- SharePoint 2013 and onwards
- SharePoint Online
For full list of authentication options check out node-sp-auth readme.
If you need support for the previous version of PnPjs, simply install the version of pnp-auth, which supports PnPjs v1:
npm install pnp-auth@0.xInstall @pnp/sp libraries (they are listed as peer dependencies for pnp-auth, that's why you should install them separately).
We need more than just @pnp/sp because it depends on some other @pnp/ packages:
npm install @pnp/logging @pnp/common @pnp/odata @pnp/sp --savenpm install pnp-auth --saveBefore using PnPjs library, you should make it aware of your authentication data. That should be performed at the start of your application. The code is fairly simple:
import{bootstrap}from'pnp-auth';import{sp}from'@pnp/sp-commonjs';bootstrap(sp,authData,siteUrl);// That's it! Now you can use pnp-sp library:sp.web.get().then(...);OR with factory methods:
import{bootstrap}from'pnp-auth';import{sp,Web}from'@pnp/sp-commonjs';bootstrap(sp,authData);// That's it! Now you can use pnp-sp library:letweb=Web(siteUrl);web.get().then(...)sp- "sp" object obtained from@pnp/sp-commonjslibrary via import:import { sp } from '@pnp/sp-commonjs';authData- can be astring,AuthConfigobject or rawnode-sp-authcredentials:string- absolute or relative path to your file with authentication data. File should be generated usingnode-sp-auth-configCLI. When string is provided,pnp-authinternally createsAuthConfigwith below default parameters:
letauthConfig=newAuthConfig({configPath: <yourpathtofile>,encryptPassword: true,saveConfigOnDisk: true});
AuthConfig- you can provideAuthConfigdirectly. To learn more checkoutnode-sp-auth-configrepository- raw credentials - you can pass any credential options which are supported by
node-sp-auth. For more information checkoutnode-sp-authrepository as well as wiki
siteUrl- your SharePoint site url. You have two options when working with SharePoint data. When usingsiteUrlparameter, you can write a codesp.web.get()etc., in that case yoursp.webobject will be attached to yoursiteUrl. If you want to work with different webs, you can use factory method:Web(<url to SharePoint>)
Of course, you can do bootstrap manually, if you want. pnp-auth exports NodeFetchClient which you can use in pnp's setup method:
importNodeFetchClientfrom'pnp-auth';import{sp}from'@pnp/sp-commonjs';sp.setup({sp: {fetchClientFactory: ()=>{returnnewNodeFetchClient(authData,siteUrl);}}});npm installnpm run build- tslint & TS compile
Library has a few integration tests:
npm install- Rename
settings.sample.tstosettings.ts. UpdatewebTitleandsubsiteUrlto your real data. - Use
node-sp-auth-configto generate credentials inside./config/private.jsonfile. Site url in credentials should point to site withwebTitlefrom step#2. - Run
npm test

