From f599f49949f5fe9436603b4601bb0cda6a77a560 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 4 Mar 2024 19:25:01 +0300 Subject: [PATCH] use-promise --- index.mjs | 150 +++++++++++++++++++++++++++--------------------------- 1 file changed, 76 insertions(+), 74 deletions(-) diff --git a/index.mjs b/index.mjs index 01fa815..8428f79 100644 --- a/index.mjs +++ b/index.mjs @@ -17,89 +17,91 @@ const server = Server.configure({ connection.requiresAuthentication = true; }, onAuthenticate(data) { - // console.debug(data) - if (!data.requestHeaders) { - console.error('Request headers not found'); - return null; - } - const shout_id = parseInt(data.documentName.replace('shout-', ''), 10) - console.debug(`shout_id extracted: ${shout_id}`); + return new Promise((resolve, reject) => { + const headers = data.requestHeaders + if (!headers) { + console.error('Request headers not found'); + return Promise.reject('required header is not present') + } + const shout_id = parseInt(data.documentName.replace('shout-', ''), 10) + console.debug(`shout_id extracted: ${shout_id}`); - const token = data.token || data.requestHeaders['authorization'] || '' + const token = data.token || headers['authorization'] || '' - const params = { - token_type: 'access_token', - token - }; + const params = { + token_type: 'access_token', + token + }; - if (!token) { - console.error('Authorization token not found'); - return null; - } + if (!token) { + console.error('Authorization token not found'); + return Promise.reject('token is not found') + } - authorizer.validateJWTToken(params) - .then(response => { - if (!response?.data?.is_valid) { - console.error('Invalid authorization token'); - return null; - } - - const { sub: user_id, allowed_roles: roles } = response.data.claims - console.debug(`user_id: ${user_id} roles: ${roles}`) - - if (roles.includes('editor')) { - return { - id: user_id, - roles: Array.isArray(roles) ? roles : roles.split(',') + authorizer.validateJWTToken(params) + .then(response => { + if (!response?.data?.is_valid) { + console.error('Invalid authorization token'); + return Promise.reject('token is invalid') } - } - authorizer.getProfile(params).then((r) => { - console.debug(r) - const { profile: author } = r.data.app_data - const author_id = author.get('id') - if(author_id) { - const query = ` - query { - get_shout(shout_id: $shout_id) { - id - slug - authors - } - } - `; - fetch('https://core.discours.io/graphql', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ query, variables: { shout_id } }), + const { sub: user_id, allowed_roles: roles } = response.data.claims + console.debug(`user_id: ${user_id} roles: ${roles}`) + + if (roles.includes('editor')) { + return Promise.resolve({ + id: user_id, + roles: Array.isArray(roles) ? roles : roles.split(',') }) - .then(res => res.json()) - .then(data => { - console.debug(data) - const { authors } = data.get_shout; - if (authors.includes(author_id)) { - return { - id: user_id, - author: author_id, - roles: Array.isArray(roles) ? roles : roles.split(','), - }; - } - return null; - }) - .catch(e => { - console.error('Error fetching shout data:', e.message); - console.error(e.stack); - return null; - }); } + + authorizer.getProfile(params).then((r) => { + console.debug(r) + const { profile: author } = r.data.app_data + const author_id = author.get('id') + if(author_id) { + const query = ` + query { + get_shout(shout_id: $shout_id) { + id + slug + authors + } + } + `; + fetch('https://core.discours.io/graphql', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ query, variables: { shout_id } }), + }) + .then(res => res.json()) + .then(data => { + console.debug(data) + const { authors } = data.get_shout; + if (authors.includes(author_id)) { + return { + id: user_id, + author: author_id, + roles: Array.isArray(roles) ? roles : roles.split(','), + }; + } + return Promise.reject('not in authors list') + }) + .catch(e => { + console.error('Error fetching shout data:', e.message); + console.error(e.stack); + return Promise.reject('error fetching shout data') + }); + } + }) }) - }) - .catch(e => { - console.error('Error validating authorization token:', e.message); - console.error(e.stack); - return null; - }); - }, + .catch(e => { + console.error('Error validating authorization token:', e.message); + console.error(e.stack); + return Promise.reject('token is invalid') + }); + }) + } }); server.listen().then(r => console.info('started'));