https://github.com/mglagola/hapi-custom-auth
A hapi auth plugin
https://github.com/mglagola/hapi-custom-auth
authentication authorization hapi javascript nodejs npm
Last synced: 6 months ago
JSON representation
A hapi auth plugin
- Host: GitHub
- URL: https://github.com/mglagola/hapi-custom-auth
- Owner: mglagola
- Created: 2018-07-25T18:34:07.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-25T18:39:13.000Z (about 7 years ago)
- Last Synced: 2025-03-26T16:02:39.615Z (6 months ago)
- Topics: authentication, authorization, hapi, javascript, nodejs, npm
- Language: JavaScript
- Size: 49.8 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hapi Custom Auth
Easy way to create a hapi "custom" auth scheme. Looks for value associated with `request.headers.authorization`.
## Usage
##### Install
```
npm install --save hapi-custom-auth
```##### Configure Server:
```js
const HapiCustomAuth = require('hapi-custom-auth');
// ...const AuthSetup = {
// See below example
plugin: require('./auth-setup');
}// ...
const server = new Hapi.Server(...);
const plugins = [
// ...
HapiCustomAuth,
AuthSetup,
// ...,
];
await server.register(plugins);
// ...```
##### Auth Setup (ex: auth-setup.js)
```js
const validate = async (authorization) => {
try {
const user = await fakeSQLQuery(`user.token === ${authorization}`);
if (!user) return { isValid: false };
const credentials = {
userId: user.id,
scope: [
'user',
`user-${user.id}`,
],
};
return { isValid: true, credentials };
} catch (error) {
return { isValid: false };
}
};const isValidToken = (token) => token && token.indexOf('pizza') >= 0;
const errorFunc = ({ errorType, message, scheme }) => ({
errorType,
message,
scheme,
attributes: { error: 'Invalid token', invalid_token: true },
});exports.register = function (server, options) {
server.auth.strategy('custom-auth', 'custom-auth', config);
};exports.pkg = {
name: 'MyAuth',
version: '1.0.0',
};
```