简体   繁体   中英

CanCanCan gem with Devise + Omniauth Ruby on Rails

Im using an Omniauth + Devise authentication system, where the user can register with his e-mail + password or with his Google+ account.

Now I need to use CanCanCan gem to check if the user that is loging in has permissions to go to the after login area, but I dont know where i can do that condition, in which file Devise stores the redirect after sucessfull login function?

You have to override Devise registration controller.

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    '/home' # your path to redirect after signup
  end
end

You can define the access permissions in abilities.rb file.

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= user.new

    # Here you can define the permissions for home page for user
  end
end

You can implement the after_sign_in_path method in your Application Controller, where resource is your user:

class ApplicationController < ActionController::Base
  def after_sign_in_path_for(resource)
    if resource.can? :show, ProtectedResource
      protected_area_path
    else
      denied_access_path
    end
  end
end

This will tell Devise where to redirect your user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM