Next.js Discord

Discord Forum

Bcrypt in client component?

Answered
style posted this in #help-forum
Open in Discord
Avatar
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
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
Avatar
@Cape lion why do you want to use this in the client?
Avatar
I can show you entire code if you need
Avatar
@style and ofc i want to hash password while inserting it into database
Avatar
Cape lion
why not hash the password on the server?
Avatar
@Cape lion why not hash the password on the server?
Avatar
how would i do that if i got component with useStates that got all of the data, I could create api route?
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
its not really account system but i need hashed password
its complicated :p
Avatar
Cape lion
haha okay
Avatar
thank you
Avatar
@Cape lion haha okay
Avatar
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
okey nvm had to move it from utils file to route my bad thank you!