From be2bf9d8c44cbb9f70e14132987999a1ac26a67f Mon Sep 17 00:00:00 2001 From: Mo BETTA Date: Tue, 13 Dec 2016 08:22:37 -0800 Subject: [PATCH] User registration --- README.md | 1 + models/users.js | 19 ++++++++++++------- package.json | 1 + src/authentication/authentication.js | 2 +- src/authentication/register.js | 13 ++++++++----- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 26a1538..dabd7ec 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ $ use lizardboard ``` MONGODB_URI=mongodb://localhost/lizardboard SECRET='Put Your Secret Here' +FRONTEND_SERVER='http://localhost:3000' ``` ###### Install all the things ``` diff --git a/models/users.js b/models/users.js index 819a72c..1eb284f 100644 --- a/models/users.js +++ b/models/users.js @@ -1,15 +1,20 @@ const mongoose = require( 'mongoose' ) const Schema = mongoose.Schema const bcrypt = require( 'bcrypt-nodejs' ) +const randtoken = require('rand-token') const UserSchema = new Schema({ - email: { type: String, required: true, unique: true }, - password: { type: String, required: true }, - token: { type: String }, - token_expires: { type: Date }, - created_at: Date, - updated_at: Date -}) + name: { type: String, required: true}, + email: { type: String, required: true, unique: true }, + password: { type: String, required: true }, + phone_number: { type: String }, + newsletter_subscribed: { type: Boolean, default: false }, + API_key: { type: String, default: () => { return randtoken.generate(32) }}, + company: { type: String }, + dragonboard_bar: { type: Boolean, default: true }, + active: { type: Boolean, default: true } +}, +{ timestamps: true }) UserSchema.pre( 'save', function( next ) { const user = this diff --git a/package.json b/package.json index 742c124..f68f29e 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "passport-jwt": "^2.2.1", "passport-local": "^1.0.0", "pug": "^2.0.0-beta6", + "rand-token": "^0.3.0", "react": "^15.3.2", "react-dom": "^15.3.2", "react-router": "^2.6.1" diff --git a/src/authentication/authentication.js b/src/authentication/authentication.js index 3957255..a273121 100644 --- a/src/authentication/authentication.js +++ b/src/authentication/authentication.js @@ -10,7 +10,7 @@ const generateToken = user => { const userInfo = user => ({ email: user.email, - _id: user._id + password: user.password }) const tokenInfo = user => { diff --git a/src/authentication/register.js b/src/authentication/register.js index 34cf743..018de9f 100644 --- a/src/authentication/register.js +++ b/src/authentication/register.js @@ -6,7 +6,9 @@ const { generateToken, userInfo, tokenInfo } = require( './authentication' ) const ERROR_MESSAGE = 'Cannot authenticate request' exports.register = ( request, response, next ) => { - const { email, password } = request.body + const { + name, email, password, phone_number, newsletter_subscribed, company + } = request.body if( !email ) { return response.status( 422 ).send({ error: ERROR_MESSAGE }) @@ -21,14 +23,15 @@ exports.register = ( request, response, next ) => { return response.status( 422 ).send({ error: ERROR_MESSAGE }) } - const user = new User({ email, password }) + const user = new User({ + name, email, password, phone_number, newsletter_subscribed, company + }) user.save(( err, user ) => { if( err ) { return next( err ) } - - return response.status( 201 ).json( tokenInfo( user )) + response.redirect( process.env.FRONTEND_SERVER + '/dashboard' ) }) }) -} \ No newline at end of file +}