Skip to content

Instantly share code, notes, and snippets.

@kilgarenone
Created October 13, 2019 03:53
Show Gist options
  • Save kilgarenone/dc521c015b14ee8205b4232fbdd2baf4 to your computer and use it in GitHub Desktop.
Save kilgarenone/dc521c015b14ee8205b4232fbdd2baf4 to your computer and use it in GitHub Desktop.
store user data to user session
const bcrypt = require("bcryptjs");
router.post("/register", async (req, res) => {
const { fullName, email, password } = req.body;
const hashedPassword = await bcrypt.hash(password, 8);
const query = `INSERT INTO account(fullname, email, password) VALUES ($1, $2, $3) RETURNING user_id, fullname, email`;
const values = [fullName, email, hashedPassword];
const { rows } = await db.query(query, values);
// STORING STUFF TO 'req.session' TO TELL US THIS USER STILL LOGGEDIN
req.session.isLoggedIn = true;
// AND WE STORE USER PROFILE DATA TOO
req.session.user = rows[0];
res.status(200).send(rows[0]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment