atinux / nuxt-auth-utils

Add Authentication to Nuxt applications with secured & sealed cookies sessions.
MIT License
973 stars 91 forks source link

Unable to use `hashPassword` in Prisma seed files #246

Closed iLucasPires closed 1 month ago

iLucasPires commented 1 month ago

Hi, I'm trying to use the hashPassword function from the nuxt-auth-utils package to hash passwords in a Prisma seed file. However, I'm unable to import or use the function correctly within my seeding logic.

Example Code (Prisma seed file):

import { PrismaClient } from '@prisma/client';
import { hashPassword } from 'nuxt-auth-utils'; // Fails here

const prisma = new PrismaClient();

async function main() {
  const hashedPassword = await hashPassword('user_password');

  await prisma.user.create({
    data: {
      email: 'user@example.com',
      password: hashedPassword,
    },
  });
}

main()
  .catch(e => console.error(e))
  .finally(async () => {
    await prisma.$disconnect();
  });

I would appreciate any help or guidance on resolving this issue.

Thanks!

atinux commented 1 month ago

Hi @iLucasPires

I recommend you to use server tasks for seed, see https://nitro.unjs.io/guide/tasks

See example with Drizzle ORM: https://hub.nuxt.com/docs/recipes/drizzle#seed-the-database-optional

iLucasPires commented 1 month ago

I understand, Vlw vlw(Brazilian slang for thank you)