Last active
October 12, 2017 19:15
-
-
Save ivanvs/52088dbe422d3099ad0ff4aaa2f781cf to your computer and use it in GitHub Desktop.
React example how to do authenitcation with Twitter
This file contains hidden or 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
| router.route('/auth/twitter') | |
| .post((req, res, next) => { | |
| request.post({ | |
| url: `https://api.twitter.com/oauth/access_token?oauth_verifier`, | |
| oauth: { | |
| consumer_key: 'KEY', | |
| consumer_secret: 'SECRET', | |
| token: req.query.oauth_token | |
| }, | |
| form: { oauth_verifier: req.query.oauth_verifier } | |
| }, function (err, r, body) { | |
| if (err) { | |
| return res.send(500, { message: err.message }); | |
| } | |
| console.log(body); | |
| const bodyString = '{ "' + body.replace(/&/g, '", "').replace(/=/g, '": "') + '"}'; | |
| const parsedBody = JSON.parse(bodyString); | |
| req.body['oauth_token'] = parsedBody.oauth_token; | |
| req.body['oauth_token_secret'] = parsedBody.oauth_token_secret; | |
| req.body['user_id'] = parsedBody.user_id; | |
| next(); | |
| }); | |
| }, passport.authenticate('twitter-token', {session: false}), function(req, res, next) { | |
| if (!req.user) { | |
| return res.send(401, 'User Not Authenticated'); | |
| } | |
| // prepare token for API | |
| req.auth = { | |
| id: req.user.id | |
| }; | |
| return next(); | |
| }, generateToken, sendToken); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment