I just started working on a Rails 4 application (4.2.3), where I use Devise to authenticate users. I want users to be able to play with the application before signing upp by creating a test project and logging in as a guest user. When a user signs up (or logs in), I want to assign a test project to the new current user.
I follow this guide from Platformatec: https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user
Creating a guest user works, but when I register or in an active guest user session, I get the following error:
Filter chain halted as :require_no_authentication rendered or redirected
If I clear the session, it will work. The method that controls my guest user is as follows:
def current_or_guest_user if current_user if session[:guest_user_id] && session[:guest_user_id] != current_user.id logging_in guest_user(with_retry = false).try(:destroy) session[:guest_user_id] = nil end current_user else guest_user end end
As mentioned, creating guest users seems to work very well. But this logic never happens:
I am sure that my guest session conflicts with my new user and causes this Devise error (since the guest user is never deleted during registration):
Filter chain halted as :require_no_authentication rendered or redirected
The rest of my guest logic looks something like this related guide: https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user . I also added the code from the paragraph / authentication example: https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user#authentication-this-may-interfere-with-the-current_user -helper .
Any ideas on what I am missing, and how can I get my current_or_guest_user delete the guest user when registering and signing when using Devise?
Update
Here's what my routes currently look like:
devise_for :users, controllers: { sessions: "users/sessions", registrations: "users/registrations" } root :to => 'public#index' resources :apps get 'users/show', to: "users#show" get 'users', to: "users#index" post 'guests/receive_guest', to: "guests#receive_guest"
Update 2
The manual has the following statement:
When (and if) a user logs in or logs in, we delete the guest user and clear the session variable.
This does not explain how and where to do it. I guess I should call current_or_guest_user again. But I'm not sure where, since I am not familiar with Devise.
Update 3
To make it more understandable. These are the steps that I want to achieve.
- The user creates a test project.
- After creating a test project, a guest user and session is created. Guest must be deleted after session or # 3.
- If the guest user subscribes, he must log in, and the test project must be assigned to a new real user.
- Guest must be deleted.