add sign up server side, still working on client side
This commit is contained in:
parent
1f75b4d390
commit
fdd946b961
8 changed files with 85 additions and 8 deletions
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
});
|
||||
|
|
|
@ -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
12
server/utils/jwt.js
Normal 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);
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
20
shared/redux/actions/user.js
Normal file
20
shared/redux/actions/user.js
Normal 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)));
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
12
shared/redux/reducers/user.js
Normal file
12
shared/redux/reducers/user.js
Normal 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;
|
Loading…
Reference in a new issue