Add session maxAge

This commit is contained in:
Maki
2026-01-11 15:40:32 +08:00
parent 4ec794efc5
commit caf6ae9416
5 changed files with 18 additions and 90 deletions
+13 -11
View File
@@ -29,19 +29,19 @@ if (!fs.existsSync(path.join(process.cwd(), ".env"))) {
const secret = PasswordGen(24)
const rString = `
port=80
altregion=us # the displayed region in each page's header
port=80
altregion=us # the displayed region in each page's header
servicename=File Server # the displayed service name in each page's header
description=A simple file server built with Express. # the displayed description in the home page
servicename=File Server # the displayed service name in each page's header
description=A simple file server built with Express. # the displayed description in the home page
bootmessage=Welcome!
bootmessage=Welcome!
session_secret=${secret} # the session secret used for logins. you can generate a new secret using the idGen command.
enable_register=false # it's advised to keep this off unless you're hosting a public instance
session_secret=${secret} # the session secret used for logins. you can generate a new secret using the idGen command.
enable_register=false # it's advised to keep this off unless you're hosting a public instance
postwrite_notice=Hi! # the notice on top of the post form
postwrite_html=false # if enabled, users can use html in posts. this is unsafe! you can set it to true if you trust your users.
postwrite_notice=Hi! # the notice on top of the post form
postwrite_html=false # if enabled, users can use html in posts. this is unsafe! you can set it to true if you trust your users.
`
fs.writeFileSync(path.join(process.cwd(), ".env"), rString);
@@ -74,7 +74,7 @@ export var server: Server;
import moment from 'moment-timezone'
import { Server } from "http";
import session from "express-session";
import session, { MemoryStore, Store } from "express-session";
var accessLogStream = fs.createWriteStream('./pvfiles/interactions.log', { flags: 'a' });
@@ -88,15 +88,17 @@ try {
filename: (req, file, cb) =>
cb(null, `${Date.now()}_${file.originalname}`)
})
upload = multer({storage})
upload = multer({ storage })
app.use(
session({
name: "session",
store: new MemoryStore(),
secret: process.env.session_secret,
resave: false,
saveUninitialized: false,
cookie: {
maxAge: Date.now() + (30 * 24 * 3600 * 1000),
httpOnly: true,
sameSite: "lax",
secure: dev
+1 -1
View File
@@ -91,7 +91,7 @@ export async function Main(app: Express) {
username: user.username
};
res.redirect("/");
res.redirect(req.query?.next?.toString() ?? "/");
}
);
});
+2 -2
View File
@@ -185,7 +185,7 @@ export async function Main(app: Express) {
<form action="/posts/new" method="GET">
<button class="tonly">New Post</button>
</form>
` : `<p><a href="/login">Log in</a> to create posts.</p>`}
` : `<p><a href="/login?next=/posts/new">Log in</a> to create posts.</p>`}
<hr>
<br>
<div class="postlist_r">
@@ -202,7 +202,7 @@ export async function Main(app: Express) {
<p>You must be logged in to create a post.</p>
</div>
`, `
<a href="/login">Log in</a> | <a href="/register">Register</a><br><br>
<a href="/login?next=/posts/new">Log in</a> | <a href="/register?next=/posts/new">Register</a><br><br>
`))
else {
return res.send(newpost_string());
+1 -75
View File
@@ -91,81 +91,7 @@ export async function Main(app: Express) {
if (isnameerror)
res.send(register_string(`The username ${username} is already taken!`))
else
res.send(`
<!DOCTYPE html>
<body>
<style>
h1 {
font-size: 50
}
body {
margin-right: 10%;
margin-left: 10%;
margin-bottom: 10%;
text-align: left;
color: black;
font-family: Verdana, sans-serif;
}
a:link {
color: #3c72a3
}
a:visited {
color: #2a567d
}
@media screen and (prefers-color-scheme: dark) {
body {
background-color: #383737;
color: #b0acac;
}
}
@media screen and (prefers-color-scheme: light) {
body {
background-color: #e3e3e3;
color: black;
}
}
.description {
font-size: 32px
}
.details {
font-size: 20px
}
.extra {
font-size: 24px
}
</style>
<h1>${process.env.servicename || "File Server"}</h1>
<div class="details">
<p>Server region: ${'(alternate) ' + process.env.altregion}</p>
</div><hr><br>
<div class="description">
${process.env.description || "A simple file server built with Express."}
</div>
<div class="extra">
Account created successfully!<br>
${req.session.user ? `
<p>Logged in as ${req.session.user.username}<br>
<a href=/api/logout>log out</a></p>
` : `
<a href=/login>Log in</a> | <a href=/register>Register</a><br>
`}
<br><br>
Pages<br>
) <a href=/staticfiles>/staticfiles</a><br>
) <a href="/">Home</a>
</div>
</body>
`);
res.redirect(req.query?.next?.toString() ?? "/");
}
);
} catch (err) {
+1 -1
View File
@@ -145,7 +145,7 @@ export async function Main(app: Express) {
app.get("/upload", async (req: Request, res: Response) => {
if (!req.session.user)
return res.status(401).send(upload_string("", "You must be <a href=/login>logged in</a> to use this feature."))
return res.status(401).send(upload_string("", "You must be <a href=/login?next=/upload>logged in</a> to use this feature."))
else {
const user = req.session.user;
return res.send(await upload_string_home(null, null, user))