diff --git a/server/src/routes/api/v1/sessions.js b/server/src/routes/api/v1/sessions.js index d502f2f51..6bf079d35 100644 --- a/server/src/routes/api/v1/sessions.js +++ b/server/src/routes/api/v1/sessions.js @@ -27,10 +27,12 @@ router.get('/hasValid/:id', async (req, res) => { req.headers['react-map-secret'] === api.reactMapSecret ) { const results = await Session.query().whereRaw( - `json_extract(data, '$.passport.user.id') = ${req.params.id}`, + `json_extract(data, '$.passport.user.id') = ${req.params.id} + OR json_extract(data, '$.passport.user.discordId') = "${req.params.id}" + OR json_extract(data, '$.passport.user.telegramId') = "${req.params.id}"`, ) res.status(200).json({ - valid: Boolean(results.length), + valid: !!results.length, length: results.length, }) console.log(`[API] api/v1/sessions/hasValid/${req.params.id}`) @@ -50,7 +52,11 @@ router.get('/clearSessions/:id', async (req, res) => { req.headers['react-map-secret'] === api.reactMapSecret ) { const results = await Session.query() - .whereRaw(`json_extract(data, '$.passport.user.id') = ${req.params.id}`) + .whereRaw( + `json_extract(data, '$.passport.user.id') = ${req.params.id} + OR json_extract(data, '$.passport.user.discordId') = "${req.params.id}" + OR json_extract(data, '$.passport.user.telegramId') = "${req.params.id}"`, + ) .delete() res.status(200).json({ results }) console.log(`[API] api/v1/sessions/clearSessions/${req.params.id}`) diff --git a/server/src/routes/api/v1/users.js b/server/src/routes/api/v1/users.js index a97db2ea3..8a8818215 100644 --- a/server/src/routes/api/v1/users.js +++ b/server/src/routes/api/v1/users.js @@ -16,7 +16,7 @@ router.get('/', async (req, res) => { console.log('[API] api/v1/users') } catch (e) { console.error('[API Error] api/v1/sessions', e) - res.status(500).json({ status: 'ServerError', reason: e.message }) + res.status(500).json({ status: 'error', reason: e.message }) } }) @@ -26,14 +26,57 @@ router.get('/:id', async (req, res) => { api.reactMapSecret && req.headers['react-map-secret'] === api.reactMapSecret ) { - res.status(200).json(await User.query().findById(req.params.id)) + const user = await User.query().findById(req.params.id) + res + .status(200) + .json(user || { status: 'error', reason: 'User Not Found' }) } else { throw new Error('Incorrect or missing API secret') } console.log(`[API] api/v1/users/${req.params.id}`) } catch (e) { console.error(`[API Error] api/v1/users/${req.params.id}`, e) - res.status(500).json({ status: 'ServerError', reason: e.message }) + res.status(500).json({ status: 'error', reason: e.message }) + } +}) + +router.get('/discord/:id', async (req, res) => { + try { + if ( + api.reactMapSecret && + req.headers['react-map-secret'] === api.reactMapSecret + ) { + const user = await User.query().where('discordId', req.params.id).first() + res + .status(200) + .json(user || { status: 'error', reason: 'User Not Found' }) + } else { + throw new Error('Incorrect or missing API secret') + } + console.log(`[API] api/v1/users/discord/${req.params.id}`) + } catch (e) { + console.error(`[API Error] api/v1/users/discord/${req.params.id}`, e) + res.status(500).json({ status: 'error', reason: e.message }) + } +}) + +router.get('/telegram/:id', async (req, res) => { + try { + if ( + api.reactMapSecret && + req.headers['react-map-secret'] === api.reactMapSecret + ) { + const user = await User.query().where('telegramId', req.params.id).first() + res + .status(200) + .json(user || { status: 'error', reason: 'User Not Found' }) + } else { + throw new Error('Incorrect or missing API secret') + } + console.log(`[API] api/v1/users/telegram/${req.params.id}`) + } catch (e) { + console.error(`[API Error] api/v1/users/telegram/${req.params.id}`, e) + res.status(500).json({ status: 'error', reason: e.message }) } })