{"success":true,"name":"hash-passwords-with-bcrypt-and-compare-with-its-own-function","description":"Building signup, login, or password change with a users table","category":"ai-brimind","path":"ai-brimind/hash-passwords-with-bcrypt-and-compare-with-its-own-function/SKILL.md","file":"SKILL.md","content":"---\nname: hash-passwords-with-bcrypt-and-compare-with-its-own-function\ndescription: \"Building signup, login, or password change with a users table\"\n---\n\n# Hash Passwords With bcrypt and Compare With Its Own Function — Never Store or Log the Plaintext\n\n## When to use\nYou are writing any code that receives, stores, or checks a password (signup, login, reset, change), and the current plan stores it as-is, 'encrypts' it home-made, or compares it with plain equality.\n\n## Example request\nPassword saved directly into the users collection → one DB leak or one curious screenshot exposes every user's password, including the ones they reuse for their email; passwords are never stored, only their hashes. Hashing done with plain SHA/MD5 or a hand-rolled scramble → these are fast by design, so attackers test billions per second; password hashing is a solved problem with dedicated slow algorithms — bcrypt is the standard reach. Login compares strings with === after hashing manually → salt handling done wrong breaks everything; bcrypt.compare exists precisely to check a password against its hash correctly. Whole request body logged for debugging on the login route → the password lands in plaintext in the logs forever; secrets must be stripped before logging, and reset flows send a one-time link, never the password itself.\n\n## Instructions\nAct as a developer who treats every password as radioactive material passing through the system: it may be used for one comparison, and it must never be stored, logged, or echoed anywhere — because users reuse passwords, so leaking yours leaks their bank. For any password-touching code, do this: (1) At signup or password change, hash before storing using a real password-hashing library — bcrypt (or argon2) — via its standard usage: generate the hash with a cost factor (bcrypt's default salting is built in; you do NOT hand-manage salts). Store ONLY the hash. If you were about to write your own 'encryption' or use plain SHA256, stop: fast hashes and home-made schemes are exactly what attackers eat; this problem has a name and a library. (2) At login, fetch the user's stored hash and check with the library's comparison function — bcrypt.compare(plaintext, hash) — never by hashing manually and comparing strings; the library handles the salt embedded in the hash. On failure, return the SAME message and status for 'no such user' and 'wrong password' ('invalid credentials'), so the API does not confirm which emails exist. (3) Sweep the plaintext's whole journey: it must appear only in the request body and the compare call — never in console.logs, error messages, analytics, or responses. If the project logs request bodies anywhere, exclude or mask the password field explicitly. (4) The DB layer helps too: make sure queries selecting users for general purposes do not drag the hash field into API responses (exclude it in the schema or query projection) — even hashes do not belong in JSON sent to browsers. (5) 'Forgot password' means a expiring one-time reset link or code — never emailing the password (which you could not do anyway, since you only have the hash — that is the proof you did it right). Only mark this done once you can point at: the hash call at signup, the compare call at login, the identical failure message, and confirm the plaintext appears in zero logs and zero responses.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #203\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}