r/webdev • u/dhd_jpg • Oct 25 '26
Question how do you build authentication from scratch?
hi everyone! so i only have a semester left in college and we are now currently working on our capstone project. it’s a 2 part class so this semester focuses only on the “planning” phase while next semester we spend entire semester actually building/coding.
for background, we are a 3 person group, BaaS such as supabase, firebase or auth0 are banned, we have 3 user groups with different roles and our stack includes:
* JS, Node.js/Express
* React
* PostgreSQL
we don’t have much experience on how to build authentication from scratch so anyyyy tips/tutorials/resources are very much appreciated!!
TIA guys :))
51
u/-Tochi Oct 25 '26
The legend Web Dev Simplified made a video about this a few days ago
https://www.youtube.com/watch?v=FbXPaIcpvww
164
u/codegems Oct 25 '26
Pick a framework that implements authentication and study its code.
15
u/tjthouhid1 Oct 25 '26
For a capstone on Express + React + Postgres, I'd skip JWTs and do plain server-side sessions. It's simpler and harder to get wrong:
express-sessionwithconnect-pg-simpleso sessions live in Postgres. Cookie set tohttpOnly,secure,sameSite: 'lax'.rolecolumn (or arolestable if a user can have several), plus a small middleware likerequireRole('admin')on each route. Always check on the server; hiding buttons in React isn't security.For reading, The Copenhagen Book is a short, practical guide to exactly this, and the OWASP Authentication and Session Management cheat sheets are great to cite in your planning doc.
28
u/canadian-dev Oct 25 '26
I haven't used it in quite awhile but I remember this being a really good and well written resource on auth
https://auth.pilcrowonpaper.com/
6
u/Electronic_Many_2658 Oct 25 '26
If you build it yourself, add the boring stuff people forget. Rate limit login attempts per account and per IP, hash with bcrypt or argon2, and use the same error message whether the email or the password was wrong. Bots will hit any login form within days trying leaked passwords, so log failed attempts from day one so you can see it happening. Also kill active sessions when someone changes their password.
3
u/Gremlation Oct 25 '26
Read the Copenhagen book and API Tokens: A Tedious Survey.
2
u/kemalios Oct 25 '26
Your login will be fine. The reset flow is what gets forgotten: a reset token is a second credential, so random, single use, stored hashed, with an expiry, and using it should kill existing sessions. Same for logout. A stateless JWT with a 24 hour life and a button that clears the cookie is not a logout. Either put sessions in Postgres as rows you can delete, or keep a token version per user and bump it.
Three roles now, but the demo will get "can we add an admin". Roles in their own table with a join, not an enum column on users, and one middleware that turns a user into permissions so handlers never check role names. Costs an afternoon now, a rewrite later.
3
u/RobHowdle Oct 25 '26
I built my own authentication. My Auth is a centralised authentication and authorisation service that gives applications a consistent way to establish trust and access protected resources.
The flow is generally: an application authenticates with my auth system using its configured credentials, receives or obtains an appropriate access token, and uses that token when requesting access to protected services. The receiving service validates the token and checks the permissions before granting access.
Traditionally, each application might implement and maintain its own authentication and access-control logic. The auth system centralises this process, making it easier to manage permissions consistently, integrate new applications and reuse a common authentication layer.
The key benefit is that applications can authenticate and access authorised resources through a shared system without having to rebuild the same infrastructure for every project, while keeping their own data and business logic separate.
2
u/armahillo rails Oct 25 '26
If it were me, my recommendation is:
- Start with a very basic resource-based app, like a “ user can write a note and save it to a DB”
- Now add the comstraint “the user can authenticate with the system and the DB connects their auth record to their note records”
- Now add authorizatiom “a user can only see their own notes, even if they have the exact URL for another user’s note”
Approaching it this way will help you incrementally understand the difference between authentication (identifying the user) and authorization (assigning permissions to the user)
32
u/bronze_by_gold Oct 25 '26
“Claude. Build authentication from scratch. Make no mistakes.”
3
u/jacobdevhq Oct 25 '26
hash passwords w argon2 or bcrypt, dont write ur own crypto. httpOnly session cookie, n put the role check in one middleware so ur 3 user types dont leak into every route. add login rate limiting early, its the thing ppl forget
2
u/battering_ram Oct 25 '26
Poorly
0
u/Tri-Stain Oct 25 '26
YouTube would help give you a bunch of examples, can look at these easiest auth imo for this would be using logical groups assigned with basic auth
2
u/jax024 Oct 25 '26
I’ve found writing auth in Go with the stdlb to be a good way to learn fundamentals
1
u/pedrorijo91 Oct 25 '26
I wrote something a few years ago https://pedrorijo.com/blog/scala-play-auth/
It’s a scala example, but easy to convert to other languages
1
u/Random-num-451284813 Oct 26 '26
Is openssl allowed? is well documented
1
u/gowthamshankar05 Oct 27 '26
for a capstone i'd keep it boring. users table with email and a password hash (argon2 or bcrypt from a library, don't write the hashing yourself). on login check the hash, make a random session id, save it in a sessions table and send it back as an httpOnly secure cookie. logout just deletes the row.
honestly sessions are easier to get right than JWT for this. you can revoke them and there's less to mess up.
rate limit login attempts. same error for wrong email and wrong password. reset flow with a token that expires. skim the OWASP authentication cheat sheet once, it's short.
1
u/NoDepartment772 Oct 31 '26
The two decisions that matter most, in order:
How you store passwords. argon2id if you can (Node's
argon2package), bcrypt with a cost of 12+ if you can't. Never a plain SHA-256, and never anything reversible. Both libraries handle the per-user salt for you. Hash on the server, compare with their verify function, don't roll your own comparison.Whether the session is a database row or a token. For a capstone with three roles, I'd do sessions: a random 128-bit id in an
httpOnly; Secure; SameSite=Laxcookie, and asessionstable holding a hash of that id, the user_id, and an expiry. Logout is aDELETE, "log out everywhere" is aDELETE WHERE user_id = ?, and a stolen cookie can be killed. JWT gets you statelessness you don't need here, and the moment you want revocation you're back to a server-side store anyway — with more moving parts.Then the things that are easy to forget and get marked on:
localStorage. It's readable by any script on the page.The OWASP Cheat Sheet Series has a page for each of these and it's the right thing to cite in your report. Read the session management and password storage ones before you design the schema — it's much cheaper than discovering the gap at review.
1
u/DoNotEverListenToMe Oct 25 '26
Why would you dude?
1
u/hotmilkramune Oct 25 '26
How "from scratch" is from scratch? BetterAuth is a decent library for auth that does a lot of heavy lifting, not sure if that's allowed. If not you'll have to build your own. Look into OAuth2, it's the established standard for most logins that involves a user requesting a login, server returning a code, user exchanging code for a token via POST request, and finally getting a JWT token with claims.
0
u/General_Patient4904 Oct 25 '26
You can build it using an API to authenticate users against your users database table, with proper authorization and some additional logic for email verification.
-2
u/nicolix9 Oct 25 '26
You should checkout passport.js
-1
u/glenpiercev Oct 25 '26
Think about “threat actors”. What kinds of people/organizations might want to break the system you’re building and how could they? Then think about how to defend against that.
-2
u/Gaboik Oct 25 '26
WorkOS
-3
u/DiscombobulatedBet88 Oct 25 '26
If you have to ask, dont. Use something prr build in your framework
-4
u/rbobby full-stack Oct 25 '26
I think you should push back against the teacher. Authentication is the one area where I personally cannot recommend an outsourced oauth2.0 based solution. Auth0 is a great example.
Teaching how to build your own authentication system is like teaching target practice to the suicidal.