@@ -10,72 +10,97 @@ const router = express.Router();
1010
1111router . post ( '/signup' , ( req , res ) => {
1212 const body = _ . pick ( req . body , [ 'username' , 'email_id' , 'name' , 'college' , 'password' , 'dob' , 'gender' , 'city' , 'joinedOn' , 'bio' ] ) ;
13- User . findOne ( { 'username' :body . username } , function ( err , user ) {
13+ User . findOne ( { 'username' :body . username , 'email_id' : body . email_id } , function ( err , user ) {
1414 if ( err ) {
1515 res . status ( 404 ) . send ( err )
1616 rs } else if ( user ) {
17- res . status ( 404 ) . send ( 'User with Username Exists' )
17+ res . status ( 404 ) . json ( {
18+ 'success' :false ,
19+ 'msg' :'User with Username Exists'
20+ } )
1821 } else {
1922 var user = new User ( body ) ;
2023 user . save ( ) . then ( ( user ) => {
2124 var token = jwt . sign ( { username : user . username } , 'secret' , {
2225 expiresIn : 86400 // expires in 24 hours
2326 } ) ;
27+ < << << << HEAD
2428 res . json ( { success : true , message : 'Acount registered!' } ) ;
29+ = === ===
30+ res . status ( 200 ) . json ( {
31+ 'success' :true ,
32+ 'msg' :'Registration successful , Now you can Login' ,
33+ 'token' :token
34+ } )
35+ >>> > >>> 08e47 d897437c805e78a5f0730556facf6bb31a6
2536 } , ( e ) => {
26- res . status ( 400 ) . send ( e )
37+ res . status ( 400 ) . json ( {
38+ 'success' :false ,
39+ 'msg' :e
40+ } )
2741 } )
2842 }
2943 } )
3044
3145} ) ;
3246
33- router . get ( '/me' , VerifyToken , function ( req , res ) {
47+
48+
49+ router . get ( '/profile' , VerifyToken , function ( req , res ) {
50+
51+ User . findOne ( { username :req . username } , function ( err , user ) {
52+ if ( err ) return res . status ( 500 ) . send ( err ) ;
53+ if ( ! user ) return res . status ( 404 ) . send ( "No user found." ) ;
54+ res . status ( 200 ) . json ( {
55+ 'success' :true ,
56+ 'msg' :user
57+ } ) ;
58+ } ) ;
59+
60+ } ) ;
61+
62+
63+ router . get ( '/profile/me' , VerifyToken , function ( req , res ) {
3464
3565 User . findOne ( { username :req . username } , function ( err , user ) {
3666 if ( err ) return res . status ( 500 ) . send ( err ) ;
3767 if ( ! user ) return res . status ( 404 ) . send ( "No user found." ) ;
38- res . status ( 200 ) . send ( user ) ;
68+ res . status ( 200 ) . json ( {
69+ 'success' :true ,
70+ 'msg' :user ,
71+ 'abd' :'gdhd'
72+ } ) ;
3973 } ) ;
4074
4175} ) ;
4276
43- router . post ( '/login ' , ( req , res ) => {
77+ router . post ( '/signin ' , ( req , res ) => {
4478 const body = _ . pick ( req . body , [ 'username' , 'email_id' , 'password' ] ) ;
4579 console . log ( `***${ body . password } *** ${ body . username } ` ) ;
4680
4781 User . findByUsername ( body . username , body . password , ( err , user ) => {
4882 if ( err ) {
49- res . status ( 404 ) . send ( err )
83+ res . status ( 404 ) . json ( {
84+ 'success' :false ,
85+ 'msg' :err
86+ } )
5087 } else if ( ! user ) {
51- res . status ( 404 ) . send ( 'User Not Found' )
88+ res . status ( 404 ) . json ( {
89+ 'success' :false ,
90+ 'msg' :'User not found'
91+ } )
5292 } else if ( user ) {
5393 var token = jwt . sign ( { username : user . username } , 'secret' , {
5494 expiresIn : 86400 // expires in 24 hours
5595 } ) ;
56- res . send ( user )
96+ res . json ( {
97+ 'success' :true ,
98+ 'msg' :'User with Username Exists'
99+ } )
57100 }
58101 } )
59102} )
60103
61104
62- function isLoggedIn ( req , res , next ) {
63-
64- if ( req . isAuthenticated ( ) )
65- return next ( ) ;
66-
67- res . redirect ( '/' ) ;
68- }
69-
70- function requiresLogin ( req , res , next ) {
71- if ( req . session && req . session . userId ) {
72- return next ( ) ;
73- } else {
74- var err = new Error ( 'You must be logged in to view this page.' ) ;
75- err . status = 401 ;
76- return next ( err ) ;
77- }
78- }
79-
80105
81106module . exports = router ;
0 commit comments