Skip to content

Commit

Permalink
The big SQL overhaul. Expect bugs!
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Alves committed May 31, 2016
1 parent bbd9056 commit 9f7aeb8
Show file tree
Hide file tree
Showing 19 changed files with 215 additions and 248 deletions.
4 changes: 4 additions & 0 deletions generated/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"chai": "^2.1.0",
"chalk": "^1.0.0",
"connect-mongo": "^0.7.0",
"connect-session-sequelize": "^3.0.0",
"cookie-parser": "^1.3.4",
"express": "^4.12.0",
"express-session": "^1.10.3",
Expand Down Expand Up @@ -58,7 +59,10 @@
"passport-google-oauth": "^0.1.5",
"passport-local": "^1.0.0",
"passport-twitter": "^1.0.2",
"pg": "^4.5.5",
"pg-hstore": "^2.3.2",
"run-sequence": "^1.0.2",
"sequelize": "^3.23.3",
"serve-favicon": "^2.2.0",
"sinon": "^1.13.0",
"socket.io": "^1.3.4",
Expand Down
25 changes: 9 additions & 16 deletions generated/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,10 @@ name in the environment files.
*/

var mongoose = require('mongoose');
var Promise = require('bluebird');
var chalk = require('chalk');
var connectToDb = require('./server/db');
var User = mongoose.model('User');

var wipeCollections = function () {
var removeUsers = User.remove({});
return Promise.all([
removeUsers
]);
};
var db = require('./server/db');
var User = db.model('user');
var Promise = require('sequelize').Promise;

var seedUsers = function () {

Expand All @@ -43,14 +35,15 @@ var seedUsers = function () {
}
];

return User.create(users);
var creatingUsers = users.map(function (userObj) {
return User.create(userObj);
});

return Promise.all(creatingUsers);

};

connectToDb
.then(function () {
return wipeCollections();
})
db.sync({ force: true })
.then(function () {
return seedUsers();
})
Expand Down
22 changes: 11 additions & 11 deletions generated/server/app/configure/authentication/facebook.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var mongoose = require('mongoose');
var UserModel = mongoose.model('User');

