Skip to content

Instantly share code, notes, and snippets.

@jeserodz
Last active May 6, 2021 14:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeserodz/a7fdae8f45a2b61cf8be to your computer and use it in GitHub Desktop.
Save jeserodz/a7fdae8f45a2b61cf8be to your computer and use it in GitHub Desktop.
Instagram authentication with scopes permissions for Passport and Node.js.
// Node.js module to define an ExpressJS subrouter that handles requests
// to /auth path and uses passport to login using Instagram OAuth
var express = require('express');
var session = require('express-session');
var passport = require('passport');
var instagramStrategy = require('passport-instagram').Strategy;
// Require user model
var User = require('./user');
// Setup instagram strategy for passport
passport.use(new instagramStrategy({
clientID: process.env.INSTAGRAM_CLIENT_ID,
clientSecret: process.env.INSTAGRAM_CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/instagram/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOneAndUpdate(
// query document
{ instagramId: profile._json.data.id},
// update-create document
{
username: profile._json.data.username,
displayName: profile._json.data.full_name,
picture: profile._json.data.profile_picture,
instagramId: profile._json.data.id,
instagramAccessToken: accessToken
},
// options
{
'new': true,
upsert: true,
},
// callback
function(err, user) {
done(err, user);
}
);
}
));
// Passport Serialization/Deserialization of user.id inside req object
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(_id, done) {
User.findById(_id, function(err, user) {
done(err, user);
});
});
// Configure Express router and middlewares (session and passport)
var auth = express.Router();
auth.use(session({
secret: 'session secret',
saveUninitialized: false,
resave: false
}));
auth.use(passport.initialize());
auth.use(passport.session());
// Routes
// We can define the scope of token by provinding scope in the options object
auth.get('/instagram', passport.authenticate('instagram', { scope: ['basic', 'public_content', 'follower_list', 'comments', 'relationships', 'likes'] }));
auth.get('/instagram/callback',
passport.authenticate('instagram', {failureRedirect: '/fail'}),
function(req, res) {
// Successful authentication, redirect home
res.redirect('/auth/ok');
});
// Verify user after auth
auth.get('/ok', function(req, res) {
console.log(req.user);
res.json({session: req.user});
});
module.exports = auth;
@wzup
Copy link

wzup commented Aug 9, 2017

I wonder why this is not covered in official docs for passport instagram strategy.

Thanks for the gist

@deepakgd
Copy link

Unable to get email id from instagram. Is there anyother scope need to add to get email address?

@shivamv12
Copy link

{"error_type": "OAuthException", "code": 400, "error_message": "Invalid scope: ['basic', 'comments', 'follower_list', 'likes', 'public_content', 'relationships']"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment