4

It seems I must be doing something wrong here (I'm in my infancy with Spree) I followed this tutorial http://guides.spreecommerce.com/developer/authentication.html I have default devise setup and are trying now to add SpreeCommerce to it.

module Spree
  module AuthenticationHelpers
    def self.included(receiver)
      receiver.send :helper_method, :spree_login_path
      receiver.send :helper_method, :spree_signup_path
      receiver.send :helper_method, :spree_logout_path
      receiver.send :helper_method, :spree_current_user
    end

    def spree_current_user
      current_user
    end

    def spree_login_path
      # main_app.login_path
      main_app.new_user_session_path
    end

    def spree_signup_path
      main_app.new_user_registration_path
    end

    def spree_logout_path
      main_app. destroy_user_session_path
    end
  end
end

Spree::BaseController.send :include, Spree::AuthenticationHelpers
ApplicationController.send :include, Spree::AuthenticationHelpers

This is my code in authentication_helpers.rb under lib/spree

I have also added following to my spree.rb initializer

Spree.user_class = "User"

Rails.application.config.to_prepare do
  require_dependency 'spree/authentication_helpers'
end

I mounted spree engine under "/store"

Now. Forms for user obviously work in my application. When I go to store and I hit "Logout" that works as expected - logs me out from spree and my main app. Login form in Spree gives me following error:

> RuntimeError in Spree::UserSessionsController#create Could not find a
> valid mapping for nil

And when I look at session dump:

flash: {"discard"=>[], "flashes"=>{"success"=>"Logged in successfully"}}
session_id: "b71cbba980b1375c241d432920865fb6"
warden.user.spree_user.key: [[1], "fL1Ls3yoi8fg7yPFADbx"]

User obviously doesn't get logged in anywhere.

Registration form takes the input, saves record in Spree::User class but doesn't log user is neither allowed for login later. Login button is still there but when clicked doesn't do anything. I'm not logged in for the rest of the website either.

Additionally, when I try to open the cart I get (although /account gives no error):

ActiveRecord::AssociationTypeMismatch in Spree::OrdersController#edit
Spree::User(#70363670728860) expected, got User(#70363741439840) 
Lukasz Muzyka
  • 2,783
  • 1
  • 30
  • 41

2 Answers2

1

Right. Answer turns out to be quite straightforward:

Instead of adding dedicated authentication strategy to the gem file

gem 'spree_auth_devise', :git => 'https://github.com/spree/spree_auth_devise.git', :branch => '2-3-stable'

Simply use

gem 'devise'

I had them both included at the same time.

Lukasz Muzyka
  • 2,783
  • 1
  • 30
  • 41
0

I'm experiencing the same problem you are I believe: ActiveRecord::AssociationTypeMismatch in Spree::OrdersController#populate Spree::User(#70237393066980) expected, got User(#70237401744200)

Like you I added the Spree class as "User" and am using Devise.

I'd like to ask you:

  1. Do you have two different tables in your db schema: users (from devise) and spree_user? I do and am wondering if this is my problem.

  2. My gemfile doesn't have devise, though I installed it using the command line. However, I did this after running all of the spree scripts, so my migrations are first setting up everything in spree, and then my devise create users migration. Do you think this could be my problem?

Haymaker87
  • 519
  • 7
  • 18
  • 1
    Re:2 . If you installed devise from command line you would still need to require it. So if you don't, then you probably rely on what spree came with – Lukasz Muzyka Nov 20 '14 at 02:25
  • Well, I followed your own answer here to eliminate the "gem 'devise'" line in my gemfile and that stopped my Active Record Association Type Mismatch error... are you saying I should re-add that in the gem file? – Haymaker87 Nov 21 '14 at 04:18
  • ou need to make sure you have only one devise gem version declared in your Gemfile. Spree documentation shows special distribution of devise (which I added without much thinking when I had previously used standard devise distribution). Basically you need to make choice which one you want to use. Now, my colleagues are now working on a Spree project (i have couple other project to look after) and they have some trouble with spree user. So, if you're working on spree -> go with the standard spree setup. My problem arouse because I was integrating existing app with spree. – Lukasz Muzyka Nov 22 '14 at 01:23
  • I suppose ideally, I would like to keep Devise authentication (because it's what I'm most familiar with and seems flexible enough). In my gem file currently I have declared devise with no specific version, and I deleted the gem spree_auth_devise (the default spree auth stuff) and that seemed to work; which would make sense since there now isn't any duplication of authentication methods, right? What I do find is still lacking is the ability to create an admin user without the spree_auth_devise_gem. Have you had that problem? – Haymaker87 Nov 25 '14 at 17:20