Skip to content

Commit ea5b1ad

Browse files
committed
done github auth
1 parent c2989f6 commit ea5b1ad

9 files changed

Lines changed: 101 additions & 19 deletions

File tree

auth-All/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const flash = require('connect-flash');
55
const cookieParser = require('cookie-parser');
66
const session = require('express-session');
77

8-
// Google config
8+
// Social config
99
require('./config/googleConfig')(passport);
10+
require('./config/facebookConfig')(passport);
11+
require('./config/githubConfig')(passport);
1012

1113
app.use(cookieParser()); // read cookies (needed for auth)
1214
app.set('view engine', 'ejs'); // set up ejs for templating

auth-All/config/githubConfig.js

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

auth-All/config/myEnv.env

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ GOOGLE_CALLBACK=your-callback-url
1212

1313
FACEBOOK_CLIENT_ID=your-facebook-cliend-id
1414
FACEBOOK_CLIENT_SECRET=your-facebook-cliend-secret
15-
FACEBOOK_CALLBACK_URL=your-callback-url
15+
FACEBOOK_CALLBACK_URL=your-callback-url
16+
17+
GITHUB_CLIENT_ID=your-github-cliend-id
18+
GITHUB_CLIENT_SECRET=your-github-cliend-secret
19+
GITHUB_CALLBACK_URL=your-callback-url

auth-All/package-lock.json

Lines changed: 8 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"mongoose": "^5.9.24",
2727
"passport": "^0.4.1",
2828
"passport-facebook": "^3.0.0",
29+
"passport-github": "^1.1.0",
2930
"passport-google-oauth": "^2.0.0"
3031
}
3132
}

auth-All/public/css/style.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,4 @@
1010
-moz-border-radius: 50%;
1111
-ms-border-radius: 50%;
1212
-o-border-radius: 50%;
13-
}
14-
15-
.profileLogo {
16-
height: 100px;
17-
width: 100px;
1813
}

auth-All/routers/authRouter.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ module.exports = (app, passport) => {
1111
});
1212
});
1313

14-
/***** Google ****/
15-
// send to google to do the authentication
14+
/***** github ****/
15+
// send to github to do the authentication
1616
// profile gets us their basic information including their name
1717
// email gets their emails
18-
app.get('/auth/google',
18+
app.get('/auth/github',
1919
passport.authenticate(
20-
'google',
20+
'github',
2121
{ scope: ['profile', 'email'] }
2222
)
2323
);
2424

25-
app.get('/auth/google/callback',
25+
app.get('/auth/github/callback',
2626
passport.authenticate(
27-
'google',
27+
'github',
2828
{
2929
successRedirect: '/profile',
3030
failureRedirect: '/'
@@ -52,6 +52,27 @@ module.exports = (app, passport) => {
5252
}
5353
));
5454

55+
/***** Github ****/
56+
// send to github to do the authentication
57+
// profile gets us their basic information including their name
58+
// email gets their emails
59+
app.get('/auth/github',
60+
passport.authenticate(
61+
'github',
62+
{ scope: ['profile', 'email'] }
63+
)
64+
);
65+
66+
app.get('/auth/github/callback',
67+
passport.authenticate(
68+
'github',
69+
{
70+
successRedirect: '/profile',
71+
failureRedirect: '/'
72+
}
73+
));
74+
75+
// Logout common
5576
app.get('/logout', (req, res) => {
5677
req.logout();
5778
res.redirect('/');

auth-All/views/index.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<li><a href="/auth/facebook">
2424
<img class="logoDesign" src="./img/facebook.png" alt="facebook"></a>
2525
</li>
26-
<li><a href="/auth/google">
26+
<li><a href="/auth/github">
2727
<img class="logoDesign" src="./img/github.png" alt="github"></a>
2828
</li>
2929
<li><a href="/auth/google">

auth-All/views/profile.ejs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,27 @@
2020
<img class="logoDesign" src=<%= user.socialImg %> alt="">
2121
</div>
2222
</span>
23-
24-
<div>
25-
<strong>Email</strong>: <%= user.email %><br>
26-
<strong>Name</strong>: <%= user.name %><br><br>
27-
<img class="profileImg" src=<%= user.picture %> alt="">
23+
<br>
24+
<div class="row">
25+
<div class="col s12 m6">
26+
<img class="responsive-img" src=<%= user.picture %> alt="">
27+
</div>
28+
<div class="col s12 m6">
29+
<div class="card center-align">
30+
<div class="card-content">
31+
<table class="highlight">
32+
<tr>
33+
<td><h6>Email:</h6> </td>
34+
<td><%= user.email %></td>
35+
</tr>
36+
<tr>
37+
<td><h6>Name:</h6> </td>
38+
<td><%= user.name %></td>
39+
</tr>
40+
</table>
41+
</div>
42+
</div>
43+
</div>
2844
</div>
2945
</div>
3046
</div>

0 commit comments

Comments
 (0)