AlchemyCMS / alchemy_cms

Alchemy is the Open Source Rails CMS framework for the component based web that can be used as classic server side rendered or headless CMS.
https://www.alchemy-cms.com
BSD 3-Clause "New" or "Revised" License
846 stars 315 forks source link

Authentication problem when trying to integrate alchemy_cms 2.7.2 into an existing app with devise #460

Closed ChristianPeters closed 10 years ago

ChristianPeters commented 10 years ago

Hey guys,

I need your help. I have spent quite some time on trying to integrate the authentication mechanisms of alchemy and my existing Rails app.

Then I tested whether the app still works and deleted the session cookie inbetween.

Problem 1 (fixed): Paths of the main app not available

First, I had to prepend all my path helper calls with main_app. for all devise controller actions because they were unavailable.

Problem 2a: Signing in with a main app user fails

When I try to sign in in the main app, authentication fails (silently) although I am sure about the password:

Started POST "/users/sign_in" for 127.0.0.1 at 2014-02-06 17:48:42 +0100
Processing by SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Md0+cXB1JAK/luvEz9Vqv4+j0ViquWhBfxg7b5GBUtk=", "user"=>{"email"=>"myaccount@example.com", "password"=>"[FILTERED]"}, "button"=>""}
  Alchemy::Site Load (0.4ms)  SELECT "alchemy_sites".* FROM "alchemy_sites" WHERE "alchemy_sites"."host" = 'localhost' LIMIT 1
  Alchemy::Site Load (0.3ms)  SELECT "alchemy_sites".* FROM "alchemy_sites"
  Alchemy::Site Load (0.3ms)  SELECT "alchemy_sites".* FROM "alchemy_sites" LIMIT 1
  Alchemy::Language Load (0.4ms)  SELECT "alchemy_languages".* FROM "alchemy_languages" WHERE "alchemy_languages"."site_id" = 1 AND "alchemy_languages"."id" = 1 LIMIT 1
Completed 401 Unauthorized in 7.6ms

I guess devise tries to look in the alchemy_users table instead of the users table. Well, the SQL output tells me that it doesn't look there either. Is there a configuration problem?

Problem 2b: Signing in with the Alchemy user fails

Trying to sign in with the first alchemy admin user reveals the same problem – it says "Ungültige Anmeldedaten":

Started POST "/cms/admin/login" for 127.0.0.1 at 2014-02-06 18:06:48 +0100
Processing by Alchemy::UserSessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Md0+cXB1JAK/luvEz9Vqv4+j0ViquWhBfxg7b5GBUtk=", "user"=>{"login"=>"my_alchemy_admin@example.com", "password"=>"[FILTERED]"}, "user_screensize"=>"1680x1050"}
  Alchemy::Site Load (0.4ms)  SELECT "alchemy_sites".* FROM "alchemy_sites" WHERE "alchemy_sites"."host" = 'localhost' LIMIT 1
  Alchemy::Site Load (0.2ms)  SELECT "alchemy_sites".* FROM "alchemy_sites"
  Alchemy::Site Load (0.2ms)  SELECT "alchemy_sites".* FROM "alchemy_sites" LIMIT 1
  Alchemy::Language Load (0.4ms)  SELECT "alchemy_languages".* FROM "alchemy_languages" WHERE "alchemy_languages"."site_id" = 1 AND "alchemy_languages"."id" = 1 LIMIT 1
Completed 401 Unauthorized in 8.2ms

Then I tried to reset my password of the alchemy user...

Problem 3: Devise uses only the Alchemy::User

... and got this error rendering the reset_password_instructions.text.erb:

undefined method `name_with_salutation' for #<Alchemy::User:0x007fbd5dd97b10>

name_with_salutation is an instance method of the User class from the main app.

In search of a clean solution, I found out that you followed the community's wish to decouple Alchemy from devise and extracted the Alchemy user model into its own gem in version 3.0.

As updating my big main app to Rails 4 is no option at this time, I attempted to backport the respective commit to the 2.7-stable branch. The 40-50 merge conflicts were rather straightforward (current_user becomes current_alchemy_user and so on) but I had to give up on getting the test suite green.

So, what should I do?

Although it's not nicely integrated, I can live with two different authentication scopes. But the separation does not seem to work either.

tvdeyen commented 10 years ago

Hi Christian.

Alchemy v2.7.2 does not support using another devise based authentication solution.

Please use Alchemy 3.0 https://github.com/magiclabs/alchemy_cms/tree/master

In Alchemy 3 it is very easy to hook up your own user class and authentication mechanism. See https://github.com/magiclabs/alchemy_cms#authentication-user-model

Could you please use the Google User Group for questions like this? https://groups.google.com/forum/#!forum/alchemy-cms

This is an issue tracker for bug reports.

Thank you and let us know if you need more help

tvdeyen commented 10 years ago

Since you are using a Rails 3.2 app and maybe can not upgrade, please see the https://github.com/magiclabs/alchemy-devise gem.

Maybe this helps with integrating in your app.

The big problem is, that Devise does not support multiple rails engines.