Hey folks, I recently asked about favourite tech stacks among solo developers . The answers included a healthy mix of batteries included frameworks like Rails and Django along with NodeJS. How are you handling authentication in NodeJS applications? Are you using external services or rolling out your own auth layer? I would love to know what open source Node libraries folks are using for auth these days!
http://www.passportjs.org is what I use.
External service: Authress, handles the mess that comes with authorization tokens, CORS, multiple sign-ins, session management. Also handles access control and permissions management which we couldn't find anywhere else.
One thing that's important to call out is that things like passport created a single point of failure within our architecture and wasn't ready to scale up like we needed.
I used to roll my own auth layer. Bad idea. Now I am always use firebase and I've been a happy camper ever since!
Why it's a bad idea?
If you are using firebase for other parts of your solution, e.g. as a database provider or as a cloud platform, you may as well use their authentification service too and save yourself some time.
Making a secure and fast auth layer is hard and time-consuming work that you could (in parts) save yourself with firebase.
I would certainly recommend at least taking a look at it, firebase has a generous free tier too.
Hasura Backend Plus
I'm always integrating 3rd party auth, if I'm not using Django. Firebase or Auth0 are really great solutions. I really can't recommend them enough.
I used jsonwebtoken package in NodeJs and stored them in Redis.
I've thought about rolling out my own but then stopped when I consider how much work it is. Going a little bit beyond the standard auth mechanic i.e. managing access tokens, encrypting passwords, implementing basic CORS. You also need to implement password reset flows, send out email verification links etc. Login and registration screens need validation, you need to check and handle errors coming back from the server (e.g. duplicate email addresses, malformed email addresses). Enforce good password structure and prevent against brute force attacks.
And that's just to get you up and running, on top of that you probably want analytics e.g. to track logins, and be able to offer multiple sign-in methods e.g. Google.
Doing all this from scratch and doing it without leaving any holes is pretty gnarly, and is another thing to maintain and take you away from developing core app logic.
I've used Auth0 in the past which was pretty straight forward, but if you want to go down the self-hosted route, KeyCloak is an excellent open source oAuth server and it does the job pretty well (gives you everything I mentioned above out the box). On top of a browser client, it also has Node-Connect which gives you authentication middleware so you can call authenticate() on any route. I like having my auth logic totally separate to my app, feels more self-contained that way. It takes a while to set up and there's a bit of a learning curve but once that's done you can set and forget, it just works. I hear Firebase is good too.
Just a note, CORS is orthogonal to authentication. You can avoid worrying about CORS if your authentication API is on the same hostname as your web app — and conversely, you’ll have to to deal with CORS for any requests you make to other hostnames, not just authentication-related ones.
I use session auth with Passport.js
Here's the collection of libraries I use for auth:
Authentication is pretty easy to solve, even when you add in OAuth providers like Google, Twitter, Facebook, etc, so Ive rolled my own. The flow is basically:
Authorization on the other hand, is a bit more difficult and will depend on the requirements of your app. Building and modeling ACLs (access control lists) for things can get a bit hairy at times, but Ive always tended to roll my own.
Same, but with cookies. Remember to either use short-lived JWT or check them against your application state!