*.js: WIP changes for session handling, but ...
Some checks failed
Basic Checking / Explore-Gitea-Actions (push) Failing after 42s
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:
parent
b41568337b
commit
523b979764
3 changed files with 23 additions and 36 deletions
35
app.js
35
app.js
|
@ -38,16 +38,21 @@ const db = new sqlite3.Database(config.db_path, (err) => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const accessLogStream = fs.createWriteStream(
|
if (config.PRODUCTION) {
|
||||||
path.join(__dirname, 'log', 'access.log'),
|
let accessLogStream = fs.createWriteStream(
|
||||||
{ flags: 'a' }
|
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(bodyParser.json())
|
||||||
app.use(morgan('combined', { stream: accessLogStream }))
|
|
||||||
app.use(session({
|
app.use(session({
|
||||||
resave: false,
|
resave: false,
|
||||||
saveUninitialized: false,
|
saveUninitialized: false,
|
||||||
|
cookie: { maxAge: 3600000 },
|
||||||
secret: config.session_token
|
secret: config.session_token
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
@ -84,12 +89,6 @@ try {
|
||||||
|
|
||||||
// Serve the HTML file
|
// Serve the HTML file
|
||||||
app.get('/', (req, res) => {
|
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'))
|
res.sendFile(path.join(__dirname, 'index.html'))
|
||||||
})
|
})
|
||||||
app.get('/asset/frontend.js', (req, res) => {
|
app.get('/asset/frontend.js', (req, res) => {
|
||||||
|
@ -97,9 +96,13 @@ app.get('/asset/frontend.js', (req, res) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get('/newSession', (req, res) => {
|
app.get('/newSession', (req, res) => {
|
||||||
log.info(req.session)
|
log.info(req.session.id)
|
||||||
req.session.regenerate()
|
req.session.regenerate((error) => {
|
||||||
log.info(req.session)
|
if (error) {
|
||||||
|
log.error(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
log.info(req.session.id)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Route to get a random animal name
|
// Route to get a random animal name
|
||||||
|
@ -120,12 +123,10 @@ app.post('/recordButtonClick', (req, res) => {
|
||||||
try {
|
try {
|
||||||
// const { buttonName, sessionId } = req.body;
|
// const { buttonName, sessionId } = req.body;
|
||||||
const result = req.body
|
const result = req.body
|
||||||
log.error(result)
|
|
||||||
|
|
||||||
db.run(
|
db.run(
|
||||||
'INSERT INTO button_clicks (session_id, animal_name, button_name, timestamp, time_difference) VALUES (?, ?, ?, ?, ?)',
|
'INSERT INTO button_clicks (session_id, animal_name, button_name, timestamp, time_difference) VALUES (?, ?, ?, ?, ?)',
|
||||||
[
|
[
|
||||||
result.session,
|
req.session.id,
|
||||||
result.animal,
|
result.animal,
|
||||||
result.button,
|
result.button,
|
||||||
result.time,
|
result.time,
|
||||||
|
|
|
@ -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 () {
|
async function newSession () {
|
||||||
setSessionStartTime()
|
setSessionStartTime()
|
||||||
try {
|
try {
|
||||||
|
@ -34,7 +22,6 @@ async function newSession () {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching data:', error)
|
console.error('Error fetching data:', error)
|
||||||
}
|
}
|
||||||
getSessionId()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to set session start time
|
// Function to set session start time
|
||||||
|
@ -52,7 +39,7 @@ function displayTimeDifference () {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to record button clicks on the server
|
// Function to record button clicks on the server
|
||||||
async function recordButtonClick (buttonName, sessionId) {
|
async function recordButtonClick (buttonName) {
|
||||||
try {
|
try {
|
||||||
const currentTime = new Date()
|
const currentTime = new Date()
|
||||||
if (lastButtonClickTime) {
|
if (lastButtonClickTime) {
|
||||||
|
@ -62,7 +49,6 @@ async function recordButtonClick (buttonName, sessionId) {
|
||||||
const bodyData = JSON.stringify({
|
const bodyData = JSON.stringify({
|
||||||
animal,
|
animal,
|
||||||
button: buttonName,
|
button: buttonName,
|
||||||
session: sessionId,
|
|
||||||
difference: timeDifference,
|
difference: timeDifference,
|
||||||
time: sessionStartTime
|
time: sessionStartTime
|
||||||
})
|
})
|
||||||
|
@ -86,11 +72,11 @@ async function recordButtonClick (buttonName, sessionId) {
|
||||||
|
|
||||||
// Add click event listeners to the buttons
|
// Add click event listeners to the buttons
|
||||||
document.getElementById('isCritterButton').addEventListener('click', () => {
|
document.getElementById('isCritterButton').addEventListener('click', () => {
|
||||||
recordButtonClick('is critter', getSessionId())
|
recordButtonClick('is critter')
|
||||||
})
|
})
|
||||||
|
|
||||||
document.getElementById('isNotCritterButton').addEventListener('click', () => {
|
document.getElementById('isNotCritterButton').addEventListener('click', () => {
|
||||||
recordButtonClick('is not critter', getSessionId())
|
recordButtonClick('is not critter')
|
||||||
})
|
})
|
||||||
|
|
||||||
document.getElementById('startOverButton').addEventListener('click', () => {
|
document.getElementById('startOverButton').addEventListener('click', () => {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const config = module.exports
|
const config = module.exports
|
||||||
const PRODUCTION = process.env.NODE_ENV === 'production'
|
config.PRODUCTION = process.env.NODE_ENV === 'production'
|
||||||
const bole = require('bole')
|
const bole = require('bole')
|
||||||
|
|
||||||
config.express = {
|
config.express = {
|
||||||
|
@ -7,7 +7,7 @@ config.express = {
|
||||||
ip: '127.0.0.1'
|
ip: '127.0.0.1'
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PRODUCTION) {
|
if (config.PRODUCTION) {
|
||||||
config.express.ip = '0.0.0.0'
|
config.express.ip = '0.0.0.0'
|
||||||
config.db_path = 'db/results.db'
|
config.db_path = 'db/results.db'
|
||||||
config.session_token = process.env.SESSION_TOKEN
|
config.session_token = process.env.SESSION_TOKEN
|
||||||
|
|
Loading…
Reference in a new issue