Passwords and Signing In
Accounts start with a password you must never be able to read. Hash passwords with bcryptjs, build sign-up and sign-in routes, then keep a person signed in: with a session cookie, or with a token (JWT) the way most APIs do, and know which fits when.
Updated
Passwords
- Storing Passwords Safely: Hashing (Journeyman): An app never keeps a password itself; it keeps a bcrypt hash, a one-way fingerprint made with a random salt and a slow cost factor, and it checks each sign-in with bcryptjs's compare.
- Sign-Up and Sign-In Routes (Journeyman): A sign-up route hashes the password, saves the student behind a unique index on email and answers 409 for an email that's taken, and a sign-in route checks the password with compare and answers one vague 401 for any mistake, never sending a hash back.
Staying Signed In
- Sessions and Cookies (Journeyman): A session keeps a student signed in the way a coat check does, with the record on the server and only a random ticket in the browser's cookie, and express-session sets it up with the HttpOnly, Secure and SameSite flags; it's the simpler choice for one website on its own server.
- Tokens: Signing In with a JWT (Journeyman): A JWT is a signed token the server hands out at sign-in and each request sends back in an Authorization Bearer header; jsonwebtoken's sign and verify make and check it, anyone can read its payload, and it fits APIs that apps and other servers call.
Exercise: Study Group Sign-In
- Exercise: Study Group Sign-In (Journeyman): Finish the study group's sign-up, sign-in and /me routes, saving only bcrypt hashes, refusing an email that's taken with 409, answering every sign-in mistake with one 401 and handing back a JWT that lasts an hour, and let Supertest's tests check every answer against a throwaway MongoDB.