use-promise-2
This commit is contained in:
parent
f599f49949
commit
a8358375e4
98
index.mjs
98
index.mjs
|
@ -1,16 +1,13 @@
|
||||||
import { Authorizer } from '@authorizerdev/authorizer-js';
|
import { Authorizer } from '@authorizerdev/authorizer-js';
|
||||||
import { Server } from '@hocuspocus/server'
|
import { Server } from '@hocuspocus/server';
|
||||||
|
|
||||||
|
const port = process.env.PORT || 4000;
|
||||||
const port = process.env.PORT || 4000
|
|
||||||
const authorizer = new Authorizer({
|
const authorizer = new Authorizer({
|
||||||
clientID: process.env.AUTHORIZER_CLIENT_ID || '',
|
clientID: process.env.AUTHORIZER_CLIENT_ID || '',
|
||||||
authorizerURL: process.env.AUTHORIZER_URL || 'https://auth.discours.io',
|
authorizerURL: process.env.AUTHORIZER_URL || 'https://auth.discours.io',
|
||||||
redirectURL: process.env.REDIRECT_URL || 'https://testing.discours.io',
|
redirectURL: process.env.REDIRECT_URL || 'https://testing.discours.io',
|
||||||
});
|
});
|
||||||
|
|
||||||
// console.info(authorizer)
|
|
||||||
|
|
||||||
const server = Server.configure({
|
const server = Server.configure({
|
||||||
port,
|
port,
|
||||||
onConnect({ connection }) {
|
onConnect({ connection }) {
|
||||||
|
@ -18,90 +15,105 @@ const server = Server.configure({
|
||||||
},
|
},
|
||||||
onAuthenticate(data) {
|
onAuthenticate(data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const headers = data.requestHeaders
|
const headers = data.requestHeaders;
|
||||||
if (!headers) {
|
if (!headers) {
|
||||||
console.error('Request headers not found');
|
console.error('Request headers not found');
|
||||||
return Promise.reject('required header is not present')
|
return reject(new Error('Required header is not present'));
|
||||||
}
|
}
|
||||||
const shout_id = parseInt(data.documentName.replace('shout-', ''), 10)
|
|
||||||
|
const shout_id = parseInt(data.documentName.replace('shout-', ''), 10);
|
||||||
console.debug(`shout_id extracted: ${shout_id}`);
|
console.debug(`shout_id extracted: ${shout_id}`);
|
||||||
|
|
||||||
const token = data.token || headers['authorization'] || ''
|
const token = data.token || headers['authorization'] || '';
|
||||||
|
|
||||||
const params = {
|
|
||||||
token_type: 'access_token',
|
|
||||||
token
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
console.error('Authorization token not found');
|
console.error('Authorization token not found');
|
||||||
return Promise.reject('token is not found')
|
return reject(new Error('Token is not found'));
|
||||||
}
|
}
|
||||||
|
|
||||||
authorizer.validateJWTToken(params)
|
authorizer.validateJWTToken({ token_type: 'access_token', token })
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (!response?.data?.is_valid) {
|
if (!response?.data?.is_valid) {
|
||||||
console.error('Invalid authorization token');
|
console.error('Invalid authorization token');
|
||||||
return Promise.reject('token is invalid')
|
return reject(new Error('Token is invalid'));
|
||||||
}
|
}
|
||||||
|
|
||||||
const { sub: user_id, allowed_roles: roles } = response.data.claims
|
const { sub: user_id, allowed_roles: roles } = response.data.claims;
|
||||||
console.debug(`user_id: ${user_id} roles: ${roles}`)
|
console.debug(`user_id: ${user_id} roles: ${roles}`);
|
||||||
|
|
||||||
if (roles.includes('editor')) {
|
if (roles.includes('editor')) {
|
||||||
return Promise.resolve({
|
return resolve({
|
||||||
id: user_id,
|
id: user_id,
|
||||||
roles: Array.isArray(roles) ? roles : roles.split(',')
|
roles: Array.isArray(roles) ? roles : roles.split(','),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
authorizer.getProfile(params).then((r) => {
|
fetch('https://core.discours.io/graphql', {
|
||||||
console.debug(r)
|
method: 'POST',
|
||||||
const { profile: author } = r.data.app_data
|
headers: { 'Content-Type': 'application/json' },
|
||||||
const author_id = author.get('id')
|
body: JSON.stringify({
|
||||||
if(author_id) {
|
query: `
|
||||||
const query = `
|
|
||||||
query {
|
query {
|
||||||
|
get_author(user_id: $user_id) {
|
||||||
|
id
|
||||||
|
slug
|
||||||
|
user
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
variables: { user_id },
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(data => {
|
||||||
|
const { id: author_id } = data.get_author
|
||||||
|
if (author_id) {
|
||||||
|
fetch('https://core.discours.io/graphql', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
query: `query {
|
||||||
get_shout(shout_id: $shout_id) {
|
get_shout(shout_id: $shout_id) {
|
||||||
id
|
id
|
||||||
slug
|
slug
|
||||||
authors
|
authors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`,
|
||||||
fetch('https://core.discours.io/graphql', {
|
variables: { shout_id },
|
||||||
method: 'POST',
|
}),
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ query, variables: { shout_id } }),
|
|
||||||
})
|
})
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
console.debug(data)
|
|
||||||
const { authors } = data.get_shout;
|
const { authors } = data.get_shout;
|
||||||
if (authors.includes(author_id)) {
|
if (authors.includes(author_id)) {
|
||||||
return {
|
return resolve({
|
||||||
id: user_id,
|
id: user_id,
|
||||||
author: author_id,
|
author: author_id,
|
||||||
roles: Array.isArray(roles) ? roles : roles.split(','),
|
roles: Array.isArray(roles) ? roles : roles.split(','),
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
return Promise.reject('not in authors list')
|
return reject(new Error('User is not in authors list'));
|
||||||
})
|
})
|
||||||
.catch(e => {
|
.catch(e => {
|
||||||
console.error('Error fetching shout data:', e.message);
|
console.error('Error fetching shout data:', e.message);
|
||||||
console.error(e.stack);
|
console.error(e.stack);
|
||||||
return Promise.reject('error fetching shout data')
|
return reject(new Error('Error fetching shout data'));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.catch(e => {
|
||||||
|
console.error('Error fetching author data:', e.message);
|
||||||
|
console.error(e.stack);
|
||||||
|
return reject(new Error('Error fetching author data'));
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch(e => {
|
.catch(e => {
|
||||||
console.error('Error validating authorization token:', e.message);
|
console.error('Error validating authorization token:', e.message);
|
||||||
console.error(e.stack);
|
console.error(e.stack);
|
||||||
return Promise.reject('token is invalid')
|
return reject(new Error('Error validating authorization token'));
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen().then(r => console.info('started'));
|
server.listen().then(r => console.info('Server started'));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user