*.js: WIP changes for session handling, but ...
Some checks failed
Basic Checking / Explore-Gitea-Actions (push) Failing after 42s

/newSession is still not right. It seems to work right the first time,
but then is an error...

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2023-09-29 16:34:41 -04:00
parent b41568337b
commit 523b979764
Signed by: vbatts
GPG key ID: E30EFAA812C6E5ED
3 changed files with 23 additions and 36 deletions

35
app.js
View file

@ -38,16 +38,21 @@ const db = new sqlite3.Database(config.db_path, (err) => {
}
})
const accessLogStream = fs.createWriteStream(
path.join(__dirname, 'log', 'access.log'),
{ flags: 'a' }
)
if (config.PRODUCTION) {
let accessLogStream = fs.createWriteStream(
path.join(__dirname, 'log', 'access.log'),
{ flags: 'a' }
)
app.use(morgan('combined', { stream: accessLogStream }))
} else {
app.use(morgan('combined'))
}
app.use(bodyParser.json())
app.use(morgan('combined', { stream: accessLogStream }))
app.use(session({
resave: false,
saveUninitialized: false,
cookie: { maxAge: 3600000 },
secret: config.session_token
}))
@ -84,12 +89,6 @@ try {
// Serve the HTML file
app.get('/', (req, res) => {
if (typeof req.session.cookie.expires === 'undefined') {
const hour = 3600000
req.session.cookie.expires = new Date(Date.now() + hour)
req.session.cookie.maxAge = hour
}
res.sendFile(path.join(__dirname, 'index.html'))
})
app.get('/asset/frontend.js', (req, res) => {
@ -97,9 +96,13 @@ app.get('/asset/frontend.js', (req, res) => {
})
app.get('/newSession', (req, res) => {
log.info(req.session)
req.session.regenerate()
log.info(req.session)
log.info(req.session.id)
req.session.regenerate((error) => {
if (error) {
log.error(error)
}
})
log.info(req.session.id)
})
// Route to get a random animal name
@ -120,12 +123,10 @@ app.post('/recordButtonClick', (req, res) => {
try {
// const { buttonName, sessionId } = req.body;
const result = req.body
log.error(result)
db.run(
'INSERT INTO button_clicks (session_id, animal_name, button_name, timestamp, time_difference) VALUES (?, ?, ?, ?, ?)',
[
result.session,
req.session.id,
result.animal,
result.button,
result.time,

View file

@ -13,18 +13,6 @@ async function getNextAnimal () {
}
}
// Function to set or retrieve the session ID cookie
function getSessionId () {
const sessionId = document.cookie.replace(
/(?:(?:^|.*;\s*)sessionId\s*=\s*([^;]*).*$)|^.*$/,
'$1'
)
if (!sessionId) {
return newSession()
}
return sessionId
}
async function newSession () {
setSessionStartTime()
try {
@ -34,7 +22,6 @@ async function newSession () {
} catch (error) {
console.error('Error fetching data:', error)
}
getSessionId()
}
// Function to set session start time
@ -52,7 +39,7 @@ function displayTimeDifference () {
}
// Function to record button clicks on the server
async function recordButtonClick (buttonName, sessionId) {
async function recordButtonClick (buttonName) {
try {
const currentTime = new Date()
if (lastButtonClickTime) {
@ -62,7 +49,6 @@ async function recordButtonClick (buttonName, sessionId) {
const bodyData = JSON.stringify({
animal,
button: buttonName,
session: sessionId,
difference: timeDifference,
time: sessionStartTime
})
@ -86,11 +72,11 @@ async function recordButtonClick (buttonName, sessionId) {
// Add click event listeners to the buttons
document.getElementById('isCritterButton').addEventListener('click', () => {
recordButtonClick('is critter', getSessionId())
recordButtonClick('is critter')
})
document.getElementById('isNotCritterButton').addEventListener('click', () => {
recordButtonClick('is not critter', getSessionId())
recordButtonClick('is not critter')
})
document.getElementById('startOverButton').addEventListener('click', () => {

View file

@ -1,5 +1,5 @@
const config = module.exports
const PRODUCTION = process.env.NODE_ENV === 'production'
config.PRODUCTION = process.env.NODE_ENV === 'production'
const bole = require('bole')
config.express = {
@ -7,7 +7,7 @@ config.express = {
ip: '127.0.0.1'
}
if (PRODUCTION) {
if (config.PRODUCTION) {
config.express.ip = '0.0.0.0'
config.db_path = 'db/results.db'
config.session_token = process.env.SESSION_TOKEN