@das3mical/adonis-mercure
Version:
Mercure Hub integration for AdonisJS v6 — publish real-time updates via Server-Sent Events (SSE)
25 lines (24 loc) • 758 B
JavaScript
import jws from 'jws';
export class TokenGenerator {
#config;
constructor(config) {
this.#config = config;
}
generate(payload) {
return new Promise((resolve, reject) => {
jws
.createSign({
payload: { mercure: payload },
secret: this.#config.jwt.secret,
header: { alg: this.#config.jwt.alg },
})
.on('error', reject)
.on('done', resolve);
});
}
// WARNING: never pass user-controlled topics directly — a wildcard topic ('*')
// would grant the token holder access to all private events.
generateSubscribeToken(topics) {
return this.generate({ subscribe: topics });
}
}