Devise Multiple Resources, Registrations, STI and Single Sign Sign On
-
Upload
sandip-ransing -
Category
Documents
-
view
215 -
download
0
Transcript of Devise Multiple Resources, Registrations, STI and Single Sign Sign On
-
8/3/2019 Devise Multiple Resources, Registrations, STI and Single Sign Sign On
1/3
Multiple resources, registrations with devise, STI and single sign sign on
Devise handles authentication, authorization part inside rails application quite easily and itscustomizable too. One can always customize default devise configurations.
This Post will show how to manage multiple resources (like admin, staff, employee, guests etc.)
through devise and STI with individual registrations process but login section will be the same forall.
# Gemfilegem 'devise'
# consolebundle installrails g devise_install
rails g devise Userrake db:migraterake routes
# User modelclass User < ActiveRecord::Base
# Include default devise modules. Others available are:# :token_authenticatable, :lockable, :timeoutable, :confirmable and :activatabledevise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your modelattr_accessible :email, :password, :password_confirmation
end
## Single Table Inheritance
# Admin modelclass Admin < Userend
# Staffclass Staff < Userend
# Employeeclass Employee < Userend
# Guestclass Guest < Userend
www.funonrails.com github @sandipraning twitter @sandipransing
http://www.funonrails.com/http://www.funonrails.com/ -
8/3/2019 Devise Multiple Resources, Registrations, STI and Single Sign Sign On
2/3
# routes
devise_for :users, :skip => :registrationsdevise_for :admins, :skip => :sessionsdevise_for :staffs, :skip => :sessions
devise_for :employees, :skip => :sessionsdevise_for :guests, :skip => :sessions
# Customizing default login/logout routes, views, actions
devise_for :users, :controller => {:sessions => 'sessions'}, :skip => [:sessions, :registrations] dodelete '/logout', :to => 'sessions#destroy', :as => :destroy_user_sessionget '/login', :to => 'sessions#new', :as => :new_user_sessionpost '/login', :to => 'sessions#create', :as => :user_session
end
# app/controllers/sessions_controllerclass SessionsController < Devise::SessionsControllerend
## Overriding default after sign in path
# app/controller/application_controller.rbclass ApplicationController < ActionController::Base
protect_from_forgeryhelper_method :account_url
def account_urlreturn new_user_session_url unless user_signed_in?case current_user.class.namewhen 'Customer'edit_customer_registration_url
when 'Admin'edit_home_page_section_url
else
root_urlend if user_signed_in?end
end
# app/controllers/sessions_controller.rbclass SessionsController < Devise::SessionsController
def after_sign_in_path_for(resource)stored_location_for(resource) || account_url
end
end
www.funonrails.com github @sandipraning twitter @sandipransing
http://www.funonrails.com/http://www.funonrails.com/ -
8/3/2019 Devise Multiple Resources, Registrations, STI and Single Sign Sign On
3/3
## Changing default login field email to username# config/initializers/devise.rbconfig.authentication_keys = [ :username ]
# app/models/user.rbvalidates :username, :presence => true,:uniqueness => {:allow_blank => true},:format => {:with => /^\w+[\w\s:?']+$/i, :allow_blank => true}
def email_required?false
end
www.funonrails.com github @sandipraning twitter @sandipransing
http://www.funonrails.com/http://www.funonrails.com/