Skip to content

Instantly share code, notes, and snippets.

@JesusMurF
Created March 31, 2018 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JesusMurF/487018a7b749ec496524ae12d5070cf7 to your computer and use it in GitHub Desktop.
Save JesusMurF/487018a7b749ec496524ae12d5070cf7 to your computer and use it in GitHub Desktop.
SignIn with Instagram in Node.js
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;
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/mydatabase');
<h1 class="title">Inicia sesión con Instagram
<i class="fab fa-instagram"></i>
</h1>
<a class="button is-primary" href="/auth/instagram">
<span class="icon is-small">
<i class="fab fa-instagram"></i>
</span>
<span>Inicia sesión con Instagram</span>
</a>
<a class="button is-default" href="/profile">Ir al perfil</a>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.2/css/bulma.min.css">
<script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
<title>Instagram login app</title>
</head>
<body>
{{{body}}}
</body>
</html>
<ul>
<li>{{user.full_name}}</li>
<li>{{user.username}}</li>
<li>
<img src="{{user.profile_picture}}" alt="">
</li>
<li>{{user.bio}}</li>
<li>
<a href="{{user.website}}">{{user.website}}</a>
</li>
<li>media {{user.counts.media}} follows {{user.counts.follows}} followers {{user.counts.followed_by}}</li>
</ul>
<a href="/logout" class="button is-danger">Cerrar sesión</a>
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);
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