Seed users for prisma

Hello,

I started using redwood a couple of days back and seems like a great stack to quickly build things!

Now, like the title says - there is the seed.ts included as a part of prisma, but I’m having issue on how to populate user data?

Initially I’ve tried to just use bcrypt to create the seed and the hash, but since that’s probably not really how it works in the background, that obviously didn’t work. Then I’ve tried to searching if there is a way to overwrite the login handler, but that one also only seems to trigger after you already have the user to login.

Now my question is: is there a way to programmatically add users in the seed.ts file?

Thanks!

Yes.

You can create a user with normal Prisma create statements.

You will just have to specify the salt and hashPassword.

You can create the hashPassword using the same code as here:

  // hashes a password using either the given `salt` argument, or creates a new
  // salt and hashes using that. Either way, returns an array with [hash, salt]
  _hashPassword(text: string, salt?: string) {
    const useSalt = salt || CryptoJS.lib.WordArray.random(128 / 8).toString()

    return [
      CryptoJS.PBKDF2(text, useSalt, { keySize: 256 / 32 }).toString(),
      useSalt,
    ]
  }

Then store that with your User.

I would rather avoid code duplication if possible, in case there would be changes in the future, but if there is no other way, I’ll take it! Thanks!