diff --git a/back/api/auth.js b/back/api/auth.js index 0ee7828..4fa2558 100644 --- a/back/api/auth.js +++ b/back/api/auth.js @@ -8,11 +8,11 @@ const router = express.Router(); router.post('/login', async (req, res) => { const { username, password } = req.body; const connection = await getConnection(); - const users = await getUserByUsername(connection, username); + const user = await getUserByUsername(connection, username); connection.end(); - if (users[0]) { - if (users[0].password === sha256(password)) { - const token = jwt.sign({ id: users[0].id }, process.env.JWT_SECRET, { + if (user[0]) { + if (user[0].password === sha256(password)) { + const token = jwt.sign({ id: user[0].id }, process.env.JWT_SECRET, { expiresIn: 1000 * 60 * 60 * 24 * 7, }); return res.send({ token: token }); @@ -25,8 +25,8 @@ router.post('/register', async (req, res) => { const { username, password } = req.body; const connection = await getConnection(); - const use = await getUserByUsername(connection, username); - if (use[0]) { + const user = await getUserByUsername(connection, username); + if (user[0]) { connection.end(); return res.status(401).send({ error: 'Username already exists' }); } diff --git a/front/src/pages/Channel.tsx b/front/src/pages/Channel.tsx index 7799e0b..4fdd00d 100644 --- a/front/src/pages/Channel.tsx +++ b/front/src/pages/Channel.tsx @@ -99,6 +99,7 @@ export default function Channel() { {messages.slice(0, maxMessageToShown).map((message) => (
  • {message.username}: {message.content}

    +

    {new Date(message.date).toLocaleString()}

    {(user?.id === message.user_id || user?.id === channel.owner_id || user?.admin === 1) && ( )} diff --git a/front/src/pages/Register.tsx b/front/src/pages/Register.tsx index 31f7a9a..7ddb452 100644 --- a/front/src/pages/Register.tsx +++ b/front/src/pages/Register.tsx @@ -9,17 +9,18 @@ export default function Register () { function handleSubmit(e: React.FormEvent) { e.preventDefault(); - axios.post("/api/auth/register", { username, password }); - axios - .post("/api/auth/login", { username, password }) - .then((res) => { - localStorage.setItem("token", res.data.token); - navigate("/"); - }) - .catch((err) => { - alert(err.response.data.message); - } - ); + axios.post("/api/auth/register", { username, password }).then(() => { + axios + .post("/api/auth/login", { username, password }) + .then((res) => { + localStorage.setItem("token", res.data.token); + navigate("/"); + }) + .catch((err) => { + alert(err.response.data.message); + } + ); + }); } return (