Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove/Disable Sign Up From Devise

I'm trying to remove/disable the user/sign_up path from Devise. I'm doing this because I don't want random people gaining access to the application. I have it partly working by adding the following in routes.rb

Rails.application.routes.draw do
  devise_scope :user do
    get "/sign_in" => "devise/sessions#new" # custom path to login/sign_in
    get "/sign_up" => "devise/registrations#new", as: "new_user_registration" # custom path to sign_up/registration
  end

...

devise_for :users, :skip => :registration

end

However, this breaks <%= link_to "Profile", edit_user_registration_path, class: "btn btn-info btn-flat" %>

which I want to keep so that users can update their profile. I know it's because of the devise_for :users, :skip => :registration

Is there a solution for this issue?

Running

Devise (4.2.0, 4.1.1, 4.1.0)

Rails 4.2.5

ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]

like image 465
M.T Davis Avatar asked Sep 29 '16 15:09

M.T Davis


4 Answers

The easiest way is just removing :registerable devise module from the default list defined into your Model (the class name used for the application’s users, usually User).

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  ...
end

So you'll have it like this:

class User < ActiveRecord::Base
  devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable

  ...
end
like image 112
jcgil Avatar answered Nov 17 '22 12:11

jcgil


Solution to removing sign_up path from Devise

Enter the following at the beginning of routes.rb

Rails.application.routes.draw do
  devise_scope :user do
    get "/sign_in" => "devise/sessions#new" # custom path to login/sign_in
    get "/sign_up" => "devise/registrations#new", as: "new_user_registration" # custom path to sign_up/registration
  end

...After the statement above, add the following below in routes.rb

devise_for :users, :skip => [:registrations] 
  as :user do
  get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
  put 'users' => 'devise/registrations#update', :as => 'user_registration'
  end

This will remove/disable the user/sign_up path for Devise without breaking edit_user_registration_path

Restart your rails server and it should work.

like image 28
M.T Davis Avatar answered Nov 17 '22 14:11

M.T Davis


I just had the same issue. My solution is a mix of these answers.

  1. Comment out or remove :registerable in user.rb:
class User < ActiveRecord::Base
  devise :database_authenticatable, #:registerable,
         :recoverable, :rememberable, :trackable, :validatable
end
  1. Remove the registration paths from devise_for in routes.rb:
devise_for :users, :skip => [:registrations], controllers: {
  sessions: 'users/sessions'
}

Now Devise will skip all of the registration links from their view and also you no longer have the registration paths on your routes.

like image 9
David Roth Avatar answered Nov 17 '22 14:11

David Roth


Since as is just an alias for devise_scope, you can put all that in just one block.

devise_for :users, skip: [:registrations]
as :user do
  get "/sign_in" => "devise/sessions#new" # custom path to login/sign_in
  get "/sign_up" => "devise/registrations#new", as: "new_user_registration" # custom path to sign_up/registration
  get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
  put 'users' => 'devise/registrations#update', :as => 'user_registration'
end
like image 5
Tomislav Mikulin Avatar answered Nov 17 '22 12:11

Tomislav Mikulin