You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What is the difference between Registration, Log-in, and Authentication?
0. Review and Refactor
GET /robots
GET /users/:id
How do we NOT send back the password in the response?
What happens if we request a user that does not exist?
// Refactor response to NOT send back the passwordstaticasyncgetSingleUserFromDB(userId){constquery=awaitpool.query("SELECT id, username, bio FROM users WHERE id = $1",[userId])returnquery.rows[0]}
// Refactor code to return a 404 if the user doesn't exist in our DBasyncfunctiongetSingleUser(req,res){constuserId=req.params.idconstuser=awaitUserModel.getSingleUserFromDB(userId)//asyncif(user){constusersRobots=awaitUserModel.getUsersRobotsFromDB(userId)res.send({user, usersRobots})}else{res.sendStatus(404)}}
1. Building out Registration
We are going to build out the route POST /users/register.
What should be in the body of the request?
What should our server respond with if the registration was successful?
What should our server respond with if the registration was unsuccessful?
We are going to build out the route POST /users/login.
Why a POST and not a GET request?
What should be in the body of the request?
What should our server respond with if the login was successful?
What should our server respond with if the login was unsuccessful?
//userRouter.jsrouter.post('/login',loginUser)
//userController.jsasyncfunctionloginUser(req,res){let{username, password}=req.bodyconstfoundUser=awaitUserModel.loginUser(username,password)if(foundUser){res.send(foundUser)}else{res.status(401).send("invalid username or password")}}
//userModel.jsstaticasyncloginUser(username,password){letquery=awaitpool.query("SELECT * FROM users WHERE username = $1 AND password = $2",[username,password])returnquery.rows[0]}