eletrotupi / tcc/ api/src/routes/users.js36300f0d1b099080ea7e30ec28c85263f2dcb5c1
824 B | 31 lines Raw
1
const express = require("express");
2
const bcrypt = require("bcryptjs");
3
const db = require("../db");
4
const router = express.Router();
5

6
// XXX: Needs translations
7
router.post("/", (req, res) => {
8
  const { email, password, first_name, last_name } = req.body;
9
  if (!email || !password || !first_name || !last_name)
10
    return res.status(400).json({ error: "Campos faltantes" });
11

12
  const hashed = bcrypt.hashSync(password, 10);
13

14
  try {
15
    const stmt = db.prepare(
16
      `
17
        INSERT INTO users (email, password, first_name, last_name)
18
        VALUES (?, ?, ?, ?)
19
      `
20
    );
21

22
    const result = stmt.run(email, hashed, first_name, last_name);
23

24
    res.status(201).json({ id: result.lastInsertRowid });
25
  } catch (err) {
26
    res.status(400).json({ error: "Email já cadastrado" });
27
  }
28
});
29

30
module.exports = router;
31