@@ -5,6 +5,7 @@ Copyright (c) 2019 - present AppSeed.us
55
66*/
77const express = require ( 'express' ) ;
8+ const Joi = require ( 'joi' ) ;
89// eslint-disable-next-line new-cap
910const router = express . Router ( ) ;
1011const bcrypt = require ( 'bcrypt-nodejs' ) ;
@@ -15,52 +16,32 @@ const ActiveSession = require('../models/activeSession');
1516const reqAuth = require ( '../config/safeRoutes' ) . reqAuth ;
1617const { smtpConf } = require ( '../config/config' ) ;
1718
18- // route /admin /users/
19+ // Route: <HOST>:PORT/api /users/
1920
20- router . post ( '/all' , reqAuth , function ( req , res ) {
21- User . find ( { } , function ( err , users ) {
22- if ( err ) {
23- res . json ( { success : false } ) ;
24- }
25- users = users . map ( function ( item ) {
26- const x = item ;
27- x . password = undefined ;
28- x . __v = undefined ;
29- return x ;
30- } ) ;
31- res . json ( { success : true , users : users } ) ;
32- } ) ;
21+ const userSchema = Joi . object ( ) . keys ( {
22+ email : Joi . string ( ) . email ( ) . required ( ) ,
23+ username : Joi . string ( ) . alphanum ( ) . min ( 4 ) . max ( 15 ) . optional ( ) ,
24+ password : Joi . string ( ) . required ( )
3325} ) ;
3426
35- router . post ( '/edit' , reqAuth , function ( req , res ) {
36- const { userID, username, email } = req . body ;
27+ router . post ( '/register' , ( req , res ) => {
3728
38- User . find ( { _id : userID } ) . then ( ( user ) => {
39- if ( user . length == 1 ) {
40- const query = { _id : user [ 0 ] . _id } ;
41- const newvalues = { $set : { username : username , email : email } } ;
42- User . updateOne ( query , newvalues , function ( err , cb ) {
43- if ( err ) {
44- // eslint-disable-next-line max-len
45- res . json ( { success : false , msg : 'There was an error. Please contract the administator' } ) ;
46- }
47- res . json ( { success : true } ) ;
48- } ) ;
49- } else {
50- res . json ( { success : false } ) ;
51- }
52- } ) ;
53- } ) ;
29+ // Joy Validation
30+ const result = userSchema . validate ( req . body ) ;
31+ if ( result . error ) {
32+ res . status ( 422 ) . json ( { success : false ,
33+ msg : 'Validation err: ' + result . error . details [ 0 ] . message } ) ;
34+ return ;
35+ }
5436
55- router . post ( '/register' , ( req , res ) => {
5637 const { username, email, password } = req . body ;
5738
5839 User . findOne ( { email : email } ) . then ( ( user ) => {
40+
5941 if ( user ) {
42+
6043 res . json ( { success : false , msg : 'Email already exists' } ) ;
61- //} else if (password.length < 6) {
62- // // eslint-disable-next-line max-len
63- // res.json({success: false, msg: 'Password must be at least 6 characters long'});
44+
6445 } else {
6546 bcrypt . genSalt ( 10 , ( err , salt ) => {
6647 bcrypt . hash ( password , salt , null , ( err , hash ) => {
@@ -83,6 +64,15 @@ router.post('/register', (req, res) => {
8364} ) ;
8465
8566router . post ( '/login' , ( req , res ) => {
67+
68+ // Joy Validation
69+ const result = userSchema . validate ( req . body ) ;
70+ if ( result . error ) {
71+ res . status ( 422 ) . json ( { success : false ,
72+ msg : 'Validation err: ' + result . error . details [ 0 ] . message } ) ;
73+ return ;
74+ }
75+
8676 const email = req . body . email ;
8777 const password = req . body . password ;
8878
@@ -116,10 +106,6 @@ router.post('/login', (req, res) => {
116106 } ) ;
117107} ) ;
118108
119- router . post ( '/checkSession' , reqAuth , function ( req , res ) {
120- res . json ( { success : true } ) ;
121- } ) ;
122-
123109router . post ( '/logout' , reqAuth , function ( req , res ) {
124110 const token = req . body . token ;
125111 ActiveSession . deleteMany ( { token : token } , function ( err , item ) {
@@ -130,4 +116,43 @@ router.post('/logout', reqAuth, function(req, res) {
130116 } ) ;
131117} ) ;
132118
119+ router . post ( '/checkSession' , reqAuth , function ( req , res ) {
120+ res . json ( { success : true } ) ;
121+ } ) ;
122+
123+ router . post ( '/all' , reqAuth , function ( req , res ) {
124+ User . find ( { } , function ( err , users ) {
125+ if ( err ) {
126+ res . json ( { success : false } ) ;
127+ }
128+ users = users . map ( function ( item ) {
129+ const x = item ;
130+ x . password = undefined ;
131+ x . __v = undefined ;
132+ return x ;
133+ } ) ;
134+ res . json ( { success : true , users : users } ) ;
135+ } ) ;
136+ } ) ;
137+
138+ router . post ( '/edit' , reqAuth , function ( req , res ) {
139+ const { userID, username, email } = req . body ;
140+
141+ User . find ( { _id : userID } ) . then ( ( user ) => {
142+ if ( user . length == 1 ) {
143+ const query = { _id : user [ 0 ] . _id } ;
144+ const newvalues = { $set : { username : username , email : email } } ;
145+ User . updateOne ( query , newvalues , function ( err , cb ) {
146+ if ( err ) {
147+ // eslint-disable-next-line max-len
148+ res . json ( { success : false , msg : 'There was an error. Please contract the administator' } ) ;
149+ }
150+ res . json ( { success : true } ) ;
151+ } ) ;
152+ } else {
153+ res . json ( { success : false } ) ;
154+ }
155+ } ) ;
156+ } ) ;
157+
133158module . exports = router ;
0 commit comments