Storing JWT refresh token in httpOnly cookie

So, I'm building the auth part of an application. I have always return both access and refresh tokens on the payload. And them have them stored separately on the client. Now, after doing a little research, I have found that some people return just the access token on the payload, and have the refresh token set as an httpOnly cookie. What is the opinion on this? Good practice? Bad practice? Unnecessary / anti pattern? Thanks in advance!
13 Replies
Jaaneek
Jaaneek13mo ago
Set both as http only cookie, do not expose them to the FE at all If you can you can also look to outsource auth altogether. Services like clerk, supabase and many others are amazing when it comes to it.
foenix
foenix13mo ago
Or just set access as httpOnly and do not expose refresh to client. It's enough to store it on server side if you are using NextAuth or similar library
barry
barry13mo ago
JWT's are terrible for 99% of web applications. httpOnly cookies prevent XSS but the lack of an anti CSRF measure means you're vulnerable to CSRF, can't be solved with standard JWT implementation
Matvey
Matvey13mo ago
Storing a refresh token as an http-only cookie and an access token in a javascript variable is a great auth strategy and it prevents both XSS and CSRF attcks
gustagol
gustagolOP13mo ago
even if the access token is short lived?
barry
barry13mo ago
Yeah but still need something that's not sent along automatically by the browser, aka. anti csrf token
gustagol
gustagolOP13mo ago
do you know any blog posts or videos about that? I have only ever used JWT and am kinda new to back-end, would love to learn more about that
barry
barry13mo ago
And I would never outsource authentication Personally, I want to own the whole process, outsourcing doesn't make sense other than OAuth. There's a truth to not needing to reinvent the wheel, and there's libraries for auth to use, but I'd still like to own the fucking wheel. Not hand it over and pay a premium.
Matvey
Matvey13mo ago
I think this video describes the authentication strategy you are talking about, with JWTs and refresh/access tokens - https://youtu.be/iD49_NIQ-R4
Ben Awad
YouTube
How to Store JWT for Authentication
Learn how you can store your JWT in memory instead of localStorage or a cookie for authentication. This method limits your exposure to CSRF and XSS attacks. #jwt Links from video: https://blog.hasura.io/best-practices-of-using-jwt-with-graphql/ https://www.owasp.org/index.php/Cross-site_Scripting_(XSS) https://www.owasp.org/index.php/Cross-Sit...
barry
barry13mo ago
Just stay away from JWT's is my advice. Also the advice from OWASP
Matvey
Matvey13mo ago
If you use TypeScript for the backend, I would just use something like Lucia Auth, instead of doing everything from scratch.
barry
barry13mo ago
Either that (lucia-auth) or Oslo also by pilcrown, auth utils https://github.com/pilcrowOnPaper/oslo
Want results from more Discord servers?
Add your server