fix: fixed register page

This commit is contained in:
Lukian 2025-03-25 14:20:55 +01:00
parent 90a6b6fa63
commit 414be922bc
3 changed files with 19 additions and 17 deletions

View file

@ -8,11 +8,11 @@ const router = express.Router();
router.post('/login', async (req, res) => { router.post('/login', async (req, res) => {
const { username, password } = req.body; const { username, password } = req.body;
const connection = await getConnection(); const connection = await getConnection();
const users = await getUserByUsername(connection, username); const user = await getUserByUsername(connection, username);
connection.end(); connection.end();
if (users[0]) { if (user[0]) {
if (users[0].password === sha256(password)) { if (user[0].password === sha256(password)) {
const token = jwt.sign({ id: users[0].id }, process.env.JWT_SECRET, { const token = jwt.sign({ id: user[0].id }, process.env.JWT_SECRET, {
expiresIn: 1000 * 60 * 60 * 24 * 7, expiresIn: 1000 * 60 * 60 * 24 * 7,
}); });
return res.send({ token: token }); return res.send({ token: token });
@ -25,8 +25,8 @@ router.post('/register', async (req, res) => {
const { username, password } = req.body; const { username, password } = req.body;
const connection = await getConnection(); const connection = await getConnection();
const use = await getUserByUsername(connection, username); const user = await getUserByUsername(connection, username);
if (use[0]) { if (user[0]) {
connection.end(); connection.end();
return res.status(401).send({ error: 'Username already exists' }); return res.status(401).send({ error: 'Username already exists' });
} }

View file

@ -99,6 +99,7 @@ export default function Channel() {
{messages.slice(0, maxMessageToShown).map((message) => ( {messages.slice(0, maxMessageToShown).map((message) => (
<li key={message.id}> <li key={message.id}>
<p>{message.username}: {message.content}</p> <p>{message.username}: {message.content}</p>
<p>{new Date(message.date).toLocaleString()}</p>
{(user?.id === message.user_id || user?.id === channel.owner_id || user?.admin === 1) && ( {(user?.id === message.user_id || user?.id === channel.owner_id || user?.admin === 1) && (
<button onClick={() => {deleteMessage(message.id)}}>Delete</button> <button onClick={() => {deleteMessage(message.id)}}>Delete</button>
)} )}

View file

@ -9,17 +9,18 @@ export default function Register () {
function handleSubmit(e: React.FormEvent) { function handleSubmit(e: React.FormEvent) {
e.preventDefault(); e.preventDefault();
axios.post("/api/auth/register", { username, password }); axios.post("/api/auth/register", { username, password }).then(() => {
axios axios
.post("/api/auth/login", { username, password }) .post("/api/auth/login", { username, password })
.then((res) => { .then((res) => {
localStorage.setItem("token", res.data.token); localStorage.setItem("token", res.data.token);
navigate("/"); navigate("/");
}) })
.catch((err) => { .catch((err) => {
alert(err.response.data.message); alert(err.response.data.message);
} }
); );
});
} }
return ( return (