crdt-server/index.mjs
Stepan Vladovskiy 4808d6d613
Some checks failed
CI/CD / test (push) Failing after 9s
CI/CD / deploy (push) Failing after 34s
feat: with sentry node in dependensies and sentry dsn in index
2024-04-07 15:14:38 -03:00

54 lines
1.5 KiB
JavaScript

import { Authorizer } from '@authorizerdev/authorizer-js';
import { Server } from '@hocuspocus/server';
const authorizer = new Authorizer({
clientID: process.env.AUTHORIZER_CLIENT_ID || '',
authorizerURL: 'https://auth.discours.io',
redirectURL: 'https://testing.discours.io',
});
const Sentry = require("@sentry/node");
Sentry.init({ dsn: 'https://c98cd247c6744938a4daf0b3118722a2@glitchtip.discours.io/4' });
const startServer = async () => {
const server = await Server.configure({
port: process.env.PORT || 4000,
async onConnect({ connection }) {
connection.requiresAuthentication = true;
},
async onAuthenticate(data) {
if (data.requestHeaders) {
const params = {
token_type: 'access_token',
token: data.requestHeaders['authorization'] || '',
};
if (params.token) {
const response = await authorizer.validateJWTToken(params);
if (response?.data?.is_valid) {
const { sub: user, allowed_roles: roles } = response.data.claims;
console.debug(`user_id: ${user} roles: ${roles}`);
return {
id: user,
roles,
};
}
console.error('no valid auth token presented');
throw new Error('Not authorized!');
}
}
},
}).listen();
server.listen();
};
process.on('unhandledRejection', (reason, promise) => {
Sentry.captureException(reason);
});
process.on('uncaughtException', (error) => {
Sentry.captureException(error);
});
startServer();