1- const { body, validationResult} = require ( "express-validator" ) ;
1+ const { body, validationResult } = require ( "express-validator" ) ;
22const User = require ( "../models/User" )
33const bcrypt = require ( "bcrypt" ) ;
44const jwt = require ( "jsonwebtoken" ) ;
5+ require ( 'dotenv' ) . config ( )
56
67
78
@@ -12,50 +13,48 @@ const jwt = require("jsonwebtoken");
1213module . exports . registerValidations = [
1314 body ( "name" ) . not ( ) . isEmpty ( ) . trim ( ) . withMessage ( "Name is Required" ) ,
1415 body ( "email" ) . not ( ) . isEmpty ( ) . trim ( ) . withMessage ( "Email is Required" ) ,
15- body ( "password" ) . isLength ( { min : 8 } ) . withMessage ( 'Password must be eight character Long' )
16+ body ( "password" ) . isLength ( { min : 8 } ) . withMessage ( 'Password must be eight character Long' ) ,
1617] ;
1718// trim() extra spaces ko remove karega
1819
1920
2021// ye yaha export hraha hy our routes/userroutes may imort hoga
21- module . exports . register = async ( req , res ) => {
22- // res.json(req.body);
23- const { name, email, password} = req . body ;
24- // res.send(password);
25- const errors = validationResult ( req ) ;
26- if ( ! errors . isEmpty ( ) )
27- {
28- res . status ( 400 ) . json ( { errors : errors . array ( ) } )
29- }
30- // else{
31- // res.json("You have done! thankYou")
32- // }
33-
34- try {
35- const checkUser = await User . findOne ( { email} )
36- if ( checkUser ) {
37- return res . status ( 400 ) . json ( { errors : [ { msg : 'This Email Is Already Exist' } ] } ) ;
38- }
39-
40- const salt = await bcrypt . genSalt ( 10 ) ;
41- // hash password
42- const hash = await bcrypt . hash ( password , salt ) ;
43- try {
44- const user = await User . create ( {
45- name,
46- email,
47- password : hash ,
48- } )
49-
50- } catch ( error ) {
51- return res . status ( 500 ) . json ( { errors : error } ) ;
52- }
53-
54-
55- }
56-
57- catch ( error ) {
58- return res . status ( 500 ) . json ( { errors : error } ) ;
59- }
60- // 5000 server internal error
22+ module . exports . register = async ( req , res ) => {
23+ const { name, email, password } = req . body ;
24+ const errors = validationResult ( req ) ;
25+ if ( ! errors . isEmpty ( ) ) {
26+ return res . status ( 400 ) . json ( { errors : errors . array ( ) } ) ;
27+ }
28+ try {
29+ const checkUser = await User . findOne ( { email } ) ;
30+ if ( checkUser ) {
31+ return res
32+ . status ( 400 )
33+ . json ( { errors : [ { msg : 'Email is already taken' } ] } ) ;
34+ }
35+
36+ // Hash password
37+ const salt = await bcrypt . genSalt ( 10 ) ;
38+ const hash = await bcrypt . hash ( password , salt ) ;
39+ try {
40+ const user = await User . create ( {
41+ name,
42+ email,
43+ password : hash ,
44+ } ) ;
45+ const token = createToken ( user ) ;
46+ return res
47+ . status ( 200 )
48+ . json ( { msg : 'Your account has been created' , token } ) ;
49+ } catch ( error ) {
50+ return res . status ( 500 ) . json ( { errors : "error h" } ) ;
51+ }
52+
53+
54+ }
55+
56+ catch ( error ) {
57+ return res . status ( 500 ) . json ( { errors : "one" } ) ;
58+ }
59+ // 5000 server internal error
6160}
0 commit comments