diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js index e69de29b..3eecaebe 100644 --- a/server/controllers/user.controller.js +++ b/server/controllers/user.controller.js @@ -0,0 +1,33 @@ +import User from '../models/user' +import passport from 'passport' +import path from 'path' + +export function newUser(req, res) { + //eventually, it would be cool to have some isomorphic rendering + res.sendFile(path.resolve(__dirname + '/../../index.html')); +} + +export function createUser(req, res, next) { + const user = new User({ + email: req.body.email, + password: req.body.password + }); + + User.findOne({email: req.body.email}, (err, existingUser) => { + if (existingUser) { + //error, already registered + //should probably redirect client side though? + return res.redirect('/signup'); + } + user.save((err) => { + if (err) { return next(err); } + req.logIn(user, (err) => { + if (err) { + return next(err); + } + res.redirect('/'); + }); + }); + }); + +} \ No newline at end of file diff --git a/server/models/user.js b/server/models/user.js index 921d33d6..e8aab518 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -2,11 +2,11 @@ import mongoose from 'mongoose'; const Schema = mongoose.Schema; 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 }, + name: { type: String, default: '' }, + 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}); diff --git a/server/routes/user.routes.js b/server/routes/user.routes.js index 7451bf89..761b0e04 100644 --- a/server/routes/user.routes.js +++ b/server/routes/user.routes.js @@ -4,4 +4,6 @@ const router = new Router(); router.route('/signup').get(UserController.newUser); -router.route('/signup').post(UserController.createUser); \ No newline at end of file +router.route('/signup').post(UserController.createUser); + +export default router; \ No newline at end of file diff --git a/server/server.js b/server/server.js index d503ba81..0dfc38e9 100644 --- a/server/server.js +++ b/server/server.js @@ -21,6 +21,7 @@ app.use(webpackHotMiddleware(compiler)); //Import all required modules import serverConfig from './config'; +import users from './routes/user.routes'; //Body parser, cookie parser, sessions, serve public assets const MongoStore = require('connect-mongo')(session); @@ -43,6 +44,7 @@ app.use(session({ })); app.use(passport.initialize()); app.use(passport.session()); +app.use('/', users); const passportConfig = require('./config/passport'); diff --git a/shared/containers/SignupView/SignupView.jsx b/shared/containers/SignupView/SignupView.jsx new file mode 100644 index 00000000..8fcc4a3b --- /dev/null +++ b/shared/containers/SignupView/SignupView.jsx @@ -0,0 +1,16 @@ +import React from 'react' + +class SignupView extends React.Component { + render() { + return ( +
+ + + + +
+ ) + } +} + +export default SignupView; diff --git a/shared/routes.js b/shared/routes.js index b536c4f6..6ff24e19 100644 --- a/shared/routes.js +++ b/shared/routes.js @@ -3,11 +3,13 @@ import React from 'react' import App from './containers/App' import IDEView from './containers/IDEView/IDEView' import LoginView from './containers/LoginView/LoginView' +import SignupView from './containers/SignupView/SignupView' const routes = ( + );