Skip to content

Commit 4732426

Browse files
committed
wikis accessToken made persistent
1 parent a18aa85 commit 4732426

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

.env.dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PORT=5000
22
NODE_ENV="development"
33
JWT_SECRET="thisismysupersecrettokenjustkidding"
4-
DATABASE_URL="mongodb://localhost:27017/sugarizerDb"
4+
DATABASE_URL="mongodb://localhost:27017/testing-1"
55
SENDGRID_API_KEY='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'
66
SOCKET_PORT=8810
77
clientbaseurl="http://localhost:3000"

app/controllers/wikis.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const axios = require('axios')
22
const HttpStatus = require('http-status-codes')
33
const WikiHelper = require('../utils/wikis-helper')
44
const HANDLER = require('../utils/response-helper')
5+
const Organization = require('../models/Organisation')
56
const { changeFileOnRemote, addPageToIndex, fetchPagesIndex, updatePagesIndex, getOpts, getOrgId } = WikiHelper
67

78
const clientId = process.env.GITHUB_OAUTH_APP_CLIENTID
@@ -14,6 +15,13 @@ module.exports = {
1415

1516
getWikis: async (req, res, next) => {
1617
try {
18+
if (!accessToken) {
19+
const Org = await Organization.find({}).lean().exec()
20+
console.log(Org[0])
21+
if (Org[0].wikis.accessToken) {
22+
accessToken = Org[0].wikis.accessToken
23+
}
24+
}
1725
if (!accessToken) {
1826
res.status(HttpStatus.OK).json({ wikis: 'NO_ACCESS_TOKEN' })
1927
} else {
@@ -95,7 +103,9 @@ module.exports = {
95103
redirect_url: `https://github.com/login/oauth/authorize?client_id=${clientId}&scope=repo`
96104
})
97105
} else {
98-
res.redirect(`${process.env.clientbaseurl}/wikis`)
106+
res.status(HttpStatus.OK).json({
107+
redirect: false
108+
})
99109
}
100110
},
101111

@@ -110,6 +120,9 @@ module.exports = {
110120
const resp = await axios.post('https://github.com/login/oauth/access_token', body, opts)
111121
accessToken = resp.data.access_token
112122
WikiHelper.setOpts(accessToken)
123+
const Org = await Organization.find({}).exec()
124+
Org[0].wikis.accessToken = accessToken
125+
await Org[0].save()
113126
await WikiHelper.getOrg()
114127
await WikiHelper.createRepo()
115128
await updatePagesIndex()

app/models/Organisation.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ const orgSchema = new Schema({
194194
type: Date,
195195
required: true,
196196
default: Date.now()
197+
},
198+
wikis: {
199+
accessToken: { type: String, default: null }
197200
}
198201
})
199202
module.exports = mongoose.model('Organization', orgSchema)

0 commit comments

Comments
 (0)