-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,11 +170,24 @@ MongoClient.connect("mongodb+srv:https://su123:[email protected]/db?ret | |
|
||
|
||
app.post('/upload', function (req, res) { | ||
|
||
var retries = 2; | ||
|
||
var send401Response = function() { | ||
return res.status(401).end(); | ||
}; | ||
db.collection('users').findOne({email : req.user.email}) | ||
.then(srt => { | ||
|
||
|
||
if (!req.user) res.redirect('/auth/google') | ||
if(err || !user) { return send401Response(); } | ||
var makeRequest = function() { | ||
retries--; | ||
if(!retries) { | ||
// Couldn't refresh the access token. | ||
return send401Response(); | ||
} | ||
else { | ||
console.log('upload route called'); | ||
const oauth2Client = new google.auth.OAuth2(); | ||
|
@@ -189,6 +202,28 @@ MongoClient.connect("mongodb+srv:https://su123:[email protected]/db?ret | |
version: 'v3', | ||
auth: oauth2Client | ||
}); | ||
request.then(function(reason) { | ||
if(reason.code === 401) { | ||
// Access token expired. | ||
// Try to fetch a new one. | ||
refresh.requestNewAccessToken('google', user.refreshToken, function(err, accessToken) { | ||
if(err || !accessToken) { return send401Response(); } | ||
|
||
// Save the new accessToken for future use | ||
user.save({ token: accessToken }, function() { | ||
// Retry the request. | ||
makeRequest(); | ||
}); | ||
}); | ||
}; | ||
makeRequest(); | ||
|
||
} else { | ||
// There was another error, handle it appropriately. | ||
return res.status(reason.code).json(reason.message); | ||
} | ||
}); | ||
makeRequest(); | ||
|
||
//move file to google drive | ||
|
||
|