-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoauth.js
57 lines (51 loc) · 1.63 KB
/
oauth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(function () {
var Evernote = require('evernote').Evernote
, config = require('./config.json')
, callbackUrl = "http://localhost:1820/oauth_callback";
// OAuth
exports.oauth = function (req, res) {
var client = new Evernote.Client({
consumerKey: config.API_CONSUMER_KEY,
consumerSecret: config.API_CONSUMER_SECRET,
sandbox: config.SANDBOX
});
client.getRequestToken(callbackUrl, function (error, oauthToken, oauthTokenSecret, results) {
if (error) {
req.session.error = JSON.stringify(error);
console.log('ERROR AUTHENTICATING: ' + JSON.stringify(error));
res.redirect('/');
}
else {
req.session.oauthToken = oauthToken;
req.session.oauthTokenSecret = oauthTokenSecret;
res.redirect(client.getAuthorizeUrl(oauthToken));
}
});
};
// OAuth callback
exports.oauth_callback = function(req, res) {
var client = new Evernote.Client({
consumerKey: config.API_CONSUMER_KEY,
consumerSecret: config.API_CONSUMER_SECRET,
sandbox: config.SANDBOX
});
client.getAccessToken(
req.session.oauthToken,
req.session.oauthTokenSecret,
req.param('oauth_verifier'),
function(error, oauthAccessToken, oauthAccessTokenSecret, results) {
if(error) {
console.log("ERROR DURING CALLBACK: " + JSON.stringify(error));
res.redirect('/');
} else {
console.log("OAUTH_ACCESS_TOKEN: " + oauthAccessToken);
res.redirect('/');
}
});
};
// Clear session
exports.clear = function(req, res) {
req.session.destroy();
res.redirect('/');
};
}());