Add files via upload
This commit is contained in:
parent
1896ebf469
commit
76686a0cec
1 changed files with 47 additions and 17 deletions
|
|
@ -13,9 +13,20 @@ import {
|
||||||
WEBROOT,
|
WEBROOT,
|
||||||
} from "../helpers/env";
|
} from "../helpers/env";
|
||||||
|
|
||||||
export let FIRST_RUN = db.query("SELECT * FROM users").get() === null || false;
|
function computeFirstRun(): boolean {
|
||||||
|
// DB-driven, so it stays correct across reloads and per-request updates.
|
||||||
|
return db.query("SELECT 1 FROM users LIMIT 1").get() === null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kept as an exported boolean for backwards-compatibility (e.g. root.tsx imports FIRST_RUN),
|
||||||
|
// but it is refreshed per-request via userService. Do not rely on it being constant.
|
||||||
|
export let FIRST_RUN = computeFirstRun();
|
||||||
|
|
||||||
export const userService = new Elysia({ name: "user/service" })
|
export const userService = new Elysia({ name: "user/service" })
|
||||||
|
.derive(() => {
|
||||||
|
FIRST_RUN = computeFirstRun();
|
||||||
|
return {};
|
||||||
|
})
|
||||||
.use(
|
.use(
|
||||||
jwt({
|
jwt({
|
||||||
name: "jwt",
|
name: "jwt",
|
||||||
|
|
@ -67,7 +78,8 @@ export const userService = new Elysia({ name: "user/service" })
|
||||||
export const user = new Elysia()
|
export const user = new Elysia()
|
||||||
.use(userService)
|
.use(userService)
|
||||||
.get("/setup", ({ redirect }) => {
|
.get("/setup", ({ redirect }) => {
|
||||||
if (!FIRST_RUN) {
|
const isFirstRun = computeFirstRun();
|
||||||
|
if (!isFirstRun) {
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
return redirect(`${WEBROOT}/login`, 302);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -182,27 +194,31 @@ export const user = new Elysia()
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.post(
|
.post(
|
||||||
"/register",
|
"/register",
|
||||||
async ({ body: { email, password }, set, redirect, jwt, cookie: { auth } }) => {
|
async ({ body: { email, password }, set, redirect, jwt, cookie: { auth } }) => {
|
||||||
// first user allowed even if ACCOUNT_REGISTRATION=false
|
// DB-driven "first user" detection (no stale in-memory flag) + race-safe creation.
|
||||||
const isFirstUser = FIRST_RUN;
|
// We hash outside the write-lock to keep the lock window short.
|
||||||
|
const savedPassword = await Bun.password.hash(password);
|
||||||
|
|
||||||
|
// Acquire a write lock so only one instance can perform "count==0 then insert" at a time.
|
||||||
|
db.exec("BEGIN IMMEDIATE");
|
||||||
|
try {
|
||||||
|
const isFirstUser = computeFirstRun();
|
||||||
|
|
||||||
|
// first user allowed even if ACCOUNT_REGISTRATION=false
|
||||||
if (!ACCOUNT_REGISTRATION && !isFirstUser) {
|
if (!ACCOUNT_REGISTRATION && !isFirstUser) {
|
||||||
|
db.exec("ROLLBACK");
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
return redirect(`${WEBROOT}/login`, 302);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FIRST_RUN) {
|
const existingUser = db.query("SELECT 1 FROM users WHERE email = ?").get(email);
|
||||||
FIRST_RUN = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const existingUser = await db.query("SELECT * FROM users WHERE email = ?").get(email);
|
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
|
db.exec("ROLLBACK");
|
||||||
set.status = 400;
|
set.status = 400;
|
||||||
return {
|
return {
|
||||||
message: "Email already in use.",
|
message: "Email already in use.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const savedPassword = await Bun.password.hash(password);
|
|
||||||
|
|
||||||
const role = isFirstUser ? "admin" : "user";
|
const role = isFirstUser ? "admin" : "user";
|
||||||
|
|
||||||
|
|
@ -215,15 +231,19 @@ export const user = new Elysia()
|
||||||
const userRow = db.query("SELECT * FROM users WHERE email = ?").as(User).get(email);
|
const userRow = db.query("SELECT * FROM users WHERE email = ?").as(User).get(email);
|
||||||
|
|
||||||
if (!userRow) {
|
if (!userRow) {
|
||||||
|
db.exec("ROLLBACK");
|
||||||
set.status = 500;
|
set.status = 500;
|
||||||
return {
|
return {
|
||||||
message: "Failed to create user.",
|
message: "Failed to create user.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.exec("COMMIT");
|
||||||
|
FIRST_RUN = false;
|
||||||
|
|
||||||
const accessToken = await jwt.sign({
|
const accessToken = await jwt.sign({
|
||||||
id: String(userRow.id),
|
id: String(userRow.id),
|
||||||
role: userRow.role,
|
role: userRow.role ?? "user",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
|
|
@ -243,13 +263,23 @@ export const user = new Elysia()
|
||||||
});
|
});
|
||||||
|
|
||||||
return redirect(`${WEBROOT}/`, 302);
|
return redirect(`${WEBROOT}/`, 302);
|
||||||
},
|
} catch (e) {
|
||||||
{ body: "signIn" },
|
try {
|
||||||
)
|
db.exec("ROLLBACK");
|
||||||
|
} catch {
|
||||||
|
// ignore rollback errors
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ body: "signIn" },
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
.get(
|
.get(
|
||||||
"/login",
|
"/login",
|
||||||
async ({ jwt, redirect, cookie: { auth } }) => {
|
async ({ jwt, redirect, cookie: { auth } }) => {
|
||||||
if (FIRST_RUN) {
|
if (computeFirstRun()) {
|
||||||
return redirect(`${WEBROOT}/setup`, 302);
|
return redirect(`${WEBROOT}/setup`, 302);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue