Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple strategies with one scheme - 'hapi-auth-jwt2' in Hapi.js #73

Open
heron2014 opened this issue May 31, 2016 · 0 comments
Open

Comments

@heron2014
Copy link
Member

wip

If you want to register two different strategies depends on the routes, based on hapi-auth-jwt2scheme, follow these steps:

  • using plugins:
    Create your plugins for 'some-strategy':
'use strict';

exports.register = function (server, options, next) {

  server.auth.strategy('some-name', 'jwt', false,
  {
    key: process.env.JWT_SECRET_CLIENT,
    validateFunc: customValidate,
    verifyOptions: { ignoreExpiration: true }, cookieKey: 'name-of-your-token'
  });

  return next();
}

exports.register.attributes = {
  name: 'SomeAuthentication'
};

On your handler create/save token and pass it in a cookie

....
//check your password, compare, create session and store in db, create token as JWT 
...

return redirect('/dashboard').state('name-of-your-token', token);

other plugin for different strategy

var validate = require('./validate');

exports.register = function (server, options, next) {

  server.auth.strategy('jwt', 'jwt', false,
  { key: process.env.JWT_SECRET,
    validateFunc: validate,
    verifyOptions: { ignoreExpiration: true }
  });

  return next();
}

exports.register.attributes = {
  name: 'Authentication'
};

In a handler:

On your handler create/save token and pass it in a cookie for example. 
```js

....
//check your password, compare, create session and store in db, create token as JWT 
...

return redirect('/dashboard').state('token', token);

We dont need to specify cookieKey in above because as default is looking for 'token' - check L10

-register these plugins it to your server/index
-defined your customValidate function

We had an issue of not calling the validate function on one of the strategies (on some routes). Our token on that route was undefined. We resolved it by passing cookieKey with the correct name of the token.

@heron2014 heron2014 changed the title Multiple strategies with one scheme - 'jwt' in Hapi.js Multiple strategies with one scheme - 'hapi-auth-jwt2' in Hapi.js May 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant