diff --git a/pages/authorize-contribution.vue b/pages/authorize-contribution.vue index 2ca93fa..2f2fb17 100644 --- a/pages/authorize-contribution.vue +++ b/pages/authorize-contribution.vue @@ -66,8 +66,7 @@ export default { if (!this.token) { window.location.replace(window.location.origin) } else { - // TODO try to load contribution data. May be it's already authorized - // also set `contributionIndex` + await this.check() } setTimeout(() => { this.$root.$emit('disableLoading') @@ -103,6 +102,33 @@ export default { this.status.msg = 'Something went wrong. Please contact support' this.status.type = 'is-danger' } + }, + async check() { + const body = { + token: this.token + } + try { + const response = await fetch('/api/check_contribution', { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(body) + }) + if (response.ok) { + const { id } = await response.json() + this.contributionIndex = id + } else { + const error = await response.text() + this.status.msg = error + this.status.type = 'is-danger' + this.hideSaveBtn = true + } + } catch (e) { + this.status.msg = 'Something went wrong. Please contact support' + this.status.type = 'is-danger' + } } } } diff --git a/server/controllers/contribute.js b/server/controllers/contribute.js index edbab53..ac2f66d 100644 --- a/server/controllers/contribute.js +++ b/server/controllers/contribute.js @@ -146,4 +146,23 @@ router.post('/authorize_contribution', async (req, res) => { res.send('OK') }) +router.post('/check_contribution', async (req, res) => { + if (!req.body || !req.body.token) { + res.status(404).send('Wrong request params') + } + + const contribution = await Contribution.findOne({ where: { token: req.body.token } }) + if (!contribution) { + res.status(404).send('There is no such contribution') + return + } + + if (contribution.dataValues.socialType !== 'anonymous') { + res.status(404).send('The contribution is already authorized') + return + } + + return res.json({ id: contribution.dataValues.id }).send() +}) + module.exports = router