I have server code and index.js code conected to index.html file. everything seemed to be working, but I can't get the next task done.
Task is: If a new user created, and he's redirected to casino.html and after this when he will wisit home page, hape page should check if he has session cookie set, and if yes, he should be automaticaly redirected to casino.html. But it dont seem to work.
server.js:
const express = require('express')
const uuid = require('uuid').v4
const mysql = require('mysql2')
const app = express()
const PagePassword = '1234'
const pool = mysql.createConnection({
host: "localhost",
port:'3306',
user: 'root',
password:'1234567890',
database:'mydb'
})
app.use(express.json())
app.use(express.static('client'))
const sessions ={}
app.post('/', (req,res) =>{
const sesionCheck = req.headers.cookie?.split('=')[1]
const usersession = sessions[sesionCheck]
if(usersession){
document.location.href = 'http://localhost:3000/casino.html'
}else{
console.log(req.headers)
const loginCode = `select * from users where nickname="${req.body.nickname}"`
const PostPasswd = req.body.PostPasswd
pool.query(loginCode, (err,resoult) =>{
if(!resoult.length && PostPasswd==PagePassword){
//console.log('New user created')
const code = `INSERT into Users (nickname,points,is_croupier)\nVALUES("${req.body.nickname}",1000, 0);`
pool.query(code)
//set cookie and session for new user
pool.query(loginCode, (err,resoult)=>{
const userID = resoult[resoult.length-1].user_id
const _nickname = resoult[resoult.length-1].nickname
const sessionId = uuid()
sessions[sessionId] = {_nickname, userID}
res.set('Set-Cookie', `session=${sessionId}`)
res.json({message: "UserIsCreated"})
console.log('added cookie to' + sessionId)
console.log(sessions)
})
}
if(resoult.length){
if(resoult[resoult.length-1].nickname == req.body.nickname && PostPasswd==PagePassword){
//ocupated nickname
res.json({message: "nicknameIsOccupated"})
}
}
if(PostPasswd!=PagePassword){
//invalid password
console.log('Invalid password')
}
})
}
})
app.listen(3000, ()=>{
console.log('server is running at port 3000')
})
index.js script:
const NicknameText = document.getElementById('nickname');
const PasswdText = document.getElementById('passwd');
const LoginButton = document.getElementById('loginBtn');
const ServerURL = 'http://localhost:3000';
LoginButton.onclick = async function Login(e) {
e.preventDefault();
const response = await fetch(ServerURL, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
nickname: NicknameText.value,
PostPasswd: PasswdText.value
}),
credentials: 'include'
});
const data = await response.json();
if (response.ok) {
if (data.message === "nicknameIsOccupated") {
console.log('This nickname is in use!');
NicknameText.value = ''
PasswdText.value = ''
NicknameText.placeholder = "This nickname is in use!"
const icon = document.createElement('i')
icon.className = 'fa-solid fa-circle-exclamation'// Assuming you're using Font Awesome
const container = document.createElement('div')
container.appendChild(icon)
NicknameText.appendChild(container)
// Append the icon and error message to the login button
}
if(data.message === "UserIsCreated"){
document.location.href = 'http://localhost:3000/casino.html';
}
} else {
console.error('Error:', data.message);
}
};
What is the problem?
I just need help with code