@@ -5,36 +5,114 @@ const UserModel = require('../model/user.model');
55// import bcrypt
66const bcrypt = require ( "bcryptjs" ) ;
77
8+ const jwt = require ( 'jsonwebtoken' ) ;
9+ const authParser = require ( '../middleware/middleware_auth.middleware' ) ;
810
9- router . post ( '/' , ( req , res ) => {
10- if ( ! req . body . username || ! req . body . password ) {
11+
12+
13+ router . post ( '/' , async ( req , res ) => {
14+ const { username, password} = req . body ;
15+ if ( ! username || ! password ) {
1116 return res . status ( 404 ) . send ( { message : "Must include username AND password" } ) ;
1217 }
1318
14- // req.body.password = bcrypt.hashSync(req.body.password, 10);
15-
1619 return UserModel . addUser ( req . body )
1720 . then ( ( user ) => {
18- // console.dir(user);
19- return res . status ( 200 ) . send ( user )
21+ // COMMENT: I am leaving the JWT logic in so you can see the difference
22+ // Why do we not need to encode the username in session?
23+ // Why do we not need to set the cookie anymore?
24+ //
25+ // const payload = {username};
26+ // const token = jwt.sign(payload, process.env.SUPER_SECRET, {
27+ // expiresIn: '14d'
28+ // });
29+ req . session . username = username ;
30+ return res //.cookie('token', token, {httpOnly: true})
31+ . status ( 200 ) . send ( { username} ) ;
2032 } ,
2133 error => res . status ( 500 ) . send ( error ) ) ;
2234} ) ;
2335
2436router . post ( '/authenticate' , function ( req , res ) {
25- UserModel . getUserByUserName ( req . body . username )
37+ const { username, password} = req . body ;
38+ UserModel . getUserByUserName ( username )
2639 . then ( ( user ) => {
27- // Notice that we're not using bcrypt directly anywhere in the controller.
28- // All of that behavior is getting handled closer to the database level/layer
29- user . comparePassword ( req . body . password , ( error , match ) => {
40+ user . comparePassword ( password , ( error , match ) => {
3041 if ( match ) {
31- res . send ( user ) ;
42+ // Comment: This is the same as above!
43+ //
44+ // const payload = {username};
45+ // const token = jwt.sign(payload, process.env.SUPER_SECRET, {
46+ // expiresIn: '14d'
47+ // });
48+ req . session . username = username ;
49+ return res //.cookie('token', token, {httpOnly: true})
50+ . status ( 200 ) . send ( { username} ) ;
3251 }
3352 return res . status ( 400 ) . send ( "The password does not match" ) ;
3453 } ) ;
3554 } )
3655 . catch ( ( error ) => console . error ( `Something went wrong: ${ error } ` ) ) ;
37- } ) ;
56+ } )
57+ // router.post('/', (req, res) => {
58+ // if(!req.body.username || !req.body.password) {
59+ // return res.status(404).send({message: "Must include username AND password"});
60+ // }
61+ //
62+ // // req.body.password = bcrypt.hashSync(req.body.password, 10);
63+ //
64+ // return UserModel.addUser(req.body)
65+ // .then((user) => {
66+ // // console.dir(user);
67+ // const {username} = user;
68+ // const payload = {username};
69+ // // JWT is encrypting our payload (which is whatever data we want
70+ // // to carry across sessions: in this case, just the username)
71+ // // into the cookie based on our SECRET
72+ // const token = jwt.sign(payload, process.env.SUPER_SECRET, {
73+ // expiresIn: '14d' // optional cookie expiration date
74+ // });
75+ // // Here we are setting the cookie on our response obect.
76+ // // Note that we are returning the username, but that isn't as necessary anymore
77+ // // unless we want to reference that on the frontend
78+ // return res.cookie('token', token, {httpOnly: true})
79+ // .status(200).send({username});
80+ // // return res.status(200).send(user)
81+ // },
82+ // error => res.status(500).send(error));
83+ // });
84+
85+ router . get ( '/loggedIn' , authParser , function ( req , res ) {
86+ return res . sendStatus ( 200 ) ;
87+ } )
88+
89+ //...
90+
91+ // Can you figure out why authenticate is POST now?
92+ // router.post('/authenticate', function (req, res) {
93+ // const {username, password} = req.body;
94+ // UserModel.getUserByUserName(username)
95+ // .then((user) => {
96+ // user.comparePassword(password, (error, match) => {
97+ // if (match) {
98+ // const payload = {username};
99+ // // JWT is encrypting our payload (which is whatever data we want
100+ // // to carry across sessions: in this case, just the username)
101+ // // into the cookie based on our SECRET
102+ // const token = jwt.sign(payload, process.env.SUPER_SECRET, {
103+ // expiresIn: '14d' // optional cookie expiration date
104+ // });
105+ // // Here we are setting the cookie on our response obect.
106+ // // Note that we are returning the username, but that isn't as necessary anymore
107+ // // unless we want to reference that on the frontend
108+ // return res.cookie('token', token, {httpOnly: true})
109+ // .status(200).send({username});
110+ // }
111+ // return res.status(400).send("The password does not match");
112+ // });
113+ // })
114+ // .catch((error) => console.error(`Something went wrong: ${error}`));
115+ // });
38116
39117router . get ( '/' , ( req , res ) => UserModel . getAllUsers ( )
40118 . then ( users => res . send ( users ) ) ) ;
0 commit comments