mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Auto-extend session cookie every 30 minutes
This commit is contained in:
@ -9,6 +9,9 @@ import { ensureImageFormatSupported, getBase64Async, humanFileSize } from './uti
|
|||||||
export let currentUser = null;
|
export let currentUser = null;
|
||||||
export let accountsEnabled = false;
|
export let accountsEnabled = false;
|
||||||
|
|
||||||
|
// Extend the session every 30 minutes
|
||||||
|
const SESSION_EXTEND_INTERVAL = 30 * 60 * 1000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable or disable user account controls in the UI.
|
* Enable or disable user account controls in the UI.
|
||||||
* @param {boolean} isEnabled User account controls enabled
|
* @param {boolean} isEnabled User account controls enabled
|
||||||
@ -894,6 +897,24 @@ async function slugify(text) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pings the server to extend the user session.
|
||||||
|
*/
|
||||||
|
async function extendUserSession() {
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/ping?extend=1', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: getRequestHeaders(),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Ping did not succeed', { cause: response.status });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to extend user session', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
jQuery(() => {
|
jQuery(() => {
|
||||||
$('#logout_button').on('click', () => {
|
$('#logout_button').on('click', () => {
|
||||||
logout();
|
logout();
|
||||||
@ -904,4 +925,9 @@ jQuery(() => {
|
|||||||
$('#account_button').on('click', () => {
|
$('#account_button').on('click', () => {
|
||||||
openUserProfile();
|
openUserProfile();
|
||||||
});
|
});
|
||||||
|
setInterval(async () => {
|
||||||
|
if (currentUser) {
|
||||||
|
await extendUserSession();
|
||||||
|
}
|
||||||
|
}, SESSION_EXTEND_INTERVAL);
|
||||||
});
|
});
|
||||||
|
@ -556,7 +556,13 @@ app.use('/api/users', usersPublicRouter);
|
|||||||
|
|
||||||
// Everything below this line requires authentication
|
// Everything below this line requires authentication
|
||||||
app.use(requireLoginMiddleware);
|
app.use(requireLoginMiddleware);
|
||||||
app.get('/api/ping', (_, response) => response.sendStatus(204));
|
app.get('/api/ping', (request, response) => {
|
||||||
|
if (request.query.extend && request.session) {
|
||||||
|
request.session.touch = Date.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
response.sendStatus(204);
|
||||||
|
});
|
||||||
|
|
||||||
// File uploads
|
// File uploads
|
||||||
app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar'));
|
app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar'));
|
||||||
|
Reference in New Issue
Block a user