Project

easy_login

0.0
No commit activity in last 3 years
No release in over 3 years
a simple session controller which just including :sign_in, :sign_out, :sign_in?, :current_user, :current_user? for controllers and :current_user, :current_user?, :sign_in? for views
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.10
~> 10.0

Runtime

< 2.0, >= 1.8
 Project Readme

EasyLogin

A simple session controller which just including following method for controllers

  • :sign_in
  • :sign_out
  • :signed_in?
  • :current_user
  • :current_user?

And forllowings for veiw helper

  • :current_user
  • :current_user?
  • :signed_in?

Installation

Install it yourself as:

$ gem install easy_login

And then run command to init a config file

$ bundle exec rails generate easy_login:init

Usage

Add config in config/application.rb or config/environments/*.rb

EasyLogin.setup do |config|
  config.salt = "same_salt_string"
  config.user_model = "your user model such as 'User'"
  config.user_role_attr = "your role attribute in user model such as 'role'"
end

Add following code to application_controller.rb

include EasyLogin

And then abosultely use all methods above in controller and view

Also you can declare a redirect schema for differenct user accessing differect controller and action by editing config file config/http_permissions.yml

The following is a default code of config/http_permissions.yml

default:
  ope: pass
  customer: pass
  unsigned: redirect_to->login_user_path
users:
  login:
    ope: redirect_to->root_path
    customer: redirect_to->root_path
tools:
  default:
    customer: raise->Routing Error
    unsigned: redirect_to->/tools/login

The basic format for this permission schema is like

controller:
  action:
    user_role1: pass
    user_role2: redirect_to->{path}
    user_role3: raise->{message}

The user_role is the role attribute of your user model configed in config/application.rb or config/environments/*.rb
And also you need define a enum about this role like:

class User < ApplicationRecord
  has_secure_password

  enum :role => {
    :user_role1 => 0,
    :user_role2 => 1,
    :user_role3 => 2
  }
end

The action has 3 types

pass --> run the logic in action normally
redirect_to --> redirect to other {path}, {path} also has two types, {xxx_path} means using rails routing method, and {other format} means used as absolute url string
raise --> raise a 404 response with {message}

If the schema was not defined in controller/action/user_role, the schema in controller/default/user_role will be used, Also, if it was not defined in controller/default/user_role, default/user_role will be used, if still default/user_role could not be found, easylogin will do action same as 'pass'

※ If you also want to use in ActionCable in Rails 5
Add following code to application_cable/channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base
    include EasyLogin
  end
end

And then use helper method easy_login_session in the view where you want to use cable, so that you can get session param f in cable js when create connection like following.

App.channel = App.cable.subscriptions.create {channel: "Channel", f: $('#easy_login_session').attr('f')},

And then you can access authorized user in other Channel with client

※ If you also want to use in GrapeAPI (just authorize with cookies like controller, not support omini auth)
Add following code to your root api class extends Grape::API such as api/root.rb

include EasyLogin

And then you can use some methods below in this and any other sub classes of api

  • signed_in?
  • current_user
  • current_user?(user)
  • authorize! --> response 403 error and json if authorization failed

If you want auth for every api request, write like following

after_validation do
  authorize!
end

License

The gem is available as open source under the terms of the MIT License.