THIS PROJECT IS DEPRECATED
devise-rails-api-authentication is not maintained anymore.
Token-based rails-api authentication with Devise.
Devise does not support token-based authentication anymore and devise_token_auth does not work with rails-api.
It's basically this gist wrapped as a gem with specs.
Installation
Add this line to your application's Gemfile:
gem 'devise_rails_api_authentication'
And then execute:
$ bundle
Or install it yourself as:
$ gem install devise_rails_api_authentication
Usage
First of all make sure you have a model class with authentication_token
and email
fields.
Otherwise create a migration, here's an example for a User
model and ActiveRecord:
class AddDeviseToUsers < ActiveRecord::Migration
def self.change
add_column :users, :email, :text, null: false, default: ''
add_index :users, :email, unique: true
add_column :users, :authentication_token, :text, null: false, default: ''
add_index :users, :authentication_token, unique: true
end
end
Next add the following lines to ApplicationController - see source:
class ApplicationController
include DeviseRailsApiAuthentication::Context
private def user
User.where(email: user_email).first
end
end
and the following line to your model class - see source:
class YourUserModel
include DeviseRailsApiAuthentication::Authenticatable
end
Once you run the migration, create a new user, and boot up your app, just do:
curl -H "X_USER_EMAIL: <email>" -H "X_USER_TOKEN: <token>" http://<where-your-app-lives>
Contributing
- Fork it ( https://github.com/altmetric/devise_rails_api_authentication/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
License
This project is released under the MIT license.