Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Kunal614/Resources into main
Browse files Browse the repository at this point in the history
  • Loading branch information
KUNAL committed Apr 29, 2021
2 parents e0385bc + db22ac6 commit 676bcf0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Fetch/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require("dotenv").config();
const redis = require("redis");
const mongoose = require("mongoose");
const TokenStore = require("./model/token");

const port = process.env.PORT || 3000;
const { promisify } = require("util");

let client = redis.createClient({
Expand Down Expand Up @@ -32,11 +32,12 @@ const drive = google.drive({
});

async function fetchNewToken(tkn) {
console.log('generating new token...');
const token = await axios.post("https://oauth2.googleapis.com/token", {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
refresh_token: tkn,
grant_type: "refresh_token",
grant_type: "refresh_token"
});

return token.data;
Expand All @@ -58,7 +59,7 @@ async function checkToken() {

await new_token.save();
} else if (Date.now() - Date.parse(data.date) > data.expires_in * 1000) {
const token = await fetchNewToken(data.refresh_token);
const token = await fetchNewToken(process.env.REFRESH_TOKEN);
data.refresh_token = token.access_token;
data.date = Date.now();
data.expires_in = token.expires_in;
Expand All @@ -71,7 +72,6 @@ async function checkToken() {

app.get("/getfiles/:id", async (req, res) => {
try {
await checkToken();
var fileId = req.params.id;

const info = await getAsync(fileId);
Expand All @@ -82,6 +82,8 @@ app.get("/getfiles/:id", async (req, res) => {
}

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

const response = await drive.files.list({
includeRemoved: false,
spaces: "drive",
Expand Down Expand Up @@ -126,6 +128,7 @@ app.get("/updatecache/:id", (req, res) => {
res.send({ success: true });
});


mongoose.connect(
process.env.MONGO_URL,
{
Expand All @@ -134,7 +137,7 @@ mongoose.connect(
},
(err, res) => {
if (!err) {
app.listen("3000", () => {
app.listen(port, () => {
console.log("server on");
});
}
Expand Down
2 changes: 2 additions & 0 deletions Fetch/model/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const schema = mongoose.Schema({
client_secret: { type: String },
date: { type: Date, default: Date.now() },
expires_in: { type: Number },
},{
collection:'base_tokenstuff'
});

const model = mongoose.model("token_store", schema);
Expand Down

0 comments on commit 676bcf0

Please sign in to comment.