Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivamPrajapati-dev committed May 1, 2021
1 parent db22ac6 commit e624e7f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
19 changes: 9 additions & 10 deletions Fetch/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,21 @@ async function checkToken() {
const token = await fetchNewToken(process.env.REFRESH_TOKEN);

let new_token = new TokenStore({
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
refresh_token:process.env.REFRESH_TOKEN,
expires_in: token.expires_in,
refresh_token: token.access_token,
access_token: token.access_token,
});

await new_token.save();
} else if (Date.now() - Date.parse(data.date) > data.expires_in * 1000) {
} else if (Date.now() - Date.parse(data.time) > data.expires_in * 1000) {
const token = await fetchNewToken(process.env.REFRESH_TOKEN);
data.refresh_token = token.access_token;
data.date = Date.now();
data.access_token = token.access_token;
data.time = Date.now();
data.expires_in = token.expires_in;
await data.save();
oacth2client.setCredentials({ refresh_token: token.access_token });
oacth2client.setCredentials({ access_token: token.access_token });
} else {
oacth2client.setCredentials({ refresh_token: data.refresh_token });
oacth2client.setCredentials({ access_token: data.access_token });
}
}

Expand All @@ -75,14 +74,14 @@ app.get("/getfiles/:id", async (req, res) => {
var fileId = req.params.id;

const info = await getAsync(fileId);

await checkToken();
if (info) {
res.send(JSON.parse(info));
return;
}

console.log("here");
await checkToken();


const response = await drive.files.list({
includeRemoved: false,
Expand Down
5 changes: 2 additions & 3 deletions Fetch/model/token.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const mongoose = require("mongoose");
const schema = mongoose.Schema({
access_token:{type:String},
refresh_token: { type: String },
client_id: { type: String },
client_secret: { type: String },
date: { type: Date, default: Date.now() },
time: { type: Date, default: Date.now() },
expires_in: { type: Number },
},{
collection:'base_tokenstuff'
Expand Down

0 comments on commit e624e7f

Please sign in to comment.