Devise + LocomotiveCMS - Remember the page I was trying to visit before being asked to log in
-
29-10-2019 - |
Question
I'm trying to add a simple feature to locomotivecms (github). A very simple feature: currently users are redirected to the "main admin hub" (/admin/) after logging in - even if they were trying to edit a different page. I want them to be directed to that page after logging in instead.
It seems a very simple and reasonable thing to add, but after two days trying I've decided to ask for help.
This is what I've found out so far.
- This app doesn't use ActiveRecord, but Mongoid (the backend db is MongoDB)
- Locomotive doesn't use a simple "User" schema. Authentication is divided into two parts: "accounts" (email, password, name, etc) and "site" (it's a multi-site cms). A "site" has many "memberhips". A membership has one site_id and account_id (and also, a role, but that's not important for this, I think).
- Most of the "action" in locomotive is behind the
/admin/
route. For example, the login path is/admin/log_in
. Most controllers are inside an/admin/
subfolder, too. - I've found this bit in the
/admin/sessions_controller
that apparently "fixes" the url to be visited after being logged in to the/admin/
root.
Here's the relevant bit:
def after_sign_in_path_for(resource)
admin_pages_url
end
I'm nearly sure that what I need is this instead:
def after_sign_in_path_for(resource)
stored_location_for(resource) || admin_pages_url
end
If I have understood Devise's documentation correctly, stored_location_for
searches for a value in the session (session['admin_return_to']
in Locomotive's case) to "see if someone wants to go back somewhere". If it's empty, the || ensures a safe path to the admin root.
Unfortunately this doesn't work. It seems that the session variable that I need is never set up. I was assuming that Devise did this kindof automatically.
Must I set the session value myself? If so, where? Or are my assumptions wrong?
Thanks a lot!
Solution
Best solution is updating to the latest version of LocomotiveCMS.