Skip to content

InternalOAuthError: failed to obtain request token #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
/node_modules
/node_modules
.env
37 changes: 36 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,41 @@ app.use(express.static(__dirname + '/public'));
// Setting up the Passport Strategies
require("./config/passport")(passport)

// Add code here:

app.get('/', function(req, res){
res.render('layout', {user: req.user});
});

// FACEBOOK
app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email'} ));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect: '/',
failureRedirect: '/'
})
);

// TWITTER
app.get('/auth/twitter', passport.authenticate('twitter', { scope: 'email'} ));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', {
successRedirect: '/',
failureRedirect: '/'
})
);

// GOOGLE
app.get('/auth/google',passport.authenticate('google', { scope: 'email' } ));
app.get('/auth/google/callback',
passport.authenticate('google', {
successRedirect: '/',
failureRedirect: '/'
})
);

app.get("/logout", function(req, res){
req.logout()
res.redirect("/")
})

app.listen(3000);
117 changes: 117 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
var User = require('../models/user');
var FacebookStrategy = require('passport-facebook').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;



module.exports = function(passport){
passport.serializeUser(function(user, done) {
done(null, user._id);
});

passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
console.log('deserializing user:',user);
done(err, user);
});
});

passport.use('google', new GoogleStrategy({
clientID : process.env.GOOGLE_API_KEY,
clientSecret : process.env.GOOGLE_API_SECRET,
callbackURL : "http://localhost:3000/auth/google/callbacK"
},function(token, tokenSecret, profile, done) {

process.nextTick(function() {

User.findOne({ 'gg.id' : profile.id }, function(err, user) {
if (err) return done(err);
if (user) {
return done(null, user);
} else {

var newUser = new User();
newUser.gg.id = profile.id;
newUser.gg.access_token = token;
newUser.gg.firstName = profile.displayName;
newUser.gg.email = profile.email;

newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}

});
});
}));

passport.use('twitter', new TwitterStrategy({
consumerKey : process.env.TWITTER_API_KEY,
consumerSecret: process.env.TWITTER_API_SECRET,
callbackURL : "http://localhost:3000/auth/twitter/callback"
},function(token, tokenSecret, profile, done) {

process.nextTick(function() {

User.findOne({ 'tw.id' : profile.id }, function(err, user) {
if (err) return done(err);
if (user) {
return done(null, user);
} else {

var newUser = new User();
newUser.tw.id = profile.id;

newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}

});
});
}));

passport.use('facebook', new FacebookStrategy({
clientID : process.env.FACEBOOK_API_KEY,
clientSecret : process.env.FACEBOOK_API_SECRET,
callbackURL : 'http://localhost:3000/auth/facebook/callback',
enableProof : true,
profileFields : ['name', 'emails']
}, function(access_token, refresh_token, profile, done) {

// // Use this to see the information returned from Facebook
// console.log(profile)

process.nextTick(function() {

User.findOne({ 'fb.id' : profile.id }, function(err, user) {
if (err) return done(err);
if (user) {
return done(null, user);
} else {

var newUser = new User();
newUser.fb.id = profile.id;
newUser.fb.access_token = access_token;
newUser.fb.firstName = profile.name.givenName;
newUser.fb.lastName = profile.name.familyName;
newUser.fb.email = profile.emails[0].value;

newUser.save(function(err) {
if (err)
throw err;
// after this is saved it will create a variable in the user object
return done(null, newUser);
});
}

});
});
}));

}
20 changes: 20 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var mongoose = require('mongoose');

module.exports = mongoose.model('User',{
// details from fb acct
fb: {
id: String,
access_token: String,
firstName: String,
lastName: String,
email: String
},
tw: {
id: String,
access_token: String
},
gg: {
id: String,
access_token: String
}
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"morgan": "~1.0.0",
"passport": "^0.2.0",
"passport-facebook": "^2.0.0",
"passport-google-oauth": "~0.1.5"
"passport-google-oauth": "~0.1.5",
"passport-twitter": "^1.0.3"
}
}
46 changes: 46 additions & 0 deletions views/layout.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<title>Facebook authentication</title>
</head>
<body>
<h1>FACEBOOK LOGIN USING PASSPORT</h1>
<div>
<% if(user != undefined){ %>
<h2>Below is the data sent by facebook</h2>
<pre>
<%= user.fb %>
</pre>
<a href="/logout">Logout</a>
<% } else { %>
<a href="/auth/facebook">Login with Facebook</a>
<% } %>
</div>

<h1>TWITTER LOGIN USING PASSPORT</h1>
<div>
<% if(user != undefined){ %>
<h2>Below is the data sent by TWITTER</h2>
<pre>
<%= user.tw %>
</pre>
<a href="/logout">Logout</a>
<% } else { %>
<a href="/auth/twitter">Login with TWITTER</a>
<% } %>
</div>

<h1>GOOGLE LOGIN USING PASSPORT</h1>
<div>
<% if(user != undefined){ %>
<h2>Below is the data sent by GOOGLE</h2>
<pre>
<%= user.gg %>
</pre>
<a href="/logout">Logout</a>
<% } else { %>
<a href="/auth/google">Login with GOOGLE</a>
<% } %>
</div>
</body>
</html>