Skip to content

Commit 01ca1c7

Browse files
committed
done twitter auth
1 parent e656c5e commit 01ca1c7

8 files changed

Lines changed: 76 additions & 4 deletions

File tree

auth-All/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const session = require('express-session');
99
require('./config/googleConfig')(passport);
1010
require('./config/facebookConfig')(passport);
1111
require('./config/githubConfig')(passport);
12+
require('./config/twitterConfig')(passport);
1213
require('./config/linkedinConfig')(passport);
1314

1415
/*** Middleware ***/

auth-All/config/twitterConfig.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
let TwitterStrategy = require('passport-twitter').Strategy;
2+
3+
module.exports = (passport) => {
4+
// used to serialize the user for the session
5+
passport.serializeUser((user, cb) => {
6+
cb(null, user);
7+
});
8+
9+
// used to deserialize the user
10+
passport.deserializeUser((user, cb) => {
11+
cb(null, user);
12+
});
13+
14+
// Goggle
15+
passport.use(new TwitterStrategy(
16+
{
17+
consumerKey: process.env.TWITTER_CLIENT_ID,
18+
consumerSecret: process.env.TWITTER_CLIENT_SECRET,
19+
callbackURL: process.env.TWITTER_CALLBACK,
20+
includeEmail: true
21+
},
22+
function(token, refreshToken, profile, cb) {
23+
let user = {};
24+
process.nextTick(() => {
25+
user.name = profile._json.name;
26+
user.email = profile._json.email;
27+
user.picture = profile._json.profile_image_url.replace('_normal','');
28+
user.socialName = "Twitter";
29+
user.socialImg = "./img/twitter.png";
30+
return cb(null, user);
31+
});
32+
}
33+
));
34+
};

auth-All/package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auth-All/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"passport-facebook": "^3.0.0",
2929
"passport-github2": "^0.1.12",
3030
"passport-google-oauth": "^2.0.0",
31-
"passport-linkedin-oauth2": "^2.0.0"
31+
"passport-linkedin-oauth2": "^2.0.0",
32+
"passport-twitter": "^1.0.4"
3233
}
3334
}

auth-All/routers/authRouter.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ module.exports = (app, passport) => {
8080
}
8181
));
8282

83+
/***** Twitter ****/
84+
// send to twitter to do the authentication
85+
// profile gets us their basic information including their name
86+
// email gets their emails
87+
app.get('/auth/twitter',
88+
passport.authenticate('twitter'));
89+
90+
app.get('/auth/twitter/callback',
91+
passport.authenticate(
92+
'twitter',
93+
{
94+
successRedirect: '/profile',
95+
failureRedirect: '/'
96+
}
97+
));
8398

8499
// Logout common
85100
app.get('/logout', (req, res) => {

auth-All/views/index.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<li><a href="/auth/google">
3131
<img class="logoDesign" src="./img/stackoverflow.jpg" alt="stackoverflow"></a>
3232
</li>
33-
<li><a href="/auth/google">
33+
<li><a href="/auth/twitter">
3434
<img class="logoDesign" src="./img/twitter.png" alt="twitter"></a>
3535
</li>
3636
<li><a href="/auth/linkedin">

auth-Google/config/googleConfig.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ async function extractProfile(profile) {
1111
profilePhoto: profile._json.picture,
1212
createAt: Date.now()
1313
};
14-
1514
let oldUser = await User.findOne({ googleId: userData.googleId });
1615

1716
if (oldUser) return null;

auth-Google/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let dotEnv = require('dotenv');
22
dotEnv.config({ path: './config/myEnv.env' });
33
// DB connect
4-
require('./config/dbConfig');
4+
//require('./config/dbConfig');
55

66
let app = require('./app');
77

0 commit comments

Comments
 (0)