From fdd946b9610f31fab7a9cbe7bd14cef76f6d9262 Mon Sep 17 00:00:00 2001 From: catarak Date: Thu, 9 Jun 2016 16:28:21 -0400 Subject: [PATCH] add sign up server side, still working on client side --- package.json | 4 ++- server/controllers/user.controller.js | 2 +- server/models/user.js | 1 + server/utils/jwt.js | 12 +++++++ shared/containers/SignupView/SignupView.jsx | 36 ++++++++++++++++++--- shared/redux/actions/user.js | 20 ++++++++++++ shared/redux/reducers/index.js | 6 +++- shared/redux/reducers/user.js | 12 +++++++ 8 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 server/utils/jwt.js create mode 100644 shared/redux/actions/user.js create mode 100644 shared/redux/reducers/user.js diff --git a/package.json b/package.json index fc0599ec..49b3768b 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js index 35e892ae..c6de9d32 100644 --- a/server/controllers/user.controller.js +++ b/server/controllers/user.controller.js @@ -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 }); diff --git a/server/models/user.js b/server/models/user.js index e8aab518..45218218 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -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: '' }, diff --git a/server/utils/jwt.js b/server/utils/jwt.js new file mode 100644 index 00000000..92962970 --- /dev/null +++ b/server/utils/jwt.js @@ -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); +} + diff --git a/shared/containers/SignupView/SignupView.jsx b/shared/containers/SignupView/SignupView.jsx index fa754b19..e1f92f90 100644 --- a/shared/containers/SignupView/SignupView.jsx +++ b/shared/containers/SignupView/SignupView.jsx @@ -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 ( -
- - - + + + + +
) } } -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); diff --git a/shared/redux/actions/user.js b/shared/redux/actions/user.js new file mode 100644 index 00000000..5d8a48cb --- /dev/null +++ b/shared/redux/actions/user.js @@ -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))); + } +} \ No newline at end of file diff --git a/shared/redux/reducers/index.js b/shared/redux/reducers/index.js index 0de1a613..d1b6df6b 100644 --- a/shared/redux/reducers/index.js +++ b/shared/redux/reducers/index.js @@ -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 diff --git a/shared/redux/reducers/user.js b/shared/redux/reducers/user.js new file mode 100644 index 00000000..ac735c6d --- /dev/null +++ b/shared/redux/reducers/user.js @@ -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; \ No newline at end of file