What would be the best way to create the first user? (dbAuth)

Hello,

I would like to not offer the signup functionality and instead, have a user created by default by the application which would allow inviting other users later.

I thought about using server.ts, but I wonder if this line is blocking:

  await server.start()

Ideally, I would have liked to have a function that allows me to check if there are no users yet (first boot) in the database, so if this function is blocking, I think my solution might not work.

Do you have any other suggestions? Or does the proposed solution above work?

1 Like

What auth provider ar you using?

If dbAuth, could you not just seed the first initial user during database creation?

1 Like

It’s dbAuth indeed, I thought about it, but it seems odd to me since there’s a whole logic behind dbAuth, I’d have to basically replicate the logic at the Postgres level :face_with_diagonal_mouth:

I could probably initiate an account without any password and use the forgot password then ?

Right, you can do that. Or you can just seed a User row with hashedPassword and salt.

Using prisma db seed: Command Line Interface | RedwoodJS Docs

I worked through a seed script using Typescript, sort of piggybacking off of the dbAuth example seed script. Longer post here on Spoke, but this is the working code I ended with:

const users: Array<Omit<Prisma.UserCreateInput, 'hashedPassword' | 'salt'> & { password: string }> = [
  {
    email: 'admin@example.com',
    password: 'adminpassword',
    role: { connect: { id: createdRoles.find((role) => role.name === 'ADMIN')?.id } },
  },
  // ...
]

for (const user of users) {
  const [hashedPassword, salt] = hashPassword(user.password)
  await db.user.create({
    data: {
      email: user.email,
      hashedPassword,
      salt,
      role: user.role,
    },
  })
}
1 Like

Hey ! That looks great !

I can’t find this info on the doc, does the seeding will be executed on deployment ?