Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kelkes
Last active May 16, 2022 02:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelkes/30eec9bf98d03235d38f327b6b66518f to your computer and use it in GitHub Desktop.
Save kelkes/30eec9bf98d03235d38f327b6b66518f to your computer and use it in GitHub Desktop.
hapijs/nes cookie based auth demo
import debug from 'debug';
import Nes from 'nes/client';
const client = new Nes.Client('ws://localhost:3000') // replace with your url here
// we use isomorphic-fetch
// optain a ws related cookie via this get request (using the default auth cookie)
fetch('/nes/auth', {
credentials: 'same-origin',
})
.then(() => {
return new Promise((resolve, reject) => {
client.connect({ delay: 2000, retries: 3 }, err => {
if (err) {
reject(err);
} else {
debug('Connected to Socket.');
resolve();
}
});
});
});
// somewhere in your client app setup code
return client.subscribe('/some-channel', data => {
debug(data);
});
// add this to your hapi server setup
hapi.register({
register: Nes,
options: {
auth: {
type: 'cookie',
cookie: 'ws-auth',
password: 'thisissparta',
ttl: 24 * 60 * 60 * 1000,
isSecure: process.env.NODE_ENV !== 'development',
isHttpOnly: false,
route: 'default', // our default auth (cookie) strategy is named 'default'
},
},
});
hapi.subscription('/some-channel');
// inside route
req.server.publish('/some-channel', { message: 'hello' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment