Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The idea is to store your values before redirecting user on twitter for authentication, and re-use these values once the user came back.</p> <p>OAuth2 includes the scope parameter, which perfectly suits that case. Unfortunately, TwitterStrategy is based on OAuth1. But we can tackle it !</p> <p>The next trick is about when creating the user. You should not do it when declaring strategy (because you cannot access input data), but a little later, in the last authentication callback <a href="https://github.com/jaredhanson/passport/blob/master/lib/passport/middleware/authenticate.js#L32" rel="nofollow">see here the callback arguments</a>.</p> <p>Declaring your strategy: </p> <pre><code>passport.use(new TwitterStrategy({ consumerKey: config.development.tw.consumerKey, consumerSecret: config.development.tw.consumerSecret, callbackURL: config.development.tw.callbackURL }, function(token, tokenSecret, profile, done) { // send profile for further db access done(null, profile); })); </code></pre> <p>When declaring your authentication url (repeat for a/twitter and v/twitter): </p> <pre><code>// declare states where it's accessible inside the clusre functions var states={}; app.get("/auth/c/twitter", function (req, res, next) { // save here your values: database and input var reqId = "req"+_.uniqueId(); states[reqId] = { database: 'c', age: $('input[name="creator[age]"]').val() }; // creates an unic id for this authentication and stores it. req.session.state = reqId; // in Oauth2, its more like : args.scope = reqId, and args as authenticate() second params passport.authenticate('twitter')(req, res, next) }, function() {}); </code></pre> <p>Then when declaring the callback: </p> <pre><code>app.get("/auth/twitter/callback", function (req, res, next) { var reqId = req.session.state; // reuse your previously saved state var state = states[reqId] passport.authenticate('twitter', function(err, token) { var end = function(err) { // remove session created during authentication req.session.destroy() // authentication failed: you should redirect to the proper error page if (err) { return res.redirect("/"); } // and eventually redirect to success url res.redirect("/dashboard"); } if (err) { return end(err); } // now you can write into database: var query = User.findOne({ 'twId': profile.id}); query.exec(function(err, oldUser){ if(oldUser) { return end() } // here, choose the right database depending on state var newUser = new User(); newUser.twId = profile.id; newUser.twUsername = profile.username; newUser.name = profile.displayName; newUser.avatar = profile.photos[0].value; // reuse the state variable newUser.age = state.age newUser.save(end); }); })(req, res, next) }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload