UserManagementRails
Short description and motivation.
Usage
How to use my plugin.
Gem has next methods:
decode_user(jwt)
- expects jwt with user and returns decoded payload(in our case user info).
valid_jwt?(jwt)
- expects jwt and checks if it is valid. Return true
when it is valid, otherwise returns false
Gem adds next endpoints to your application:
/logins
which has next methods:
POST
- expects. It checks if jwt is valid and if so, then save it to the cookies called 'jwt'. if jwt is not valid then it delete it from the cookies. When jwt was added to cookies than it will be sent with every request.
Gem provides current_user_hash method for your controller by adding next line to it:
include UserManagementRails::Concerns::UserResource
It has one optional param raise_jwt_error
which when true (by default) will rise exception if user jwt has expired.
If you don't want to raise exception then pass false
and when token is expired then jwt will be deleted for cookies.
How it works:
- If there is no jwt in cookies then return null
- If jwt is invalid or expired and raise_jwt_error is true then it raise an error
- If jwt is invalid or expired and raise_jwt_error is false then it return nil and delete jwt from cookies.
- If there is valid and not expired jwt in cookies then it returns a hash of user details
Installation
Add this line to your application's Gemfile:
gem 'user_management_rails'
And then execute:
$ bundle
Next, you need to run the generator:
$ rails generate user_management_rails:install --ringcaptcha-key=RINGCAPTCHA_APP_KEY --um-key=UM_KEY [--mode=OTP|PASSWORD]
This will add the necessary controller, view, and routes. You can update the files according to your needs.
Default routes installed:
$ rake routes
Prefix Verb URI Pattern Controller#Action
user_management_rails / UserManagementRails::Engine
home GET /home(.:format) user_management#home
login GET /login(.:format) user_management#login
root GET / user_management#signup
Routes for UserManagementRails::Engine:
logins POST /logins(.:format) user_management_rails/logins#create
Requirements
In your routes.rb file you should specify where /login
endpoint will be mount.
Example:
mount UserManagementRails::Engine, at: '/'
in this case gems '/login' endpoint will be mounted at: domain.com/logins
.
If you already have a '/login' endpoint you can isolate it by
mount UserManagementRails::Engine, at: '/some_path'
Then gems /login
endpoint will be mounted on domain.com/some_path/logins
- rails generator adds this route by default as stated above.
Adding new version of gem to rubygems repository
After you made changes and increment version of gem in(/lib/user_management_rails/version.rb) you need to build and push new gem file to rubygems.org:
gem build user_management_rails.gemspec
gem push user_management_rails-<VERSION_OF_GEM>.gem
License
The gem is available as open source under the terms of the MIT License.