devise_token_auth: Token-Based Authentication for Rails JSON APIs
Token based authentication for Rails JSON APIs. Designed to work with jToker and ng-token-auth.
At a glance
- What is it?
- devise_token_auth adds multi-client token authentication to Rails applications, refreshing tokens on each request, maintaining a separate session per client device, and supporting both email authentication via Devise and OAuth2 via OmniAuth.
- Who is it for?
- devise_token_auth is appropriate for Rails JSON API projects that need per-client token sessions and integration with front-end frameworks like Angular, React, or Flutter. It is not appropriate for server-rendered Rails applications where Devise's cookie-based sessions work fine.
- Can I use it commercially?
- Yes. WTFPL is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 61 days ago.
- What is it written in?
- Mainly Ruby, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on September 29, 2026, and from our analysis. They are not legal advice.
Editorial analysis
What devise_token_auth Solves and Who Needs It
Rails applications that serve JSON APIs to single-page applications or mobile clients face a fundamental authentication mismatch: the Devise gem was designed around cookie-based browser sessions, which do not work naturally for API clients. Token-based authentication is the standard alternative for these architectures.
devise_token_auth extends Devise specifically for this scenario. The gem provides token authentication with two properties the README highlights as its main design goals: tokens are refreshed on every request, reducing the window of exposure if a token is captured, and a separate session is maintained for each client-device pair, so a user can be logged in simultaneously from a phone, a desktop browser, and a tablet with independent sessions.
The last push to the repository was on 2026-07-31. The license is WTFPL.
Token Security Model: Rotation and Multi-Client Sessions
The core security mechanism is token rotation. On each successful authenticated request, the server generates a new token and returns it to the client. The client must use this new token for its next request. An old token is no longer valid after a successful response has been returned. This means that an attacker who captures a token in transit has only a small window before the legitimate client uses the token and invalidates it.
The multi-client model stores a separate token record per client device rather than a single token per user. A user account can have as many concurrent sessions as there are active clients. This contrasts with simpler implementations that store a single token per user and invalidate all sessions when the user logs in from a new device.
The security details are documented in docs/security.md within the repository. Anyone adopting the gem should read that file, particularly regarding the behavior of concurrent requests from the same client: if two requests are sent before the first response is received, one of them will fail because the token will have already rotated.
Installation and Basic Setup
Add the gem to your Gemfile:
gem 'devise_token_auth'Then install it:
bundle installThe gem depends on Devise and OmniAuth. Devise handles the user model, email authentication, password reset flow, and account confirmation. OmniAuth handles OAuth2 provider integrations. Both must be configured separately before devise_token_auth will function correctly.
The full setup documentation including the generator commands, route mounting, and model configuration is at devise-token-auth.gitbook.io/devise-token-auth. The README links to that documentation rather than duplicating the setup steps inline, so the gitbook is the primary reference for initial configuration. Support questions belong on StackOverflow under the devise-token-auth tag; GitHub issues are for bugs and enhancements only.
Front-End Client Library Integrations
The README lists six maintained front-end client libraries, each targeting a different framework. For AngularJS, ng-token-auth by the same original author (lynndylanhurley) provides the matching client-side token management. For the modern Angular framework, Angular-Token by neroniaky is the compatible client.
For React applications using Redux, redux-token-auth by kylecorbelli handles token storage and refresh. For jQuery-based applications, jToker by lynndylanhurley provides the same functionality. Two additional clients cover edge cases: vanilla-token-auth for applications that do not use any framework, and flutter_token_auth by diarmuidr3d for Flutter mobile applications.
The availability of these pre-built clients is one of devise_token_auth's practical advantages over rolling custom token authentication. Each client handles the token header format, the rotation logic, and error handling for expired or invalid tokens in a way that matches the server's behavior. The StackBlitz demo linked from the README shows the Angular-Token client running against a demo API.
Email Authentication Features from Devise
Because devise_token_auth builds on Devise, it inherits the full email authentication workflow. This includes user registration with email confirmation, account updates, account deletion, login, logout, and password reset via email.
The README describes support for multiple user models, which means a Rails application can have separate authentication domains for different user types (customers and administrators, for example) with separate token stores and separate route namespaces. The docs/usage/multiple_models.md file documents this setup.
OAuth2 authentication through OmniAuth allows users to log in with third-party providers. The gem acts as a bridge between OmniAuth's callback flow and the token issuance mechanism, so after a successful OAuth2 authentication the client receives the same token headers as after an email login. Which providers are available depends on which OmniAuth strategy gems are included in the Gemfile.
Limitations and Cases Where This Gem Is Not the Right Choice
The token rotation security model has a known failure mode for concurrent requests. If an API client sends multiple requests before receiving the first response, the second request may fail because the token has already been rotated by the first response. Applications that make parallel API calls from the client side need to handle this case, either by serializing authentication-sensitive requests or by adding retry logic for token-rotation failures.
devise_token_auth is a wrapper around Devise. Applications that do not already use Devise face the overhead of adopting both gems. Simpler alternatives exist for minimal token authentication without the Devise dependency, such as JWT-based authentication with a gem like devise-jwt. The README does not compare these options directly.
The WTFPL license is permissive to the point of being trivially open, which is typically fine for open-source projects but should be verified against any organizational policy that requires a specific OSI-approved license identifier. WTFPL is not in the OSI approved list.
Alternative: devise-jwt and Direct JWT Authentication
devise-jwt is a Devise extension that uses JSON Web Tokens instead of database-stored tokens. The architectural difference is significant: JWT-based authentication is stateless by default, meaning the server does not need to store token records in the database. This reduces database load for high-traffic APIs but introduces the well-known challenge of JWT revocation: once issued, a JWT cannot be invalidated before it expires without adding a blocklist, which reintroduces server state.
devise_token_auth stores tokens in the database and rotates them on each request. This is stateful and requires a database lookup on every authenticated request, but it allows immediate revocation: a logout deletes the token record and the session is gone instantly. The choice between the two approaches comes down to whether the application needs real-time session revocation and whether the additional database round-trip per request is acceptable at the expected traffic level.
Editorial conclusion
devise_token_auth is appropriate for Rails JSON API projects that need per-client token sessions and integration with front-end frameworks like Angular, React, or Flutter. It is not appropriate for server-rendered Rails applications where Devise's cookie-based sessions work fine. Before adopting it, confirm that your Rails version is compatible by checking the Appraisals file in the repository, and read the documented security model in docs/security.md to understand the token rotation behavior and how it interacts with concurrent requests.
Frequently asked questions
What is an auth token in the context of devise_token_auth?
devise_token_auth issues a short-lived token to the client after login and rotates it on every subsequent authenticated request. The client must send the current token in request headers and update to the new token returned in each response. Each device or client gets its own independent token record.
How does devise_token_auth differ from standard Devise?
Standard Devise uses cookie-based browser sessions designed for server-rendered applications. devise_token_auth replaces cookies with rotating token headers designed for JSON API clients such as single-page applications and mobile apps. It requires Devise as a dependency and adds the token model and route endpoints on top of it.
Does devise_token_auth work with Rails 7?
The README does not specify a Rails version constraint, but the repository includes an Appraisals file that lists the gemfile combinations used in CI testing. Checking that file against your Rails version is the reliable way to confirm compatibility before adding the gem to a new project.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/lynndylanhurley-devise-token-auth)