Add files via upload
This commit is contained in:
parent
d5e334d882
commit
611bd57ba6
1 changed files with 70 additions and 72 deletions
|
|
@ -193,90 +193,88 @@ export const user = new Elysia()
|
||||||
</BaseHtml>
|
</BaseHtml>
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.post(
|
.post(
|
||||||
"/register",
|
"/register",
|
||||||
async ({ body: { email, password }, set, redirect, jwt, cookie: { auth } }) => {
|
async ({ body: { email, password }, set, redirect, jwt, cookie: { auth } }) => {
|
||||||
// DB-driven "first user" detection (no stale in-memory flag) + race-safe creation.
|
// DB-driven "first user" detection (no stale in-memory flag) + race-safe creation.
|
||||||
// We hash outside the write-lock to keep the lock window short.
|
// We hash outside the write-lock to keep the lock window short.
|
||||||
const savedPassword = await Bun.password.hash(password);
|
const savedPassword = await Bun.password.hash(password);
|
||||||
|
|
||||||
// Acquire a write lock so only one instance can perform "count==0 then insert" at a time.
|
// Acquire a write lock so only one instance can perform "count==0 then insert" at a time.
|
||||||
db.exec("BEGIN IMMEDIATE");
|
db.exec("BEGIN IMMEDIATE");
|
||||||
try {
|
try {
|
||||||
const isFirstUser = computeFirstRun();
|
const isFirstUser = computeFirstRun();
|
||||||
|
|
||||||
// first user allowed even if ACCOUNT_REGISTRATION=false
|
// first user allowed even if ACCOUNT_REGISTRATION=false
|
||||||
if (!ACCOUNT_REGISTRATION && !isFirstUser) {
|
if (!ACCOUNT_REGISTRATION && !isFirstUser) {
|
||||||
db.exec("ROLLBACK");
|
db.exec("ROLLBACK");
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
return redirect(`${WEBROOT}/login`, 302);
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingUser = db.query("SELECT 1 FROM users WHERE email = ?").get(email);
|
const existingUser = db.query("SELECT 1 FROM users WHERE email = ?").get(email);
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
db.exec("ROLLBACK");
|
db.exec("ROLLBACK");
|
||||||
set.status = 400;
|
set.status = 400;
|
||||||
return {
|
return {
|
||||||
message: "Email already in use.",
|
message: "Email already in use.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const role = isFirstUser ? "admin" : "user";
|
const role = isFirstUser ? "admin" : "user";
|
||||||
|
|
||||||
db.query("INSERT INTO users (email, password, role) VALUES (?, ?, ?)").run(
|
db.query("INSERT INTO users (email, password, role) VALUES (?, ?, ?)").run(
|
||||||
email,
|
email,
|
||||||
savedPassword,
|
savedPassword,
|
||||||
role,
|
role,
|
||||||
);
|
);
|
||||||
|
|
||||||
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");
|
db.exec("ROLLBACK");
|
||||||
set.status = 500;
|
set.status = 500;
|
||||||
return {
|
return {
|
||||||
message: "Failed to create user.",
|
message: "Failed to create user.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
db.exec("COMMIT");
|
db.exec("COMMIT");
|
||||||
FIRST_RUN = false;
|
FIRST_RUN = false;
|
||||||
|
|
||||||
const accessToken = await jwt.sign({
|
const accessToken = await jwt.sign({
|
||||||
id: String(userRow.id),
|
id: String(userRow.id),
|
||||||
role: userRow.role ?? "user",
|
role: userRow.role ?? "user",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!auth) {
|
if (!auth) {
|
||||||
set.status = 500;
|
set.status = 500;
|
||||||
return {
|
return {
|
||||||
message: "No auth cookie, perhaps your browser is blocking cookies.",
|
message: "No auth cookie, perhaps your browser is blocking cookies.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// set cookie
|
// set cookie
|
||||||
auth.set({
|
auth.set({
|
||||||
value: accessToken,
|
value: accessToken,
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: !HTTP_ALLOWED,
|
secure: !HTTP_ALLOWED,
|
||||||
maxAge: 60 * 60 * 24 * 7,
|
maxAge: 60 * 60 * 24 * 7,
|
||||||
sameSite: "strict",
|
sameSite: "strict",
|
||||||
});
|
});
|
||||||
|
|
||||||
return redirect(`${WEBROOT}/`, 302);
|
return redirect(`${WEBROOT}/`, 302);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
try {
|
try {
|
||||||
db.exec("ROLLBACK");
|
db.exec("ROLLBACK");
|
||||||
} catch {
|
} catch (rollbackErr) {
|
||||||
// ignore rollback errors
|
console.warn("[user/register] ROLLBACK failed:", rollbackErr);
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ body: "signIn" },
|
{ body: "signIn" },
|
||||||
)
|
)
|
||||||
|
.get(
|
||||||
|
|
||||||
.get(
|
|
||||||
"/login",
|
"/login",
|
||||||
async ({ jwt, redirect, cookie: { auth } }) => {
|
async ({ jwt, redirect, cookie: { auth } }) => {
|
||||||
if (computeFirstRun()) {
|
if (computeFirstRun()) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue