ddraper

My First NPM Package

Blog Post created by ddraper on Dec 28, 2016
This is a personal blog post that is primarily intended for tracking my own learning rather than provided to the Alfresco Community for educational purposes. However if you find it useful, informative or have any comments on it then please comment below.

 

At the end of my last blog post I was looking at ways in which to write better components by abstracting as much non-browser specific code to utility modules. The idea of this was to improve testing so to minimize the amount of code that needed to be tested against multiple browsers. The side-effect of this approach means producing common capabilities that can be easily re-used against multiple web application frameworks such as Vue.js, React, Angular, etc.

 

As I developed my application I found myself pulling in more and more code and configuration that was already available in one of the many Vue.js CLI template projects. Most UI frameworks now come with a CLI that makes it easy to rapidly build out an application. I want to test out other frameworks so I thought it would make sense to abstract the core authentication and routing code into a separate project so that it could be imported into multiple different projects created using a CLI.

 

I followed some of the approach in this blog post and then mostly following these instructions on setting it up as an NPM package. Some useful information on testing the package prior to publication in described here. It was also necessary to follow this information in order to create a package with multiple exports.

 

The end result means that it is now possible to to install this new package with the following command:

npm install alfresco-auth-router --save-dev

 

Or if you prefer Yarn:

yarn add --dev alfresco-auth-router

 

Then it is just a simple case of setting up the Passport.js and Express routing using the following code in the server.js file:

passport.use(alfrescoAuthRouter.passportStrategy());
passport.serializeUser(alfrescoAuthRouter.serializeUser);
passport.deserializeUser(alfrescoAuthRouter.deserializeUser);
server.use('/auth', alfrescoAuthRouter.authRoutes(passport));
server.use('/proxy', alfrescoAuthRouter.apiRoutes());

 

This should make it much simpler to test out lots of different UI frameworks on Node.js against REST APIs provided by an Alfresco Repository. You can view this in my project by checking out this tag.

Outcomes