This is a Hapi plugin wrapper around hapi-auth-jwt2 with a specific setup as used by GeenenTijd. Compatible with Glue.
npm i geenenauth --save
server.register({
register: require('geenenauth'),
options: {
secret: 'MyJWTSecret',
issuer: 'MyIssuer', // Default GeenenTijd
audience: 'MyAudience',
userRoles: [], // Default ['user', 'editor']
adminRoles: [], // Default ['admin', 'superadmin']
}
});
User Authentication:
server.route({
method: 'GET',
path: '/me',
config: {
auth: {
strategy: 'jwtUser'
}
},
handler: function (request, reply) {
}
});
Admin Authentication:
server.route({
method: 'GET',
path: '/me',
config: {
auth: {
strategy: 'jwtAdmin'
}
},
handler: function (request, reply) {
}
});
The plugin exposes an isAdmin method.
function (request, reply) {
if (request.server.methods.isAdmin(request.auth.credentials)) {
// User is admin
}
}
Options: jsonwebtoken
- expiresIn
- notBefore
- noTimestamp
Example of settings a token that never expires (not recommended):
function (request, reply) {
const user = {
userid: 'c0cb1883-e8c6-4efa-8561-4ad4f4c14518',
username: 'glenn',
role: 'user'
};
request.server.methods.getToken(user, {
expiresIn: undefined
} (err, token) => {
// We have token here
});
}