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