Adding a password field to an existing User type


#1

I have a user type without a password field. How can I add the column and use @migrationValue to hash the password.

I checked the docs. No luck, still searching.


#2

One way would be to do the hash locally and then run the migration with hash. Is that the right way?


#3

You would follow this process:

  • first, add the new field password: String! @migerationValue(value: "default") with a default value to fulfill the required constraint
  • hash & import the password data with a script

#4

This solves it, thanks for the lightning fast response.

For reference, a sample script to generate hash

const bcrypt = require("bcryptjs");

const SALT_ROUNDS = 10;
const generatePasswordHash = async password => {
  const salt = bcrypt.genSaltSync(SALT_ROUNDS);
  const hash = await bcrypt.hash(password, SALT_ROUNDS);
  return hash;
};

generatePasswordHash("sample-password").then(password => console.log(password));