Auth in Hapi with JWT

Marcos Bérgamo
3 min readDec 11, 2015
You can pass just for this road.

Brief

The new buzz word is JWT(JSON Web Token)[I know, is not so new now :( ]. Well, this cool approach for send and consume an information easily with an encode for "security".

So, Hapi have an awesome auth system where you can create a lot of auth types, for you use when is required for that case, for example, internally you can use a JWT and for external connection an Hawk, Basic Auth, or an OAuth auth. You can have one for case you need, but in this case we'll talk about JWT auth!

The Road

I really don't want to entry in the internals about the auth system in Hapi, but show how you can implementing Hapi + JWT for authenticate your users and distribute a token for retrieve information.

My favorite plugin for that is hapi-auth-jwt2. You maybe are asking: "Why jwt2?" this is simple because "hapi-auth-jwt" was taken :p

This is my implementation of hapi-auth-jwt2, this code consist in two "big" functions.

registerAuth is the registry function in Hapi, that configuring the plugin with…

--

--