How to Remove/Disable Sign Up From Devise

前端 未结 6 1274
余生分开走
余生分开走 2021-02-05 02:56

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 p

相关标签:
6条回答
  • 2021-02-05 03:00

    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
    
    0 讨论(0)
  • 2021-02-05 03:13

    Below code seem to do the trick for me:

    Rails.application.routes.draw do
    
      devise_scope :users do #notice "users" here, not "user"
        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 => [:registrations]
      as :user do
        get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
        put 'users' => 'devise/registrations#update', :as => 'user_registration'
      end
    ...
    
    0 讨论(0)
  • 2021-02-05 03:14

    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.

    0 讨论(0)
  • 2021-02-05 03:19

    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
    
    0 讨论(0)
  • 2021-02-05 03:22

    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.

    0 讨论(0)
  • 2021-02-05 03:22

    Redirecting from controller

    I solved this issue by redirecting /sign_up to /sign_in from the controller, while preserving the functionality of editing user info. For instance:

    In controllers/users/registrations_controller.rb

    # GET /resource/sign_up
      def new
        redirect_to new_user_session_path and return
        super
      end
    

    In routes.rb, I pointed registrations resource to this controller:

      devise_for :users, controllers: {
        sessions: 'users/sessions',
        registrations: 'users/registrations'
      }
    

    So whenever users visit route /sign_up, it'll redirect them to /sign_in. Just remember to use and return after the redirection to prevent multiple render/redirect

    0 讨论(0)
提交回复
热议问题