Rails Devise not detecting user as logged in [duplicate] - ruby-on-rails-3

This question already has answers here:
Devise being logged out on post to different route
(2 answers)
Closed 2 years ago.
I am currently having an issue with Devise where before_filter :authenticate_user! always asks the user to sign in even if he has already signed in
I have an index page where users can login. Once logged in the user can click a button that takes him to another view. On that view he can issue commands to a Controller that has a before_filter :authenticate_user! setup. It looks like the filter is always redirecting the user to the sign in page
Could I be using devise in a wrong way ? I haven't customized anything yet, just using it out of the box. Does the user have to select the remember me option even if he is navigating between controllers in the same application ? Could there be a problem with how the session is persisted ?
Rails : '3.0.10'
Devise : '1.4.3'
Edit: This problem happens even with remember me ticked.
Thanks.

Devise uses session cookies. Does your browser allow cookies?

Related

using devise how do i keep a user logged in past closing the browser

I have a rails app using devise and even with the remember me button clicked when i close the browser and reopen it it loses all history of the user ever being logged in.
The session should persist well beyond that. How do i change this?
I am not using timeoutable or anything that auto logs the user out.
Thanks!
Did you look at the 'Rememberable' module in Devise?
http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Rememberable
On the user Model =>
:rememberable

Accessing a url directly without login

I am considering doing this -
Any url (excecpt those I disallow specifically) can be accessed directly without signing-in, however if you click on any of the links on the page, it will redirect you to the sign-up page
I am thinking of several ways of doing it, but neither is flexible enough to work with devise
Create a new link_to_not_registered helper which I will use on every link_to and it will check if the user is logged in or not
create a before_filter to check if the user is logged in. This is a bit problematic, as I don't know how to create a filter only when linking and not when directly accessing a page
Have an external flag to test if the user is logged in and change the page accordingly.
neither way helps me redirect the user after sign-in/sign up (new helper links to sign up, before filter becomes too complex, flags are too simple)
Is there a way to create a functionality of direct access to show actions while clicking on links requires login?
I think the best approach is a before_filter. You can check previous page by request.referrer, so if it's a page inside your app, you redirect user to signin path
def to_signin
redirect_to singin_path if request.referrer["http://myapp.com"]
end

rails devise after first sign in path

Is there a way for us to configure devise to go to a specific page after sign up? Something like after_sign_up_path for. I want the users to update their profile on first login after signup, so want to redirect them to edit_user_path(current_user).
Got it. Below is how i implemented it.
scope = Devise::Mapping.find_scope!(user)
sign_in(scope, user, {})
redirect_to edit_user_path(current_user)
Not sure if you know it already
But there is a after_sign_up_path_for method which you could override now
https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb#L98

Rails: how to redirect to a specific page after signing out with devise

I have a question about using Devise in Rails.
How can I redirect to a specific page after signing out (destroying a user session)?
I tried the following in Application Controller, which does not seem to be working:
def after_sign_out_path_for(resource_or_scope)
root_path
end
Thanks in advance!
That should work according to the wiki.
Maybe you've missed the last line:
You should also override method Devise::Controllers::Helpers#stored_location_for in your application controller, to return nil. This applies to after_sign_in_path_for also. YMMV.

Customizing devise layouts for registrations vs. passwords

I have an application that uses devise for authentication. I am trying to customize the layouts for the devise controller, and I was able to do some of this by following the answers to another question here on Stack Overflow. However, I can't find anything about how to distinguish between the devise/passwords and devise/registrations controller. I am using the following code:
def layout_by_resource
if devise_controller?
if action_name == "edit" or action_name == "update"
"application"
else
"sessions"
end
else
"application"
end
end
The problem is that when a user tries to re-set their password, it's trying to use the application layout (since the action is edit). I need it to use the sessions layout. Can anyone help me figure out how to make that happen?
You should look at this response on different layout for sign_in action in devise, which highlights the Devise docs on How To Create Custom Layouts