@@ -2,6 +2,7 @@ const axios = require('axios')
22const HttpStatus = require ( 'http-status-codes' )
33const WikiHelper = require ( '../utils/wikis-helper' )
44const HANDLER = require ( '../utils/response-helper' )
5+ const Organization = require ( '../models/Organisation' )
56const { changeFileOnRemote, addPageToIndex, fetchPagesIndex, updatePagesIndex, getOpts, getOrgId } = WikiHelper
67
78const 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 ( )
0 commit comments