Tuesday, 2 June 2020

Am I setting this up right?

Hey guys so I've built a simple API that has a registration and login route. (/api/user/register, and /api/user/login)Now when connecting to the front end web app.Should I be making another route (/login) that then does a post request towards /api/user/login for example?If so, what should I return from the api? I plan on doing sessions so return the sessionid to the /login route or something and then set it as a cookie?​API route:router.post('/login', async (request, response) => {// validate const {error} = loginValidation(request.body) if (error) return response.status(400).send(error.details\[0\].message) const {email, password} = request.body // check if email doesn't exist const user = await pool.query('SELECT id, email, password FROM users WHERE email = $1 LIMIT 1', \[email\]) if(user.rowCount == 0) return response.status(400).send('Wrong email or password') // password is correct; move on to validating password const id = user.rows\[0\].id const storedEmail = user.rows\[0\].email const storedPassword = user.rows\[0\].password const validPass = await [bcrypt.compare](https://bcrypt.compare)(request.body.password, storedPassword) if(!validPass) return response.status(400).send('Wrong email or password') // not sure what to return here....})/login​app.post("/login", (request, response) => {axios.post('http://3000/login').then(response => {request.session.userId = userIdresponse.redirect('/dashboard')}).catch(error => { console.log(error); });})Would it be something like this?

Submitted June 02, 2020 at 08:56AM by prgrmmr7

No comments:

Post a Comment