A simplistic jwt authentication for rails api.
Add this line to your application's Gemfile:
gem 'kp_jwt'And then execute:
$ bundleOr install it yourself as:
$ gem install kp_jwtRun the install generator:
$ rails generate kp_jwt:install
It will create an initializers, and migration file.
Next, create auth controller for specific entity by running the following command. e.g. admin, user, etc...
$ rails generate kp_jwt:auth_controller user
If you have not create your entity model, run the following command(e.g. admin, user, etc...):
$ rails generate kp_jwt:model user
else create a model that has method authenticate and include include KpJwt::Model or uses has_secure_password like the following code:
class User < ActiveRecord::Base
has_secure_password
include KpJwt::Model
endInclude the KpJwt::Auth module in your ApplicationController
class ApplicationController < ActionController::Base
include KpJwt::Auth
endSimply add authenticate_user! before action to the actions you need to secure
class SecuredController < ApplicationController
before_action :authenticate_user!
endPOST /user_auth
params: {
auth: {
email: 'ex@ample.com',
password: 'password123'
}
}
User current_user to access current user data model
current_user.email
current_user.child_models- Fork it ( https://github.com/kevinponce/kp_jwt )
- 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
The gem is available as open source under the terms of the MIT License.