2016-06-09 02:52:59 +02:00
|
|
|
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({
|
2016-06-09 22:28:21 +02:00
|
|
|
username: req.body.username,
|
2016-06-09 02:52:59 +02:00
|
|
|
email: req.body.email,
|
|
|
|
password: req.body.password
|
|
|
|
});
|
|
|
|
|
|
|
|
User.findOne({email: req.body.email}, (err, existingUser) => {
|
|
|
|
if (existingUser) {
|
2016-06-09 19:56:23 +02:00
|
|
|
return res.status(422).send({ error: 'Email is in use' });
|
2016-06-09 02:52:59 +02:00
|
|
|
}
|
|
|
|
user.save((err) => {
|
|
|
|
if (err) { return next(err); }
|
|
|
|
req.logIn(user, (err) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2016-06-14 20:46:40 +02:00
|
|
|
res.json({
|
|
|
|
email: req.user.email,
|
|
|
|
username: req.user.username
|
|
|
|
});
|
2016-06-09 02:52:59 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|