-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added default mongoose/schemas - Added foundation for Facebook signin - added connect-flash for messaging
- Loading branch information
Showing
12 changed files
with
201 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,34 @@ | ||
'use strict'; | ||
|
||
// third-party requires | ||
var mongoose = require('mongoose'); | ||
var express = require('express'); | ||
|
||
var passport = require('passport'); | ||
var flash = require('connect-flash'); | ||
var app = express(); | ||
|
||
// internal requires | ||
var config = require('./config.json'); | ||
|
||
// express settings | ||
app.set('views', 'views'); | ||
app.set('view engine', 'ejs'); | ||
app.use(express.static('static')); | ||
app.use(express.cookieParser('moooooooo!')); | ||
app.use(express.session({ key: 'sid', cookie: { maxAge: 60000 }})); | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
app.use(flash()); | ||
|
||
app.locals.message = ''; | ||
|
||
// connect to the local database | ||
mongoose.connect(config.database.ip, config.database.name); | ||
|
||
// setup routes | ||
require('./lib/routes')(app); | ||
console.log('Server Listening on port 3000'); | ||
app.listen(3000); | ||
|
||
// Output is important for grunt to know the | ||
// server has started | ||
app.listen(config.server.port); | ||
console.log(config.project.name + ' listening on port ' + config.server.port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"project": { | ||
"name": "scltemplates" | ||
}, | ||
"database": { | ||
"name": "scltemplates", | ||
"ip": "127.0.0.1" | ||
}, | ||
|
||
"server": { | ||
"port": 3000 | ||
}, | ||
|
||
"social": { | ||
"facebook": { | ||
"appId": "768637973146375", | ||
"appSecret": "dc55621a5e56045b7c0aed8efd1a04aa" | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,36 @@ | ||
'use strict'; | ||
var setupFacebook = require('./setupFacebook.js'); | ||
|
||
function requireAuth(req, res, next) { | ||
if (req.isAuthenticated()) { | ||
next(); | ||
} | ||
else { | ||
req.flash('info', 'You must be logged in to do that!'); | ||
res.redirect('/'); | ||
} | ||
} | ||
|
||
module.exports = function(app) | ||
{ | ||
// setup facbeook login | ||
setupFacebook(app); | ||
|
||
// index/root view | ||
app.get('/', function(req, res) { | ||
// index view. | ||
res.render('index.ejs', {text: 'Hello World'}); | ||
// console./log(req.flash('info')[0]); | ||
res.render('index.ejs', {message: req.flash('info')[0]}); | ||
}); | ||
|
||
// logout to clear the sessions | ||
app.get('/logout', function(req, res) { | ||
|
||
req.session = null; | ||
res.redirect('/'); | ||
}); | ||
|
||
// restricted "home" view | ||
app.get('/home', requireAuth, function(req, res){ | ||
res.render('home.ejs'); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
var mongoose = require('mongoose'); | ||
|
||
var userSchema = mongoose.Schema({ | ||
'version': { | ||
type: Number, | ||
default: 1 | ||
}, | ||
'username': String, | ||
'email': String, | ||
'realName': String, | ||
'facebookId': String, | ||
'joinDate': Date | ||
}); | ||
|
||
var User = mongoose.model('user', userSchema); | ||
|
||
module.exports = { | ||
User: User | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
var passport = require('passport'); | ||
var FacebookStrategy = require('passport-facebook').Strategy; | ||
var User = require('./schemas.js').User; | ||
var config = require('../config.json'); | ||
|
||
// function createFacebookPhotoUrl(facebookId) { | ||
// return 'https://graph.facebook.com/' + facebookId + '/picture'; | ||
// } | ||
|
||
|
||
module.exports = function(app) { | ||
// routes: | ||
app.get('/auth/facebook', passport.authenticate('facebook', { | ||
scope: 'email' | ||
})); | ||
|
||
app.get('/auth/facebook/callback', passport.authenticate('facebook', { | ||
failureRedirect: '/', | ||
successRedirect: '/home' | ||
})); | ||
|
||
// passport stuff | ||
passport.serializeUser(function(user, done) { | ||
done(null, user.id); | ||
}); | ||
|
||
passport.deserializeUser(function(id, done) { | ||
User.findById(id, function(err, user) { | ||
if (err) { | ||
done(err); | ||
} | ||
if (user) { | ||
done(null, user); | ||
} | ||
}); | ||
}); | ||
|
||
passport.use(new FacebookStrategy({ | ||
clientID: config.social.facebook.appId, | ||
clientSecret: config.social.facebook.appSecret, | ||
callbackURL: '/auth/facebook/callback', | ||
profileFields: ['id', 'displayName', 'photos', 'emails', 'username', 'name'] | ||
}, | ||
function(accessToken, refreshToken, profile, done) { | ||
User.findOne({ | ||
email: profile.emails[0].value | ||
}, function(err, user) { | ||
if (user) { | ||
done(null, user); | ||
} | ||
else { | ||
new User({ | ||
'username': '', | ||
'email': profile.emails[0].value, | ||
'realName': profile.displayName, | ||
'facebookId': profile.id, | ||
joinDate: new Date() | ||
}).save(function(err, newUser) { | ||
done(null, newUser); | ||
}); | ||
} | ||
}); | ||
})); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<% include layout/top %> | ||
|
||
<div class="container"><a href="/logout">Logout</a></div> | ||
<% include layout/bottom %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters