Add bootstrap-recreate endpoint for proper user creation
This commit is contained in:
@@ -416,30 +416,36 @@ export const hammerRoutes = new Elysia({ prefix: '/hammer' })
|
|||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
// Bootstrap: reset user password (temporary setup helper - REMOVE after use)
|
// Bootstrap: delete and recreate user with proper BetterAuth password (temporary - REMOVE after use)
|
||||||
.post('/bootstrap-reset', async ({ body, set }) => {
|
.post('/bootstrap-recreate', async ({ body, set }) => {
|
||||||
const user = await db.query.users.findFirst({
|
const { email, password, name } = body;
|
||||||
where: eq(users.email, body.email),
|
const existing = await db.query.users.findFirst({
|
||||||
|
where: eq(users.email, email),
|
||||||
});
|
});
|
||||||
if (!user) {
|
if (existing) {
|
||||||
set.status = 404;
|
const { accounts, sessions } = await import('../db/schema');
|
||||||
throw new Error('User not found');
|
await db.delete(accounts).where(eq(accounts.userId, existing.id));
|
||||||
|
await db.delete(sessions).where(eq(sessions.userId, existing.id));
|
||||||
|
await db.delete(users).where(eq(users.id, existing.id));
|
||||||
}
|
}
|
||||||
// Use BetterAuth's internal API to set password
|
const result = await auth.api.signUpEmail({
|
||||||
const ctx = await auth.api.signInEmail({
|
body: { email, password, name },
|
||||||
body: { email: body.email, password: body.newPassword },
|
});
|
||||||
}).catch(() => null);
|
if (!result) {
|
||||||
// If sign-in fails, the password doesn't match. We need to update via the accounts table.
|
set.status = 500;
|
||||||
// Use Bun's password hash directly
|
throw new Error('Failed to create user');
|
||||||
const hash = await Bun.password.hash(body.newPassword, { algorithm: 'bcrypt' });
|
}
|
||||||
const { accounts } = await import('../db/schema');
|
const newUser = await db.query.users.findFirst({
|
||||||
await db.update(accounts).set({ password: hash }).where(eq(accounts.userId, user.id));
|
where: eq(users.email, email),
|
||||||
// Also set role to admin
|
});
|
||||||
await db.update(users).set({ role: 'admin' }).where(eq(users.id, user.id));
|
if (newUser) {
|
||||||
return { success: true, email: body.email, role: 'admin' };
|
await db.update(users).set({ role: 'admin' }).where(eq(users.id, newUser.id));
|
||||||
|
}
|
||||||
|
return { success: true, email, role: 'admin' };
|
||||||
}, {
|
}, {
|
||||||
body: t.Object({
|
body: t.Object({
|
||||||
email: t.String({ format: 'email' }),
|
email: t.String({ format: 'email' }),
|
||||||
newPassword: t.String({ minLength: 8 }),
|
password: t.String({ minLength: 8 }),
|
||||||
|
name: t.String(),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user