Created
March 31, 2018 09:28
-
-
Save JesusMurF/487018a7b749ec496524ae12d5070cf7 to your computer and use it in GitHub Desktop.
SignIn with Instagram in Node.js
This file contains 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
const passport = require('passport'); | |
const InstagramStrategy = require('passport-instagram').Strategy; | |
const User = require('../models/User'); | |
const instaConfig = { | |
clientID: process.env.CLIENT_ID, | |
clientSecret: process.env.CLIENT_SECRET, | |
callbackURL: process.env.CALLBACK_URL | |
}; | |
const instagramInit = function(accessToken, refreshToken, profile, callback) { | |
User.findOne({ 'instagram.id': profile.id }, function(err, user) { | |
if (err) return callback(err); | |
if (user) { | |
return callback(null, user); // Check if user already exists | |
} | |
const { | |
id, | |
full_name, | |
username, | |
profile_picture, | |
bio, | |
website, | |
counts: { media, follows, followed_by } | |
} = profile._json.data; | |
const new_user = new User({ | |
instagram: { | |
id, | |
accessToken, | |
full_name, | |
username, | |
profile_picture, | |
bio, | |
website, | |
counts: { | |
media, | |
follows, | |
followed_by | |
} | |
} | |
}); | |
new_user.save(function(err, user) { | |
if (err) { | |
throw err; | |
} | |
return callback(null, user); | |
}); | |
}); | |
}; | |
passport.use(new InstagramStrategy(instaConfig, instagramInit)); | |
passport.serializeUser(function(user, done) { | |
done(null, user); | |
}); | |
passport.deserializeUser(function(obj, done) { | |
done(null, obj); | |
}); | |
function ensureAuthenticated(request, response, next) { | |
if (request.isAuthenticated()) { | |
return next(); | |
} | |
response.redirect('/'); | |
} | |
module.exports = ensureAuthenticated; |
This file contains 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
const mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost:27017/mydatabase'); |
This file contains 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
const express = require('express'); | |
const session = require('express-session'); | |
const passport = require('passport'); | |
const exphbs = require('express-handlebars'); | |
const bodyParser = require('body-parser'); | |
require('./config/db'); | |
const app = express(); | |
app.engine('handlebars', exphbs({ defaultLayout: 'main' })); | |
app.set('view engine', 'handlebars'); | |
const ensureAuthenticated = require('./controllers/authentication_instagram'); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(bodyParser.json()); | |
app.use( | |
session({ | |
saveUninitialized: true, | |
resave: true, | |
secret: 'secret' | |
}) | |
); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.get('/', (request, response) => { | |
response.render('home'); | |
}); | |
app.get('/profile', ensureAuthenticated, (request, response) => { | |
const { instagram } = request.user; | |
response.render('profile', { user: instagram }); | |
}); | |
app.get('/logout', function(req, res) { | |
req.logout(); | |
res.redirect('/'); | |
}); | |
app.get('/auth/instagram', passport.authenticate('instagram')); | |
app.get( | |
'/auth/instagram/callback', | |
passport.authenticate('instagram', { | |
successRedirect: '/profile', | |
failureRedirect: '/login' | |
}) | |
); | |
const port = 9000; | |
app.listen(port); |
This file contains 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
const { Schema } = require('mongoose'); | |
const mongoose = require('mongoose'); | |
const User = new Schema({ | |
instagram: { | |
id: String, | |
accessToken: String, | |
full_name: String, | |
username: String, | |
profile_picture: String, | |
bio: String, | |
website: String, | |
counts: { | |
media: Number, | |
follows: Number, | |
followed_by: Number | |
} | |
} | |
}); | |
module.exports = mongoose.model('User', User); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment