add a lot of server side libraries, preemptively, still hooking everything up
This commit is contained in:
parent
dfe54dd784
commit
47491f63ba
6 changed files with 147 additions and 1 deletions
|
@ -34,12 +34,17 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"babel-core": "^6.8.0",
|
||||
"bcrypt-nodejs": "0.0.3",
|
||||
"body-parser": "^1.15.1",
|
||||
"classnames": "^2.2.5",
|
||||
"codemirror": "^5.14.2",
|
||||
"connect-mongo": "^1.2.0",
|
||||
"cookie-parser": "^1.4.1",
|
||||
"express": "^4.13.4",
|
||||
"mongoose": "^4.4.16",
|
||||
"passport": "^0.3.2",
|
||||
"passport-github": "^1.1.0",
|
||||
"passport-local": "^1.0.0",
|
||||
"react": "^15.0.2",
|
||||
"react-dom": "^15.0.2",
|
||||
"react-inlinesvg": "^0.4.2",
|
||||
|
|
89
server/config/passport.js
Normal file
89
server/config/passport.js
Normal file
|
@ -0,0 +1,89 @@
|
|||
const passport = require('passport');
|
||||
const GitHubStrategy = require('passport-github').Strategy;
|
||||
const LocalStrategy = require('passport-local').Strategy;
|
||||
|
||||
const User = require('../models/user');
|
||||
|
||||
passport.serializeUser((user, done) => {
|
||||
done(null, user.id);
|
||||
});
|
||||
|
||||
passport.deserializeUser((id, done) => {
|
||||
User.findById(id, (err, user) => {
|
||||
done(err, user);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Sign in using Email and Password.
|
||||
*/
|
||||
passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
|
||||
User.findOne({ email: email.toLowerCase() }, (err, user) => {
|
||||
if (!user) {
|
||||
return done(null, false, { msg: `Email ${email} not found.` });
|
||||
}
|
||||
user.comparePassword(password, (err, isMatch) => {
|
||||
if (isMatch) {
|
||||
return done(null, user);
|
||||
}
|
||||
return done(null, false, { msg: 'Invalid email or password.' });
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
/**
|
||||
* Sign in with GitHub.
|
||||
*/
|
||||
//TODO add dotenv so I can add github login
|
||||
// passport.use(new GitHubStrategy({
|
||||
// clientID: process.env.GITHUB_ID,
|
||||
// clientSecret: process.env.GITHUB_SECRET,
|
||||
// callbackURL: '/auth/github/callback',
|
||||
// passReqToCallback: true
|
||||
// }, (req, accessToken, refreshToken, profile, done) => {
|
||||
// if (req.user) {
|
||||
// User.findOne({ github: profile.id }, (err, existingUser) => {
|
||||
// if (existingUser) {
|
||||
// req.flash('errors', { msg: 'There is already a GitHub account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
|
||||
// done(err);
|
||||
// } else {
|
||||
// User.findById(req.user.id, (err, user) => {
|
||||
// user.github = profile.id;
|
||||
// user.tokens.push({ kind: 'github', accessToken });
|
||||
// user.profile.name = user.profile.name || profile.displayName;
|
||||
// user.profile.picture = user.profile.picture || profile._json.avatar_url;
|
||||
// user.profile.location = user.profile.location || profile._json.location;
|
||||
// user.profile.website = user.profile.website || profile._json.blog;
|
||||
// user.save((err) => {
|
||||
// req.flash('info', { msg: 'GitHub account has been linked.' });
|
||||
// done(err, user);
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
// } else {
|
||||
// User.findOne({ github: profile.id }, (err, existingUser) => {
|
||||
// if (existingUser) {
|
||||
// return done(null, existingUser);
|
||||
// }
|
||||
// User.findOne({ email: profile._json.email }, (err, existingEmailUser) => {
|
||||
// if (existingEmailUser) {
|
||||
// req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with GitHub manually from Account Settings.' });
|
||||
// done(err);
|
||||
// } else {
|
||||
// const user = new User();
|
||||
// user.email = profile._json.email;
|
||||
// user.github = profile.id;
|
||||
// user.tokens.push({ kind: 'github', accessToken });
|
||||
// user.profile.name = profile.displayName;
|
||||
// user.profile.picture = profile._json.avatar_url;
|
||||
// user.profile.location = profile._json.location;
|
||||
// user.profile.website = profile._json.blog;
|
||||
// user.save((err) => {
|
||||
// done(err, user);
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
// }));
|
|
@ -5,7 +5,35 @@ const userSchema = new Schema({
|
|||
name: { type: 'String' },
|
||||
username: { type: 'String', required: true, unique: true},
|
||||
password: { type: 'String' },
|
||||
github: { type: 'String' },
|
||||
email: { type: 'String', unique: true },
|
||||
tokens: Array,
|
||||
admin: { type: Boolean, default: false }
|
||||
}, {timestamps: true});
|
||||
|
||||
/**
|
||||
* Password hash middleware.
|
||||
*/
|
||||
userSchema.pre('save', function (next) {
|
||||
const user = this;
|
||||
if (!user.isModified('password')) { return next(); }
|
||||
bcrypt.genSalt(10, (err, salt) => {
|
||||
if (err) { return next(err); }
|
||||
bcrypt.hash(user.password, salt, null, (err, hash) => {
|
||||
if (err) { return next(err); }
|
||||
user.password = hash;
|
||||
next();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Helper method for validating user's password.
|
||||
*/
|
||||
userSchema.methods.comparePassword = function (candidatePassword, cb) {
|
||||
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
|
||||
cb(err, isMatch);
|
||||
});
|
||||
};
|
||||
|
||||
export default mongoose.model('User', userSchema);
|
|
@ -4,4 +4,6 @@ const router = new Router();
|
|||
|
||||
router.route('/login').get(SessionController.newSession);
|
||||
|
||||
router.route('/login').post(SessionController.createSession);
|
||||
|
||||
router.route('/logout').get(SessionController.destroySession);
|
|
@ -0,0 +1,7 @@
|
|||
import { Router } from 'express';
|
||||
import * as UserController from '../controllers/user.controller';
|
||||
const router = new Router();
|
||||
|
||||
router.route('/signup').get(UserController.newUser);
|
||||
|
||||
router.route('/signup').post(UserController.createUser);
|
|
@ -23,14 +23,29 @@ app.use(webpackHotMiddleware(compiler));
|
|||
import serverConfig from './config';
|
||||
|
||||
//Body parser, cookie parser, sessions, serve public assets
|
||||
const MongoStore = require('connect-mongo')(session);
|
||||
|
||||
app.use(Express.static(path.resolve(__dirname, '../static')));
|
||||
app.use(bodyParser.urlencoded({extended: true}));
|
||||
app.use(bodyParser.json());
|
||||
app.use(cookieParser());
|
||||
app.use(session({secret: 'steve brule'}));
|
||||
app.use(session({
|
||||
//this should be SECRET AND IN A SECRET FILE
|
||||
//TODO add dotenv
|
||||
secret: 'steve brule',
|
||||
resave: true,
|
||||
saveUninitialized: true,
|
||||
store: new MongoStore({
|
||||
// url: process.env.MONGODB_URI || process.env.MONGOLAB_URI,
|
||||
url: serverConfig.mongoURL,
|
||||
autoReconnect: true
|
||||
})
|
||||
}));
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
|
||||
const passportConfig = require('./config/passport');
|
||||
|
||||
app.get("/", function(req, res) {
|
||||
res.sendFile(path.resolve(__dirname + '/../index.html'));
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue