Devise before_action :authenticate_admin! not authenticating admin user - ruby-on-rails

I have used devise numerous times but currently facing an issue at the moment.
I used this devise wiki to set up devise with multiple user models which i have done multiple times. https://github.com/heartcombo/devise/wiki/How-to-Setup-Multiple-Devise-User-Models
In my admins_controller.rb i have the following code
class AdminsController < ApplicationController
before_action :authenticate_admin!
end
My routes.rb
Rails.application.routes.draw do
devise_for :admins, path: 'admins', controllers: {sessions: "admins/sessions", registrations: "admins/registrations"}
namespace :admins do
root "dashboards#index"
end
end
Everything else works well but after i try to sign in as an admin, it should redirect to my admins root but i always get this error
You need to sign in or sign up before continuing.
But when i do admin_signed_in? or current_admin. I get true and my admin record accordingly. Which means the admin is already signed in.
When i comment the before_action code, then it works perfectly.
Currently stuck and cannot think of why and how to solve this issue.

Related

Devise Invitable controller seems not to be reached?

I installed devise-invitable and using the standard devise-invitable controller (which I didn't generate) it functions properly. However, when I tried generating a custom controller for devise-invitable, I encountered some issues. Please find below the steps I took.
Steps I took to generate the controller
Following the documentation of devise I tried generating the invitations controller via the console which failed:
rails generate devise:controllers users -c=invitations
Running via Spring preloader in process 64830
Could not find "invitations_controller.rb" in any of your source paths. Your current source paths are:
/Users/name/code/name/app/lib/templates/devise/controllers
/Users/name/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/devise-4.7.0/lib/generators/templates/controllers
When this didn't work, I manually tried implementing:
a controller in the folder where all other devise controllers were generated
Adding the invitations controller to routes.rb
=> This didn't seem to work either, because I tried breaking the controller to see if it was reached, but it doesn't break.
controllers/users/invitations_controller.rb
class Users::InvitationsController < Devise::InvitationsController
def new
#hotel = Hotel.find(params[:park_id])
#user = User.new
#user.hotel = #hotel
text to cause an error message
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: {
sessions: 'users/invitations'
}
The problem is in your routes since an invitation is not an session.
If you changes sessions to invitations in your routes then it will hit the users/invitations controller.
# routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: {
invitations: 'users/invitations'
}
end

Rails - How to override Devise 'current password' for omniauth users

I've seen this posted before but haven't found a solution that works for me.
I have tried following the Devise Wiki and have tried the code suggested by others to add to the controller:
Editing Users With Devise and Omniauth
[Rails]update_without_password does't update user info
https://github.com/plataformatec/devise/issues/1620
Problem is that a user signs up via our Twitter Omniauth is unable to update their account info because Devise prompts them for their 'current password' to make changes. Emailing the user their random generated password isn't an option.
What i want to do -
I want to override the Devise controller's 'update' method so that it recognises a Twitter user editing their details for the first time and allows them to skip the 'current password' validation. We assign twitter users a standard email address that can be the variable it checks for. They won't be able to submit the from without changing it.
Still the problem -
I've tried different code blocks in the Registration Controller, but I still am getting redirected back to the from with an error that the current password is incorrect. I'm not even entering into a binding.pry , so it looks like the method is not even being read?
I have tried removing the form input for 'current password' completely, and passing it in hidden with a default value.
thank you for your help!
Routes
Rails.application.routes.draw do
ActiveAdmin.routes(self)
# devise_for :users,
devise_for :users, controllers: { registrations: 'registrations' },
controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
scope '(:locale)', locale: /en|ca|es/ do
resources :politicians
resources :posts do
resources :comments, only:[:new, :create, :edit, :update, :destroy]
member do
put "like", to: "posts#upvote"
put "dislike", to: "posts#downvote"
end
end
get "/about" => "pages#about_us"
root to: 'pages#home'
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Registrations_Controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
protected
def update_resource(resource, params)
binding.pry
if current_user.email.include?("email.com")
params.delete("current_password")
resource.update_without_password(params)
else
resource.update_with_password(params)
end
end
end
You'll want to create a method similar to this one in your registration's controller:
def update_resource(resource, params)
resource.update_without_password(params)
end
Devise offers a very nice, in-depth wiki article for allowing users to update their account information without entering their password here.

Devise redirect on sign up

I know this question was asked several times. I tried them but none of them solved my problem. I used devise for users. I want to redirect the user to a different page on sign up rather than signing in directly. I created registration controller, and tried overriding "after_inactive_sign_up_path_for" but it didn't work out. I 'm using devise confirmable also. So until the user is verified I should redirect him to other page.
registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(resource)
"http://www.google.com" # Or :prefix_to_your_route
end
end
routes.rb
devise_for :users, controllers: { registrations: "registrations" }
Next I tried to move the registrations into a different folder in the controllers folder. But that too didn't work out.
FYI: I'm using rails 5 and devise 4.2.1.
Your controller should be in the path app/controllers/users/registrations_controllers.rb.
Then your routes should be
devise_for :users, controllers: { registrations: "users/registrations" }

How does one use a rails admin account outside of rails admin to edit user data?

I would like to create a new controller that will allow the admin user to edit models attached to users outside of rails admin. Here is a sample from my routes.rb:
devise_for :admins
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
devise_for :dealers
devise_for :users
get "..." => "..."
etc
What do I need to do in both my controller and in routes.rb to make this possible? I'm fairly new to Rails. Thanks! I have a limited amount of time so making a rails-admin plugin is not feasible.
In your controller check if an admin is loged in using admin_signed_in?, and you can also access it by current_admin. So you just have to place conditions checking if it's and admin to enable or not those functionalities.
EDIT:
You can have something like this in your before_filter
before_filter :check_authentication
private
def check_authentication
authenticate_user! unless admin_signed_in?
end

Setup devise with custom registration controller

I'm working on a rails site using devise, where we do not want user sign ups just yet. User authentication is so we can login to access restricted parts of the site and add/edit things as we see fit. So for now, I created the following controller:
class Users::RegistrationController < Devise::SessionsController
def new
end
end
And setup my routes in this fashion:
devise_for :users, :controllers => { :registration => "users/registration" }
However, when I run rake routes, I still see a returned value for the create action on the registration controller. Any ideas on how to get rid of it?
Try using :registrations instead of :registration. Also, it seems like your custom controller class should be defined via:
class Users::RegistrationsController < Devise::RegistrationsController

Resources