warden

Redirect to log in page if user is not authenticated with Devise

丶灬走出姿态 提交于 2019-11-27 20:29:19
I'm using Devise with Ruby on Rails. What is the recommended way to redirect unauthenticated users to the sessions#new page if they attempt to access a page that requires authentication? Right now I get an error that says no route matches the one they attempt to access (leading to a 404 error in production). Just simple add this method to application_controller.rb protected def authenticate_user! if user_signed_in? super else redirect_to login_path, :notice => 'if you want to add a notice' ## if you want render 404 page ## render :file => File.join(Rails.root, 'public/404'), :formats => [:html

Why is my RSpec not loading Devise::Test::ControllerHelpers?

南楼画角 提交于 2019-11-27 11:53:12
问题 I'm using Rails 5, and Devise 3.5.1. Going through a nice (older) book about creating/testing an API, which uses Devise authentication. It was written before Rails 5, so I chose not to use the new api-only version. Here's my test... #/spec/controllers/api/v1/users_controller_spec.rb require 'rails_helper' describe Api::V1::UsersController, :type => :controller do before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" } describe "GET #show" do before(:each) do @user =

Configuring Warden for use in RSpec controller specs

匆匆过客 提交于 2019-11-27 08:19:44
I was able to use Devise's sign_in method to log in a user in my controller specs. But now that I'm removing Devise from my application, I'm not quite sure how to get similar functionality working with just Warden on its own. How should I go about setting up spec/spec_helper.rb and related spec/support/*.rb files to get Warden running within controller specs sufficiently? I've tried setting up a file at spec/support/warden.rb with these contents: RSpec.configure do |config| config.include Warden::Test::Helpers config.after do Warden.test_reset! end end Then I have before calls similar to this

Redirect to log in page if user is not authenticated with Devise

偶尔善良 提交于 2019-11-27 04:25:07
问题 I'm using Devise with Ruby on Rails. What is the recommended way to redirect unauthenticated users to the sessions#new page if they attempt to access a page that requires authentication? Right now I get an error that says no route matches the one they attempt to access (leading to a 404 error in production). 回答1: Just simple add this method to application_controller.rb protected def authenticate_user! if user_signed_in? super else redirect_to login_path, :notice => 'if you want to add a