Skip to content

Commit 2968aad

Browse files
committed
add authentication through Github, using session
1 parent 0092c1d commit 2968aad

6 files changed

Lines changed: 91 additions & 5 deletions

File tree

auth/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module.exports = function(app){
2+
var passport = require('passport')
3+
var GitHubStrategy = require('passport-github').Strategy;
4+
var session = require('express-session');
5+
var options = require('../appConfig');
6+
7+
// var User = require('../models/users');
8+
9+
app.use(session({secret:'tswift', resave: true, saveUninitialized: true}))
10+
11+
//Initialize passport and restore authentication state if available
12+
app.use(passport.initialize());
13+
app.use(passport.session());
14+
15+
passport.use(new GitHubStrategy({
16+
clientID: options.GITHUB_CLIENT_ID || process.env.CLIENT_ID,
17+
clientSecret: options.GITHUB_CLIENT_SECRET || process.env.CLIENT_SECRET,
18+
callbackURL: '/auth/github/callback',
19+
},
20+
function(accessToken, refreshToken, user, cb) {
21+
console.log("using GITHUB strategy")
22+
return cb(null, user);
23+
}
24+
));
25+
26+
// Check this re saving to a session, serialize and deserialize
27+
// https://stackoverflow.com/questions/27637609/understanding-passport-serialize-deserialize#27637668
28+
passport.serializeUser(function(user, done) {
29+
console.log("user", user);
30+
done(null, user.id);
31+
// done(null, user);
32+
});
33+
34+
passport.deserializeUser(function(id, done) {
35+
done(null, user);
36+
});
37+
38+
//OAuth authentication route
39+
app.get('/auth/github', passport.authenticate('github'));
40+
app.get('/auth/github/callback',
41+
passport.authenticate('github', { failureRedirect: '/' }),
42+
function(req, res) {
43+
res.redirect('/profile');
44+
});
45+
}

dev.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import webpackDevMiddleware from 'webpack-dev-middleware';
88
import webpackHotMiddleware from 'webpack-hot-middleware';
99
import config from './webpack.config';
1010
import routes from './routes';
11+
import auth from './auth';
1112

1213
const app = express();
1314
const port = 3000;
@@ -20,6 +21,7 @@ app.use(webpackHotMiddleware(compiler));
2021

2122
app.use(routes);
2223

24+
2325
app.listen(port, function(error) {
2426
if (error) {
2527
console.error(error)

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@
3434
"bootstrap": "^3.3.6",
3535
"classnames": "^2.1.2",
3636
"cross-env": "^1.0.7",
37-
"express": "^4.13.3",
3837
"enzyme": "^2.2.0",
38+
"express": "^4.13.3",
39+
"express-session": "^1.15.5",
3940
"jquery": "^2.2.3",
4041
"lokijs": "^1.3.16",
42+
"passport": "^0.4.0",
43+
"passport-github": "^1.1.0",
4144
"react": "^0.14.7",
4245
"react-dom": "^0.14.7",
4346
"react-redux": "^4.2.1",
@@ -49,7 +52,7 @@
4952
"whatwg-fetch": "^2.0.2"
5053
},
5154
"devDependencies": {
52-
"chai": "^3.5.0",
55+
"chai": "^3.5.0",
5356
"css-loader": "^0.28.4",
5457
"deep-freeze": "0.0.1",
5558
"eslint": "^3.10.2",

routes.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { express, Router } from 'express';
22
import path from 'path';
3+
import auth from './auth';
34
import bodyParser from 'body-parser';
45
import api from './backend/api';
56
import ItemPersist from './backend/stores/itemPersist.js';
@@ -10,6 +11,8 @@ const router = Router();
1011
var jsonParser = bodyParser.json()
1112
var urlencodedParser = bodyParser.urlencoded({ extended: false })
1213

14+
auth(router)
15+
1316
// return all resources
1417
router.get("/api/items", function(req, res){
1518
itempersist.all().then(
@@ -70,8 +73,22 @@ router.post("/api/items/", jsonParser, function(req,res){
7073
).catch(console.log)
7174
});
7275

73-
router.get("/", function(req, res) {
74-
res.sendFile(__dirname + '/index.html')
76+
router.get('/logout', function(req, res){
77+
req.logout();
78+
res.redirect('/');
79+
})
80+
81+
function ensureAuthenticated(req,res,next){
82+
if(req.isAuthenticated())
83+
return next();
84+
res.redirect('/');
85+
}
86+
87+
88+
// need the wildcart so react-router can take over
89+
// see https://codedump.io/share/V9K5oTL502r4/1/issue-with-routing-in-react-app
90+
router.get("*", function(req, res) {
91+
res.sendFile(__dirname + '/public/index.html')
7592
})
7693

7794
export default router;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, { Component } from 'react'
2+
import { Link } from 'react-router'
3+
4+
class Profile extends Component {
5+
constructor(){
6+
super()
7+
}
8+
render() {
9+
return (<div>
10+
<p>This will be the profile.</p>
11+
12+
<li><Link to="/auth/github">Login with Github</Link></li>
13+
</div>)
14+
}
15+
}
16+
17+
export default Profile

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Router, Route, browserHistory } from 'react-router'
99
import App from './components/routes/app/App'
1010
import Admin from './components/routes/admin/Admin'
1111
import ItemEdit from './components/routes/itemEdit/ItemEdit'
12+
import Profile from './components/routes/profile/Profile'
1213
import rootReducer from './reducers'
1314
import '../public/assets/sass/bootstrap.scss'
1415
import '../public/assets/sass/style.scss'
@@ -17,7 +18,8 @@ const routes = [
1718
{ path: '/', component: App },
1819
{ path: '/admin', component: Admin },
1920
{ path: '/new', component: ItemEdit },
20-
{ path: '/edit/:id', component: ItemEdit }
21+
{ path: '/edit/:id', component: ItemEdit },
22+
{ path: '/profile', component: Profile }
2123
]
2224

2325
fetch('/api/items').then((response) => {

0 commit comments

Comments
 (0)