Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit 90c651d

Browse files
authored
Merge pull request #2 from dcollioni/f-auth
auth password and jwt features
2 parents c652529 + 7b0cdf9 commit 90c651d

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

course-02/exercises/udacity-c2-restapi/src/config/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ export const config = {
1010
"aws_region": process.env.AWS_REGION,
1111
"aws_profile": process.env.AWS_PROFILE,
1212
"aws_media_bucket": process.env.AWS_MEDIA_BUCKET
13+
},
14+
"jwt": {
15+
"secret": process.env.JWT_SECRET
1316
}
1417
}

course-02/exercises/udacity-c2-restapi/src/controllers/v0/users/routes/auth.router.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,43 @@ import * as jwt from 'jsonwebtoken';
77
import { NextFunction } from 'connect';
88

99
import * as EmailValidator from 'email-validator';
10+
import { config } from '../../../../config/config';
1011

1112
const router: Router = Router();
1213

1314
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)
1618
}
1719

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)
2122
}
2223

2324
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)
2626
}
2727

2828
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+
}
3332

3433

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+
}
3938

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+
});
4847
}
4948

5049
router.get('/verification',

0 commit comments

Comments
 (0)