@@ -4,32 +4,72 @@ const bcrypt = require("bcrypt");
4
4
const jwt = require ( "jsonwebtoken" ) ;
5
5
const SECRET = process . env . SECRET ;
6
6
7
+ const auth = ( req , res ) => {
8
+ const authHeader = req . get ( "Authorization" ) ;
9
+ if ( ! authHeader ) {
10
+ return res . status ( 401 ) . send ( "You need to include headers." ) ;
11
+ }
12
+ const token = authHeader . split ( " " ) [ 1 ] ;
13
+ return token ;
14
+ } ;
15
+
7
16
const allAdmnistrators = ( req , res ) => {
8
- projectAdmModel . find ( ( err , administrators ) => {
17
+ const token = auth ( req , res ) ;
18
+ jwt . verify ( token , SECRET , ( err ) => {
9
19
if ( err ) {
10
- return res . status ( 424 ) . send ( { message : err . message } ) ;
20
+ return res . status ( 403 ) . send ( "Invalid token!" ) ;
11
21
}
12
- res . status ( 200 ) . send ( administrators ) ;
22
+ projectAdmModel . find ( ( err , administrators ) => {
23
+ if ( err ) {
24
+ return res . status ( 424 ) . send ( { message : err . message } ) ;
25
+ }
26
+ res . status ( 200 ) . send ( administrators ) ;
27
+ } ) ;
13
28
} ) ;
14
29
} ;
15
30
16
31
const createAdministrator = ( req , res ) => {
17
- const encryptedPassword = bcrypt . hashSync ( req . body . password , 10 ) ;
18
- req . body . password = encryptedPassword ;
19
- const newAdm = new projectAdmModel ( req . body ) ;
20
-
21
- newAdm . save ( ( err ) => {
32
+ const token = auth ( req , res ) ;
33
+ jwt . verify ( token , SECRET , ( err ) => {
22
34
if ( err ) {
23
- return res . status ( 424 ) . send ( { message : err . message } ) ;
35
+ return res . status ( 403 ) . send ( "Invalid token!" ) ;
24
36
}
25
- res . status ( 201 ) . send ( {
26
- message : "Administrator successfully registered!" ,
27
- administrator : newAdm ,
37
+ projectAdmModel . findOne ( { email : req . body . email } , ( email ) => {
38
+ if ( email ) {
39
+ res . status ( 409 ) . send ( "Administrator already registered. Go to login!" ) ;
40
+ }
41
+ const encryptedPassword = bcrypt . hashSync ( req . body . password , 10 ) ;
42
+ req . body . password = encryptedPassword ;
43
+ const newAdm = new projectAdmModel ( req . body ) ;
44
+ newAdm . save ( ( err ) => {
45
+ if ( err ) {
46
+ return res . status ( 424 ) . send ( { message : err . message } ) ;
47
+ }
48
+ res . status ( 201 ) . send ( {
49
+ message : "Administrator successfully registered!" ,
50
+ administrator : newAdm ,
51
+ } ) ;
52
+ } ) ;
28
53
} ) ;
29
54
} ) ;
30
55
} ;
31
56
32
- const login = ( req , res ) => { } ;
57
+ const login = ( req , res ) => {
58
+ projectAdmModel . findOne ( { email : req . body . email } , ( err , administrator ) => {
59
+ if ( ! administrator ) {
60
+ return res . status ( 404 ) . send ( `No administrator registered with email ${ req . body . email } .` ) ;
61
+ }
62
+ const validPassword = bcrypt . compareSync (
63
+ req . body . password ,
64
+ administrator . password
65
+ ) ;
66
+ if ( ! validPassword ) {
67
+ return res . status ( 401 ) . send ( "Invalid password!" ) ;
68
+ }
69
+ const token = jwt . sign ( { email : req . body . email } , SECRET ) ;
70
+ return res . status ( 200 ) . send ( token ) ;
71
+ } ) ;
72
+ } ;
33
73
34
74
const updateAdministrator = ( req , res ) => { } ;
35
75
0 commit comments