Next.js Discord

Discord Forum

Bcrypt in client component?

Answered
style____ posted this in #help-forum
Open in Discord
Avatar
style____OP
Is there any way to use these two functions in client component?
import bcrypt from 'bcrypt';

export async function hashPassword(password: string) {
  return await bcrypt.hash(password, 10)
}

export async function comparePassword(password: string, hashedPassword: string) {
  return await bcrypt.compare(password, hashedPassword)
}
right now i get this error:
Module not found: Can't resolve 'fs'
Answered by Cape lion
yep
View full answer

16 Replies

Avatar
Cape lion
why do you want to use this in the client?
Avatar
style____OP
Maybe thats not the best design, but i got component with many useStates - and I insert data into database in it as well
and ofc i want to hash password while inserting it into database
I can show you entire code if you need
Avatar
Cape lion
why not hash the password on the server?
Avatar
style____OP
how would i do that if i got component with useStates that got all of the data, I could create api route?
Avatar
Cape lion
yep
Answer
Avatar
Cape lion
also me personally I would never implement an account system manually because I can't risk getting it wrong 😄
so I usually just use an existing product for account management
Avatar
style____OP
its not really account system but i need hashed password
its complicated :p
Avatar
Cape lion
haha okay
Avatar
style____OP
thank you
Avatar
style____OP
still getting same error even tho i made route

import { hashPassword } from "@/lib/helpers";
import { NextRequest, NextResponse } from "next/server";

export default async function POST(req: NextRequest, res: NextResponse) {
    const { password } = await req.json();
    const hash = await hashPassword(password);
    return NextResponse.json({ hash });
}


const saveProfile = async () => {

        const r = await fetch('/api/password/hash', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ password })
        })
        const { hashedPassword } = await r.json()
Avatar
Cape lion
is the error happening inside the api route?
Avatar
style____OP
okey nvm had to move it from utils file to route my bad thank you!