generated from lucien/api-template
fix: fixed register page
This commit is contained in:
parent
90a6b6fa63
commit
414be922bc
3 changed files with 19 additions and 17 deletions
|
@ -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' });
|
||||
}
|
||||
|
|
|
@ -99,6 +99,7 @@ export default function Channel() {
|
|||
{messages.slice(0, maxMessageToShown).map((message) => (
|
||||
<li key={message.id}>
|
||||
<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) && (
|
||||
<button onClick={() => {deleteMessage(message.id)}}>Delete</button>
|
||||
)}
|
||||
|
|
|
@ -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 (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue