1
+ const { AsyncRouter } = require ( "express-async-router" ) ;
2
+ const jwt = require ( "jsonwebtoken" ) ;
3
+ const { check, validationResult } = require ( "express-validator" ) ;
4
+
5
+ const User = require ( "../models/User" ) ;
6
+ const handleValidationErrors = require ( "../helpers/handleValidationErrors" ) ;
7
+
8
+ const router = AsyncRouter ( ) ;
9
+
10
+ const signUpValidators = [
11
+ check ( "email" ) . isEmail ( ) ,
12
+ check ( "password" ) . exists ( ) ,
13
+ check ( "passwordConfirm" ) . exists ( )
14
+ ] ;
15
+
16
+ const loginValidators = [
17
+ check ( "email" ) . isEmail ( ) ,
18
+ check ( "password" ) . exists ( )
19
+ ] ;
20
+
21
+ router . post (
22
+ "/sign-up" ,
23
+ [ ...signUpValidators , handleValidationErrors ] ,
24
+ async ( req , res ) => {
25
+ const userExists = await User . findOne ( { email : req . body . email } ) ;
26
+
27
+ if ( userExists )
28
+ return res . status ( 400 ) . send ( "E-mail already exists" ) ;
29
+ if ( req . body . password !== req . body . passwordConfirm )
30
+ return res . status ( 400 ) . send ( "Passwords do not match" ) ;
31
+
32
+ const user = await User . signUp ( req . body . email , req . body . password ) ;
33
+ res . status ( 201 ) . send ( user ) ;
34
+ }
35
+ ) ;
36
+
37
+ router . post (
38
+ "/login" ,
39
+ [ ...loginValidators , handleValidationErrors ] ,
40
+ async ( req , res ) => {
41
+
42
+ }
43
+ ) ;
44
+
45
+ module . exports = router ;
0 commit comments