Joomdle 1.0 Installation

"Remember Me" functionality

More
10 years 4 weeks ago #1 by ncy
"Remember Me" functionality was created by ncy
I've tried to searched for anything regarding that "Remember Me" checkbox and what the expected behavior should be but couldn't find anything. Most forum posts seem to be about Moodle not autologging in when Joomla is logged in, but that's not my problem. My problem is about persistent logins (through Joomla side) using "Remember Me" checkbox. After restarting the browser, going directly to a Moodle page seems to ignore the fact that the "Remember Me" option was selected. Joomla remembers, but not Moodle. I have to browse to a Joomla page first and then go back to the Moodle page to be logged into Moodle.

Steps taken:
  1. Login to Joomla with "Remember Me" checkbox checked.
  2. Go to a Moodle page. It logged in OK as well. So far so good.
  3. Now close the browser.
  4. Open the browser again. Return to the Joomla website. It's OK, still logged in.
  5. Go to a Moodle page. It's OK, still logged in. So far, still good.
  6. Close the browser again.
  7. Here's the problem: Open the browser again. Now instead of going to the Joomla site, go directly to a Moodle page. User is NOT logged in, even though I expect the user to be logged in because of the "Remember Me" checkbox being checked.
  8. If I browse to a Joomla page, and then return back to the Moodle page, now Moodle is logged in.

Is there a way for Moodle to automatically check whether Joomla is already logged in due to having the "Remember Me" function checked off during the last successful login? It seems a common thing to do to go directly to the Moodle courses pages, since the browser history remembers URLs and will autocomplete. It's annoying to have to go back and forth between Joomla and Moodle everytime to get into a "logged in" status.

Thank you for any assistance, and I apologize if I missed something obvious. System Check shows all green.

Please Log in or Create an account to join the conversation.

More
10 years 4 weeks ago #2 by Antonio Durán
Replied by Antonio Durán on topic "Remember Me" functionality
As far as I know there is no "remember me" funcionality in Moodle, so this cannot work.

From what I have read now, it seems a new auth method would be required for Moodle in order to get this working.

Please Log in or Create an account to join the conversation.

More
10 years 4 weeks ago #3 by ncy
Replied by ncy on topic "Remember Me" functionality
Thank you for the reply, Antonio. Could you point me in the right direction? How would I read the login information from Joomla (I can't seem to find any specific cookie that's created regarding this) and what function would I need to call to properly login to Moodle?

Please Log in or Create an account to join the conversation.

More
10 years 4 weeks ago #4 by Antonio Durán
Replied by Antonio Durán on topic "Remember Me" functionality
I don't know... for this to even work you would need to have Joomla and Moodle on the same domain, as you cannot read other domains cookies.

There is a cookie created by Joomla when you set "Remember me" although I don't remember the details.
One function for log in to Moodle is complete_user_login(). We use it in Joomdle, in moodle/auth/joomdle/land.php, if you want to take a look.

You will need a good understanding of Joomla and Moodle internals to make this work, though.

Please Log in or Create an account to join the conversation.

More
10 years 2 weeks ago #5 by ncy
Replied by ncy on topic "Remember Me" functionality
I seem to have it working now. I took an easier (although probably less-efficient?) approach . Any feedback or recommendations are appreciated :).

I create and delete my own custom "rememberme" cookie, since the Joomla one seems hard to decipher with the hash. A visit to Moodle (as a guest, or when not logged in) will bounce the user back to Joomla before returning to Moodle, if the cookie exists. If Remember Me was set, that means Joomla should login the user in that brief redirect, and when returning to Moodle, Moodle will now also be logged in.

In my Moodle theme's layout/default.php, I inserted:
Code:
if (!isloggedin() || (isloggedin() && isguestuser())) { if (isset($_COOKIE['rememberme'])) { $url = "/auto-login-from-moodle"; redirect($url); } }

So all attempts to access any page in Moodle (when not logged in and if the cookie exists) will redirect to a custom Joomla page (with Menu Item alias "auto-login-from-moodle") that I created with the code:
Code:
<?php $user = JFactory::getUser(); $isguest = $user->guest; if ($isguest) { setcookie("rememberme", 1, 1, "/"); // Kill cookie, if present, b/c it shouldn't be there as a guest } // Redirect JFactory::getApplication()->redirect('/moodle); // I have Moodle directory as "moodle" in the webroot ?>

Which just redirects back to Moodle, but also meaning:
  • If Remember Me was set, then on arrival to this Joomla page, the user will be logged in to Joomla, and when this code redirects back to Moodle, Moodle will now also be logged in.
  • If the user is not logged in (i.e. Remember Me wasn't set), make sure the custom "rememberme" cookie doesn't exist (or if it somehow does, it would get stuck in an infinite redirect loop otherwise) and just go back to Moodle page.

Finally, to create and delete the custom cookie "rememberme", in joomdlehooks.php I did:
Code:
function onUserAfterLogin($options = array()) { ... if ($options['remember'] == 1) { // Create custom Remember Me cookie with 1 year expiration setcookie("rememberme", 1, time() + (86400 * 365), "/"); } }

and
Code:
function onUserLogout($user, $options = array()) { ... // Kill the custom Remember Me cookie setcookie("rememberme", 1, 1, "/"); }

The Joomla redirect page isn't even seen because the redirect happens so quickly, so it works pretty nicely in that respect.

I realize it's a bit of a messy solution involving code modifications to Joomdle, but it's the best I could come up with right now :pinch: .

Please Log in or Create an account to join the conversation.

More
10 years 1 week ago #6 by Antonio Durán
Replied by Antonio Durán on topic "Remember Me" functionality
Thanks for sharing your solution. As you say, it involves mods to Moodle theme, so it is not something we can incorporate to Joomdle, but I am sure it can help other users.

Please Log in or Create an account to join the conversation.