Browse Source

Add json token

k.niemeskern 2 years ago
parent
commit
e315d1a799
4 changed files with 28 additions and 14 deletions
  1. 24 1
      index.js
  2. 1 0
      package.json
  3. 0 13
      postTest.js
  4. 3 0
      token.json

+ 24 - 1
index.js

@@ -10,6 +10,9 @@ const bodyParser = require('body-parser');
 const validator = require('validator');
 const API = require('./lib/API');
 const mail =  require('./notification.js')
+const jsonfile = require('jsonfile')
+const tokenFile = './token.json'
+
 
 let info = {
     followers: 99999,
@@ -22,12 +25,32 @@ let info = {
     error: 0
 }
 
+jsonfile.readFile(tokenFile, function (err, obj) {
+    if (err) console.error(err)
+    else {
+        console.dir(obj)
+        obj.access_token = process.env.access_token
+    }
+  })
+
+
 if (process.env.access_token) {
     info.tokens = process.env.access_token
     info.active = true
 }
+
 if (process.env.expire) info.expire = process.env.expire
 
+function updateTokenEnv(token){
+
+    let reqObj = {"access_token": token}
+
+    jsonfile.writeFile(tokenFile, reqObj, function (err) {
+        if (err) console.error(err)
+        else console.log("Token File updated")
+      })
+}
+
 async function watchLinkedin(){
     
     if (info.active){
@@ -50,7 +73,6 @@ async function watchLinkedin(){
             }
         });
     }
-
 }
 
 setInterval(watchLinkedin, info.updateTime)
@@ -81,6 +103,7 @@ app.get('/callback', async (req, res) => {
             info.active = true
             process.env.access_token = info.token
             process.env.expire = data.expire ? data.expire : 0
+            updateTokenEnv(info.token)
         }
         res.redirect('/');
     } catch(err) {

+ 1 - 0
package.json

@@ -13,6 +13,7 @@
     "axios": "^0.21.1",
     "body-parser": "^1.19.0",
     "express": "^4.17.1",
+    "jsonfile": "^6.1.0",
     "node-linkedin": "^0.5.6",
     "nodemailer": "^6.6.2",
     "path": "^0.12.7",

+ 0 - 13
postTest.js

@@ -1,13 +0,0 @@
-let axios = require('axios')
-// axios.post('http://woodilis.eu-4.evennode.com/update', {
-
-axios.post('https://linkedin-counter.herokuapp.com/update', {
-    count: 54321,
-  })
-  .then(function (response) {
-    console.log(response.data);
-  })
-  .catch(function (error) {
-    console.log(error);
-    console.log("error")
-  });

+ 3 - 0
token.json

@@ -0,0 +1,3 @@
+{
+    "access_token": ""
+}