@@ -7,44 +7,43 @@ import * as jwt from 'jsonwebtoken';
7
7
import { NextFunction } from 'connect' ;
8
8
9
9
import * as EmailValidator from 'email-validator' ;
10
+ import { config } from '../../../../config/config' ;
10
11
11
12
const router : Router = Router ( ) ;
12
13
13
14
async function generatePassword ( plainTextPassword : string ) : Promise < string > {
14
- //@TODO Use Bcrypt to Generated Salted Hashed Passwords
15
- return ''
15
+ const rounds = 10
16
+ const salt = await bcrypt . genSalt ( rounds )
17
+ return bcrypt . hash ( plainTextPassword , salt )
16
18
}
17
19
18
- async function comparePasswords ( plainTextPassword : string , hash : string ) : Promise < boolean > {
19
- //@TODO Use Bcrypt to Compare your password to your Salted Hashed Password
20
- return false
20
+ function comparePasswords ( plainTextPassword : string , hash : string ) : Promise < boolean > {
21
+ return bcrypt . compare ( plainTextPassword , hash )
21
22
}
22
23
23
24
function generateJWT ( user : User ) : string {
24
- //@TODO Use jwt to create a new JWT Payload containing
25
- return ''
25
+ return jwt . sign ( user , config . jwt . secret )
26
26
}
27
27
28
28
export function requireAuth ( req : Request , res : Response , next : NextFunction ) {
29
- return next ( ) ;
30
- // if (!req.headers || !req.headers.authorization){
31
- // return res.status(401).send({ message: 'No authorization headers.' });
32
- // }
29
+ if ( ! req . headers || ! req . headers . authorization ) {
30
+ return res . status ( 401 ) . send ( { message : 'No authorization headers.' } ) ;
31
+ }
33
32
34
33
35
- // const token_bearer = req.headers.authorization.split(' ');
36
- // if(token_bearer.length != 2){
37
- // return res.status(401).send({ message: 'Malformed token.' });
38
- // }
34
+ const token_bearer = req . headers . authorization . split ( ' ' ) ;
35
+ if ( token_bearer . length != 2 ) {
36
+ return res . status ( 401 ) . send ( { message : 'Malformed token.' } ) ;
37
+ }
39
38
40
- // const token = token_bearer[1];
41
-
42
- // return jwt.verify(token, "hello" , (err, decoded) => {
43
- // if (err) {
44
- // return res.status(500).send({ auth: false, message: 'Failed to authenticate.' });
45
- // }
46
- // return next();
47
- // });
39
+ const token = token_bearer [ 1 ] ;
40
+
41
+ return jwt . verify ( token , config . jwt . secret , ( err , decoded ) => {
42
+ if ( err ) {
43
+ return res . status ( 500 ) . send ( { auth : false , message : 'Failed to authenticate.' } ) ;
44
+ }
45
+ return next ( ) ;
46
+ } ) ;
48
47
}
49
48
50
49
router . get ( '/verification' ,
0 commit comments