This is my first time using Trails and I decided to follow to the guides. However as I continued I wanted to test it out on my own ideas. One of my idea was to get social login with Instagram and get some images from Instagram based on tags.

I read about Trailpack and thought I could just use one to deal with this, but after realising that I had chosen hapi during the creation of the project, there aren’t many Trailpacks for that setup. I then did a search online and found that there is a plugin for hapi called bell which allows for social authentication. After finding this, I thought the process should be straight forward to getting bell working, but due to my lack of experience with both Trails and hapi this didn’t seem so easy for me after all.

I then started looking at the trailpack-hapi github repository and found how to load a plugin and followed this with bell. I also got information on doing social signup with twitter using hapi which had some code in it. Here is the code below that I used:

module.exports = {
  ...
  plugins: [
    {
      register: require('bell'),
      options: {}
    }
  ],

  onPluginsLoaded: function (err) {
    this.packs.hapi.server.auth.strategy('instagram', 'bell', {
      provider: 'instagram',
      password: 'secret_cookie_encryption_password',
      clientId: 'Instagram client id goes here',
      clientSecret: 'Instagram client secret goes here',
      isSecure: false // Should be set to true (which is the default) in production
    });
  }
}

Now I just need to add to my routes a config.auth property containing the auth strategy I have chosen.

module.exports = [
  ...
  {
    method: 'GET',
    path: '/auth/instagram',
    config: {
      auth: 'instagram'
    },
    handler: 'InstagramAuthController.auth'
  }
]

This is all you need to get going, but however do note that we are not persisting the auth data anywhere, so we would need to deal with handling that for our application somewhere in our code.