add sign up server side, still working on client side

This commit is contained in:
catarak 2016-06-09 16:28:21 -04:00
parent 1f75b4d390
commit fdd946b961
8 changed files with 85 additions and 8 deletions

View file

@ -33,6 +33,7 @@
"node": ">=4"
},
"dependencies": {
"axios": "^0.12.0",
"babel-core": "^6.8.0",
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.15.1",
@ -52,6 +53,7 @@
"react-dom": "^15.0.2",
"react-inlinesvg": "^0.4.2",
"react-redux": "^4.4.5",
"redux": "^3.5.2"
"redux": "^3.5.2",
"redux-form": "^5.2.5"
}
}

View file

@ -9,8 +9,8 @@ export function newUser(req, res) {
}
export function createUser(req, res, next) {
console.log("in create user");
const user = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password
});

View file

@ -1,5 +1,6 @@
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt-nodejs');
const userSchema = new Schema({
name: { type: String, default: '' },

12
server/utils/jwt.js Normal file
View file

@ -0,0 +1,12 @@
import jwt from 'jwt-simple'
exports.generateToken = function(user) {
const timestamp = new Date().getTime();
return jwt.encode({
_id: user._id,
email: user.email,
name: user.name,
username: user.username,
iat: timestamp }, process.env.JWT_SECRET);
}

View file

@ -1,16 +1,42 @@
import React from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import * as UserActions from '../../redux/actions/user'
import { reduxForm } from 'redux-form'
class SignupView extends React.Component {
render() {
const {fields: { username, email, password, confirmPassword }, handleSubmit} = this.props;
return (
<form onSubmit={this.handleSubmit()}>
<input type="text" placeholder="Username"/>
<input type="text" placeholder="Email"/>
<input type="password" placeholder="Password"/>
<form onSubmit={handleSubmit(this.props.signUpUser.bind(this))}>
<input type="text" placeholder="Username" {...username}/>
<input type="text" placeholder="Email" {...email}/>
<input type="password" placeholder="Password" {...password}/>
<input type="password" placeholder="Confirm Password" {...confirmPassword}/>
<input type="submit" value="Sign Up" />
</form>
)
}
}
export default SignupView;
function mapStateToProps(state) {
return {
user: state.user
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(UserActions, dispatch);
}
function validate(formProps) {
const errors = {};
return errors;
}
// export default connect(mapStateToProps, mapDispatchToProps)(SignupView);
export default reduxForm({
form: 'signup',
fields: ['username', 'email', 'password', 'passwordConfirm'],
validate
}, mapStateToProps, mapDispatchToProps)(SignupView);

View file

@ -0,0 +1,20 @@
import * as ActionTypes from '../constants/constants'
import { browserHistory } from 'react-router'
import axios from 'axios'
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000' : '/';
export function signUpUser(formValues) {
const request = axios.post(`${ROOT_URL}/signup`, formValues);
return function(dispatch) {
axios.post(`${ROOT_URL}/signup`, formValues)
.then(response => {
dispatch({ type: AUTH_USER });
localStorage.setItem('token', response.data.token);
browserHistory.push('/');
})
.catch(response => dispatch(authError(response.data.error)));
}
}

View file

@ -2,11 +2,15 @@ import { combineReducers } from 'redux'
import file from './files'
import ide from './ide'
import preferences from './preferences'
import user from './user'
import { reducer as form } from 'redux-form'
const rootReducer = combineReducers({
form,
file,
ide,
preferences
preferences,
user
})
export default rootReducer

View file

@ -0,0 +1,12 @@
import * as ActionTypes from '../constants/constants'
const user = (state = {}, action) => {
switch (action.type) {
case ActionTypes.AUTH_USER:
return { ...state, error: '', authenticated: true };
default:
return state;
}
}
export default user;