module.exports = function (app) {
module.exports = function (app, db) {

var User = db.define('user');

var facebookConfig = app.getValue('env').FACEBOOK;

Expand All @@ -16,19 +16,19 @@ module.exports = function (app) {

var verifyCallback = function (accessToken, refreshToken, profile, done) {

UserModel.findOne({ 'facebook.id': profile.id }).exec()
User.findOne({
where: {
facebook_id: profile.id
}
})
.then(function (user) {

if (user) {
return user;
} else {
return UserModel.create({
facebook: {
id: profile.id
}
return User.create({
facebook_id: profile.id
});
}

})
.then(function (userToLogin) {
done(null, userToLogin);
Expand All @@ -45,7 +45,7 @@ module.exports = function (app) {
app.get('/auth/facebook', passport.authenticate('facebook'));

app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/login' }),
passport.authenticate('facebook', {failureRedirect: '/login'}),
function (req, res) {
res.redirect('/');
});
Expand Down
22 changes: 11 additions & 11 deletions generated/server/app/configure/authentication/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var mongoose = require('mongoose');
var UserModel = mongoose.model('User');

module.exports = function (app) {
module.exports = function (app, db) {

var User = db.model('user');

var googleConfig = app.getValue('env').GOOGLE;

Expand All @@ -17,19 +17,19 @@ module.exports = function (app) {

var verifyCallback = function (accessToken, refreshToken, profile, done) {

UserModel.findOne({ 'google.id': profile.id }).exec()
User.findOne({
where: {
google_id: profile.id
}
})
.then(function (user) {

if (user) {
return user;
} else {
return UserModel.create({
google: {
id: profile.id
}
return User.create({
google_id: profile.id
});
}

})
.then(function (userToLogin) {
done(null, userToLogin);
Expand All @@ -51,7 +51,7 @@ module.exports = function (app) {
}));

app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
passport.authenticate('google', {failureRedirect: '/login'}),
function (req, res) {
res.redirect('/');
});
Expand Down
26 changes: 18 additions & 8 deletions generated/server/app/configure/authentication/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';
var path = require('path');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var passport = require('passport');
var path = require('path');
var mongoose = require('mongoose');
var UserModel = mongoose.model('User');
var SequelizeStore = require('connect-session-sequelize')(session.Store);

var ENABLED_AUTH_STRATEGIES = [
'local',
Expand All @@ -13,14 +11,22 @@ var ENABLED_AUTH_STRATEGIES = [
//'google'
];

module.exports = function (app) {
module.exports = function (app, db) {

var dbStore = new SequelizeStore({
db: db
});

var User = db.model('user');

dbStore.sync();

// First, our session middleware will set/read sessions from the request.
// Our sessions will get stored in Mongo using the same connection from
// mongoose. Check out the sessions collection in your MongoCLI.
app.use(session({
secret: app.getValue('env').SESSION_SECRET,
store: new MongoStore({mongooseConnection: mongoose.connection}),
store: dbStore,
resave: false,
saveUninitialized: false
}));
Expand All @@ -38,7 +44,11 @@ module.exports = function (app) {
// When we receive a cookie from the browser, we use that id to set our req.user
// to a user found in the database.
passport.deserializeUser(function (id, done) {
UserModel.findById(id, done);
User.findById(id)
.then(function (user) {
done(null, user);
})
.catch(done);
});

// We provide a simple GET /session in order to get session information directly.
Expand All @@ -60,7 +70,7 @@ module.exports = function (app) {

// Each strategy enabled gets registered.
ENABLED_AUTH_STRATEGIES.forEach(function (strategyName) {
require(path.join(__dirname, strategyName))(app);
require(path.join(__dirname, strategyName))(app, db);
});

};
14 changes: 9 additions & 5 deletions generated/server/app/configure/authentication/local.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
'use strict';
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongoose = require('mongoose');
var User = mongoose.model('User');

module.exports = function (app) {
module.exports = function (app, db) {

var User = db.model('user');

// When passport.authenticate('local') is used, this function will receive
// the email and password to run the actual authentication logic.
var strategyFn = function (email, password, done) {
User.findOne({ email: email })
User.findOne({
where: {
email: email
}
})
.then(function (user) {
// user.correctPassword is a method from the User schema.
if (!user || !user.correctPassword(password)) {
Expand All @@ -22,7 +26,7 @@ module.exports = function (app) {
.catch(done);
};

passport.use(new LocalStrategy({ usernameField: 'email', passwordField: 'password' }, strategyFn));
passport.use(new LocalStrategy({usernameField: 'email', passwordField: 'password'}, strategyFn));

// A POST /login route is created to handle login.
app.post('/login', function (req, res, next) {
Expand Down
33 changes: 11 additions & 22 deletions generated/server/app/configure/authentication/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

var passport = require('passport');
var TwitterStrategy = require('passport-twitter').Strategy;
var mongoose = require('mongoose');
var UserModel = mongoose.model('User');

module.exports = function (app) {
module.exports = function (app, db) {

var User = db.model('user');

var twitterConfig = app.getValue('env').TWITTER;

Expand All @@ -16,32 +16,21 @@ module.exports = function (app) {
};

var createNewUser = function (token, tokenSecret, profile) {
return UserModel.create({
twitter: {
id: profile.id,
username: profile.username,
token: token,
tokenSecret: tokenSecret
}
return User.create({
twitter_id: profile.id
});
};

var updateUserCredentials = function (user, token, tokenSecret, profile) {

user.twitter.token = token;
user.twitter.tokenSecret = tokenSecret;
user.twitter.username = profile.username;

return user.save();

};

var verifyCallback = function (token, tokenSecret, profile, done) {

UserModel.findOne({'twitter.id': profile.id}).exec()
UserModel.findOne({
where: {
twitter_id: profile.id
}
}).exec()
.then(function (user) {
if (user) { // If a user with this twitter id already exists.
return updateUserCredentials(user, token, tokenSecret, profile);
return user;
} else { // If this twitter id has never been seen before and no user is attached.
return createNewUser(token, tokenSecret, profile);
}
Expand Down
6 changes: 3 additions & 3 deletions generated/server/app/configure/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
module.exports = function (app) {
module.exports = function (app, db) {

// setValue and getValue are merely alias
// for app.set and app.get used in the less
Expand All @@ -18,6 +18,6 @@ module.exports = function (app) {
// variable inside of server/app/configure/app-variables.js
app.use(app.getValue('log'));

require('./authentication')(app);
require('./authentication')(app, db);

};
};
Loading

0 comments on commit 9f7aeb8

Please sign in to comment.