code refactor, auto redirect to chat
This commit is contained in:
64
index.js
64
index.js
@@ -30,13 +30,38 @@ app.use(session({
|
||||
}));
|
||||
|
||||
app.post('/auth/login', async function(req, res) {
|
||||
let username = req.body.username.trim();
|
||||
let password = req.body.password.trim();
|
||||
|
||||
await loginUser(req, res);
|
||||
});
|
||||
|
||||
app.post('/auth/signup', async (req, res) => {
|
||||
await signupUser(req, res);
|
||||
});
|
||||
|
||||
// serving the login page
|
||||
app.get('/login', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '/frontend/routes/login.html'));
|
||||
})
|
||||
|
||||
// serving the signup page
|
||||
app.get('/signup', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '/frontend/routes/signup.html'));
|
||||
})
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
if(req.session.loggedin){
|
||||
res.sendFile(path.join(__dirname, '/frontend/routes/chat.html'));
|
||||
} else {
|
||||
res.redirect('/login')
|
||||
}
|
||||
|
||||
})
|
||||
// run server
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`)
|
||||
})
|
||||
|
||||
//signup function
|
||||
async function signupUser(req, res) {
|
||||
|
||||
let username = req.body.username.trim();
|
||||
let password = req.body.password.trim();
|
||||
@@ -55,30 +80,17 @@ app.post('/auth/signup', async (req, res) => {
|
||||
|
||||
// Insert user
|
||||
await insertUser(username, hash);
|
||||
req.session.loggedin = true;
|
||||
req.session.username = username;
|
||||
|
||||
return res.status(200).send("Account successfully created <a href=/login>Login screen</a>");
|
||||
|
||||
} catch (err) {
|
||||
console.error('Error inserting data:', err);
|
||||
return res.status(500).send('Error inserting data');
|
||||
}
|
||||
});
|
||||
|
||||
// serving the login page
|
||||
app.get('/login', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '/frontend/routes/login.html'));
|
||||
})
|
||||
|
||||
// serving the signup page
|
||||
app.get('/signup', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '/frontend/routes/signup.html'));
|
||||
})
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '/frontend/routes/index.html'));
|
||||
})
|
||||
// run server
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`)
|
||||
})
|
||||
}
|
||||
|
||||
// login function
|
||||
async function loginUser(req, res) {
|
||||
@@ -92,7 +104,15 @@ async function loginUser(req, res) {
|
||||
const user = result.rows[0];
|
||||
const match = await bcrypt.compare(password, user.password);
|
||||
if (match) {
|
||||
res.send('Login successful!');
|
||||
res.send(`
|
||||
<p>Login successful!</p>
|
||||
<p>Redirecting to chat...</p>
|
||||
<script>
|
||||
setTimeout(() =>{
|
||||
window.location.href = '/';
|
||||
}, 3000);
|
||||
</script>
|
||||
`);
|
||||
} else {
|
||||
res.send('Incorrect Username or Password!');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user