trusted-setup-server/server/controllers/sessions.js

67 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-01-29 12:30:50 +01:00
const express = require('express')
const router = express.Router()
const oauth = require('oauth')
const logger = require('morgan')
const consumer = new oauth.OAuth(
'https://twitter.com/oauth/request_token',
'https://twitter.com/oauth/access_token',
process.env.TWITTER_CONSUMER_KEY,
process.env.TWITTER_CONSUMER_SECRET,
'1.0A',
process.env.TWITTER_CALLBACK_URL,
'HMAC-SHA1'
)
2020-01-30 13:15:42 +01:00
router.get('/user_data', (req, res) => {
let userData = { name: 'Anonymous' }
consumer.get(
'https://api.twitter.com/1.1/account/verify_credentials.json',
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
function(error, data) {
2020-02-06 15:24:04 +01:00
if (!error) {
2020-01-30 13:15:42 +01:00
userData = JSON.parse(data)
userData.handle = userData.screen_name
2020-02-05 16:02:34 +01:00
req.session.handle = userData.screen_name
req.session.socialType = 'twitter'
2020-01-30 13:15:42 +01:00
}
res.json(userData)
}
)
})
2020-01-29 12:30:50 +01:00
router.get('/connect', (req, res) => {
consumer.getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret) {
if (error) {
res.status(500).send(error)
} else {
req.session.oauthRequestToken = oauthToken
req.session.oauthRequestTokenSecret = oauthTokenSecret
res.redirect(
2020-01-30 13:15:42 +01:00
'https://twitter.com/oauth/authorize?oauth_token=' + req.session.oauthRequestToken
2020-01-29 12:30:50 +01:00
)
}
})
})
router.get('/twitter_callback', (req, res) => {
consumer.getOAuthAccessToken(
req.query.oauth_token,
req.session.oauthRequestTokenSecret,
req.query.oauth_verifier,
(error, oauthAccessToken, oauthAccessTokenSecret) => {
if (error) {
logger.error(error)
res.status(500).send(error)
} else {
req.session.oauthAccessToken = oauthAccessToken
req.session.oauthAccessTokenSecret = oauthAccessTokenSecret
2020-02-05 11:53:45 +01:00
res.redirect('/make-contribution')
2020-01-29 12:30:50 +01:00
}
}
)
})
module.exports = router