1

I would like to use Oauth1.o to get credentials from a provider but I am halfway stuck. I am using passportjs for the implementation.

Here is the brief code:

  1. oauth.js
    const passport = require('passport');
    const OAuthStrategy = require('passport-oauth').OAuthStrategy;

    passport.use(
        'provider',
        new OAuthStrategy({
            requestTokenURL: "****",
            accessTokenURL: "****",
            userAuthorizationURL: "****",
            consumerKey: "****",
            consumerSecret: "****",
            callbackURL: "****",
        },
        (token, tokenSecret, profile, done) => {
            store[token] = tokenSecret;
            console.log(profile);
            done(null, profile);
        })
    );

    module.exports = passport;
  1. Express mini-app(modularised routing)
const express = require('express');
const router = express.Router();

const passport = require('./oauth');

/**
 * Initialize Passport
 * Restore authentication state, if any, from the session
 */
router.use(passport.initialize());
router.use(passport.session());

router.get('/oauth/sign-in', passport.authenticate('provider'));

router.get('/oauth/callback', (req, res) => {
    const { oauth_token, oauth_verifier, denied } = req.query;

    if (denied !== null && denied !== undefined) {
        req.session.destroy();
        res.json(denied);
    }

    res.json({ oauth_token, oauth_verifier });
});

module.exports = router;

From the above code, I successfully get to the callback URL after I authorize the app on the Twitter page. But I am unable to console.log the token, tokenSecret & profile. The documentation was also not clear to me as to how to use the done callback function.

I am on a learning journey using https://github.com/twitterdev/twauth-web/blob/master/twauth-web.py and would rather avoid using passport-twitter to learn master the example they have given.

Mnengwa
  • 217
  • 4
  • 10

1 Answers1

0

You need to add the authenticate middleware in the callback route as well. You can check out the docs here:

router.get('/oauth/callback', passport.authenticate('provider', {
  failureRedirect: '/login' //optional
}),
  (req, res) => {
    //...your code...
  });
Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35