Guide ·

Flutter JWT Authentication with a Node.js API: Login, Refresh Tokens and Logout

How to add JWT login to a Flutter app with a Node.js API: short-lived access tokens, rotating refresh tokens, secure storage, auto-refresh and logout.

By Ritesh Singh, full-stack developer

Almost every business app needs a login, and the usual way to do it with a Flutter app and a Node.js backend is with JSON Web Tokens (JWT). The basic version is quick to build; the problems show up later, when tokens expire in the middle of a form or users stay logged in after their access is removed. This guide covers the setup I use so that login stays smooth for users and safe for the business.

1. Two tokens, not one

Use a short-lived access token and a long-lived refresh token. The access token is a signed JWT that the app sends with every API call; it expires quickly, for example after 15 minutes, so a leaked one is useful only for a short time. The refresh token lasts much longer, for example 30 to 60 days, and has one job: getting a new access token without asking for the password again.

Make the refresh token a long random string rather than another JWT, and store only a hash of it in a refresh_tokens table with the user ID, a device name, the expiry date and a revoked flag. Because the server keeps a record of it, you can end a session at any time, which a plain JWT on its own cannot do.

2. The login endpoint in Node.js

The POST /auth/login route checks the email or phone number and the password against a hash made with bcrypt or Argon2, never a plain or reversible password. Return the same error message for an unknown user and a wrong password, so the endpoint does not reveal which accounts exist, and add rate limiting by IP and by account to slow down password guessing. For OTP logins, limit how often codes are sent and how many attempts each allows.

On success, it returns both tokens and the basic profile the app needs for its first screen.

3. What goes inside the access token

Keep the payload small: user ID, role, a token version and the expiry. A JWT is signed, not encrypted, so never put personal data inside. Sign it with a long secret stored in an environment variable, and when verifying with the jsonwebtoken package, pass the allowed algorithm explicitly, for example algorithms: ['HS256'], so the server never accepts a token signed some other way.

A token version logs a user out everywhere at once: store a number on the user row, copy it into each token, and increase it when the password, role or account status changes, so older tokens stop working at the next refresh.

4. Refresh token rotation

When the app calls POST /auth/refresh, the server looks up the hash of the refresh token, checks it is not expired or revoked, then issues a new access token and a new refresh token, and marks the old one as used. This is rotation. If a used refresh token ever comes back, someone may have copied it: revoke that device's sessions and ask the user to log in again.

Phones on poor networks sometimes send the same refresh twice, so allow a grace period of a few seconds in which the previous token returns the same new pair.

5. Storing tokens in the Flutter app

Store the refresh token with flutter_secure_storage, which uses the Keychain on iOS and encrypted storage backed by the Android Keystore. Never use SharedPreferences, which is plain storage. Keep the access token in memory and get a fresh one with the refresh token when the app starts. iOS Keychain items can survive an app reinstall, so clear old tokens on the first launch.

6. Refreshing automatically with a Dio interceptor

Users should never see an error just because a token expired. With the dio HTTP package, an interceptor adds the Authorization: Bearer header to every request. When a response comes back with status 401, the interceptor calls the refresh endpoint, saves the new tokens and retries the original request once.

The important detail is to allow only one refresh at a time. If several requests each start their own refresh, rotation treats all but the first as reuse and logs the user out. Keep a single pending refresh, for example with a Completer, make other failed requests wait for it, then retry them with the new token. If the refresh itself fails, clear the tokens and send the user to the login screen with a short message.

7. Roles and permissions are checked on the server

The role in the token only helps the app choose which tabs and buttons to show. Every API route must still check permissions and limit queries to the records the user may see. My guide on role-based access control in Next.js and Node.js shows how to enforce this in one place for every route.

8. One login API for the app and the web dashboard

Most products I build have a mobile app and a web dashboard on one backend, as in the legal case management platform, so the login rules are shared too. Only storage differs: the Flutter app keeps the refresh token in secure storage, while the browser gets it in an httpOnly, Secure cookie that JavaScript cannot read. I explain the wider approach in one API for a Flutter app and web dashboard.

9. Logout, devices and push notifications

Logout should call the API to revoke the current refresh token, not just delete it from the phone. A device list built from the refresh_tokens table lets users end the session on a lost phone. If the app uses push notifications, delete the device's FCM token on logout too, so the next person to use the phone does not receive the previous user's messages. My Flutter push notifications guide covers how device tokens are stored.

10. Test cases before release

Test more than a successful login: a blocked account, a token expiring during use, several requests failing at once, a reused refresh token, a password change on another device and logout with no network. Also prepare a working demo account, because Apple's App Review asks for sign-in details for any app with a login, as I mention in my guide on publishing a Flutter app on the Play Store and App Store.

Flutter JWT authentication checklist

  • Short-lived access JWT and a long-lived random refresh token.
  • Refresh tokens stored hashed on the server, per device, with expiry and revoke.
  • Passwords hashed with bcrypt or Argon2; rate limits on login and OTP.
  • Small token payload, secret in an environment variable, algorithm fixed on verify.
  • Token version to log a user out everywhere after a password or role change.
  • Refresh token rotation with reuse detection and a short grace period.
  • Tokens in flutter_secure_storage, never in SharedPreferences.
  • One Dio interceptor that refreshes once and retries waiting requests.
  • Permissions checked on every API route, not only in the app.
  • Server-side logout, device list and FCM token cleanup.
  • Edge cases tested and a demo account ready for store review.

Building a Flutter app that needs a secure login and a backend to match? I build the app and the Node.js or PHP API together, so authentication works the same way on mobile and web. See my Flutter app development service and Node.js and PHP backend development, or send a short project brief.

In practice

Case studies using this approach

Keep reading

Related guides

Related services

Hire me for this

Want this done for your product?

Start your project →