fix first user owner assignment on OIDC login

This commit is contained in:
drifterza
2026-02-27 13:56:15 +02:00
parent 0cff389051
commit 64edd2041b
2 changed files with 142 additions and 9 deletions
+25 -9
View File
@@ -82,19 +82,35 @@ export async function loader({ request, context }: Route.LoaderArgs) {
})()
: userInfo.picture;
const [{ count: userCount }] = await context.db
const [{ count: ownerCount }] = await context.db
.select({ count: count() })
.from(users)
.where(eq(users.caps, Roles.owner));
await context.db
.insert(users)
.values({
id: ulid(),
sub: claims.sub,
caps: userCount === 0 ? Roles.owner : Roles.member,
})
.onConflictDoNothing();
const needsOwner = ownerCount === 0;
if (needsOwner) {
await context.db
.insert(users)
.values({
id: ulid(),
sub: claims.sub,
caps: Roles.owner,
})
.onConflictDoUpdate({
target: users.sub,
set: { caps: Roles.owner },
});
} else {
await context.db
.insert(users)
.values({
id: ulid(),
sub: claims.sub,
caps: Roles.member,
})
.onConflictDoNothing();
}
return redirect("/", {
headers: {