From bb6566a45eaa83704a2c7b018af0df81852398c1 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Wed, 19 Oct 2016 12:40:52 +0800 Subject: [PATCH] clean up and fix devise code --- app/controllers/application_controller.rb | 18 ++---------------- app/controllers/users/passwords_controller.rb | 2 +- .../users/registrations_controller.rb | 12 ++++++++++++ app/controllers/users/sessions_controller.rb | 2 ++ app/views/layouts/_mobilemenu.html.erb | 2 +- config/initializers/doorkeeper.rb | 4 ++-- config/routes.rb | 10 +++++----- 7 files changed, 25 insertions(+), 25 deletions(-) create mode 100644 app/controllers/users/sessions_controller.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6138fa31..4285682e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -21,20 +21,6 @@ class ApplicationController < ActionController::Base helper_method :authenticated? helper_method :admin? - def after_sign_in_path_for(resource) - sign_in_url = new_user_session_url - sign_up_url = new_user_registration_url - stored = stored_location_for(User) - - if stored - stored - elsif request.referer.include?(sign_in_url) || request.referer.include?(sign_up_url) - super - else - request.referer || root_path - end - end - def handle_unauthorized if authenticated? and params[:controller] == 'maps' and params[:action] == 'show' redirect_to request_access_map_path(params[:id]) @@ -42,7 +28,7 @@ class ApplicationController < ActionController::Base redirect_to root_path, notice: "You don't have permission to see that page." else store_location_for(resource, request.fullpath) - redirect_to new_user_session_path, notice: 'Try signing in to do that.' + redirect_to sign_in_path, notice: 'Try signing in to do that.' end end @@ -60,7 +46,7 @@ class ApplicationController < ActionController::Base def require_user return true if authenticated? - redirect_to new_user_session_path, notice: 'You must be logged in.' + redirect_to sign_in_path, notice: 'You must be logged in.' return false end diff --git a/app/controllers/users/passwords_controller.rb b/app/controllers/users/passwords_controller.rb index bffe3ab6..8a66e820 100644 --- a/app/controllers/users/passwords_controller.rb +++ b/app/controllers/users/passwords_controller.rb @@ -7,6 +7,6 @@ class Users::PasswordsController < Devise::PasswordsController end def after_sending_reset_password_instructions_path_for(_resource_name) - new_user_session_path if is_navigational_format? + sign_in_path if is_navigational_format? end end diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index e472152e..7c211f26 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -10,6 +10,18 @@ class Users::RegistrationsController < Devise::RegistrationsController signed_in_root_path(resource) end + def after_sign_in_path_for(resource) + stored = stored_location_for(User) + return stored if stored + + if request.referer&.match(sign_in_url) || request.referer&.match(sign_up_url) + super + else + request.referer || root_path + end + end + + private def store_location diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb new file mode 100644 index 00000000..d2daa38c --- /dev/null +++ b/app/controllers/users/sessions_controller.rb @@ -0,0 +1,2 @@ +class Users::SessionsController < Devise::SessionsController +end diff --git a/app/views/layouts/_mobilemenu.html.erb b/app/views/layouts/_mobilemenu.html.erb index f04f79a7..e012a808 100644 --- a/app/views/layouts/_mobilemenu.html.erb +++ b/app/views/layouts/_mobilemenu.html.erb @@ -42,7 +42,7 @@ <%= link_to "Request Invite", request_path %>
  • - <%= link_to "Login", new_user_session_path %> + <%= link_to "Login", sign_in_path %>
  • <% end %> <% if current_user %> diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rb index 40de1df8..a276bba0 100644 --- a/config/initializers/doorkeeper.rb +++ b/config/initializers/doorkeeper.rb @@ -5,13 +5,13 @@ Doorkeeper.configure do # This block will be called to check whether the resource owner is authenticated or not. resource_owner_authenticator do - current_user || redirect_to(new_user_session_url) + current_user || redirect_to(sign_in_url) end # If you want to restrict access to the web interface for adding oauth authorized applications, # you need to declare the block below. admin_authenticator do - current_user || redirect_to(new_user_session_url) + current_user || redirect_to(sign_in_url) end # Authorization Code expiration time (default 10 minutes). diff --git a/config/routes.rb b/config/routes.rb index e20f600f..8ba116a1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -65,14 +65,14 @@ Metamaps::Application.routes.draw do devise_for :users, skip: :sessions, controllers: { registrations: 'users/registrations', passwords: 'users/passwords', - sessions: 'devise/sessions' + sessions: 'users/sessions' } devise_scope :user do - get 'login' => 'devise/sessions#new', :as => :new_user_session - post 'login' => 'devise/sessions#create', :as => :user_session - get 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session - get 'join' => 'devise/registrations#new', :as => :new_user_registration_path + get 'login' => 'users/sessions#new', :as => :sign_in + post 'login' => 'users/sessions#create', :as => :user_session + get 'logout' => 'users/sessions#destroy', :as => :destroy_user_session + get 'join' => 'users/registrations#new', :as => :sign_up end resources :users, except: [:index, :destroy] do