Last active
May 16, 2022 02:44
-
-
Save kelkes/30eec9bf98d03235d38f327b6b66518f to your computer and use it in GitHub Desktop.
hapijs/nes cookie based auth demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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