Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise before filter that prevents access to "new_user_registration_path" unless user is signed-in

I am using Devise in my rails app. My Users model is registerable, which means that anyone can go to /users/sign_up and create a new account.

Is it possible to protect this route, so that only signed_in users can create new accounts?

like image 409
stephenmurdoch Avatar asked Nov 18 '10 08:11

stephenmurdoch


2 Answers

Create a Controller with class Devise::RegistrationsController heriting. After you can add your filter. You just need define this controller like registration controller

class RegistrationsController < Devise::RegistrationsController
  before_filter :authenticate_user!
end

In your routes.rb

devise_for :users, :controllers => { :registrations => 'registrations'}
like image 54
shingara Avatar answered Oct 14 '22 16:10

shingara


It didn't worked for me because authenticate_user! is not getting called.

I fixed it that way :

class RegistrationsController < Devise::RegistrationsController
    before_filter :prevent_sign_up

private
    def prevent_sign_up
        redirect_to new_user_session_path and return
    end
end
like image 39
nverinaud Avatar answered Oct 14 '22 15:10

nverinaud