Problem redirecting to forum after login


(Leah Kramer) #1

Hello. So in searching some other threads here, it seems like the expected behavior is this:

  1. Someone is reading a thread in Discourse and decided to login so they click the Login button
  2. They pop over to WordPress where they login
  3. They are redirected back to the Discourse thread that they were reading

That sounds great! In our case, this isn’t working I think because we use the MemberMouse plugin which forces people to a certain “welcome” URL after logging in. BUT, I can bypass that with a filter MemberMouse provides.

The only problem, is that the URL of the Discourse topic the person was reading doesn’t seem to be passed to WordPress.

E.g. Let’s say I’m in this topic in Discourse:
https://forums.gemsociety.org/t/any-brazil-company-which-sold-rough-faceting-amethyst-citerine-and-amertine/1246

And I click the Login button in Discourse

I get sent to this URL:
https://www.gemsociety.org/login/?redirect_to=%2Flogin%3Fsso%3Dbm9uY2U9Y2IwZGE0NmRlMGM0YTlhOGM1OTc4OGU0NGRjOGU3MDkmcmV0dXJu%250BX3Nzb191cmw9aHR0cCUzQSUyRiUyRmZvcnVtcy5nZW1zb2NpZXR5Lm9yZyUy%250BRnNlc3Npb24lMkZzc29fbG9naW4%253D%250B%26sig%3D419ba4dfda93d068c81470cf32e4aec633217047728a9bca514bbae3891cffde

Nowhere in that big string do I find the URL the person was reading in Discourse (/any-brazil-company…/).

So, I’m a little confused here. Any thoughts? Thanks!


(Simon Cossar) #2

The sso return path is saved on Discourse as the value of the nonce key. If you can find a way to get your WordPress site to redirect back to Discourse (/session/sso_login?) with the sso params users should get redirected to the page they clicked the login link on.


(Leah Kramer) #3

Thanks Simon for the reply. So, should I redirect the user to this?

https://forums.gemsociety.org/session/sso_login?sso=bm9uY2U9ZTU3NWU3YzI2ODM2MjEzYjNmZTNmZWE5ZTc4NjNiNTQmcmV0dXJu%250BX3Nzb191cmw9aHR0cCUzQSUyRiUyRmZvcnVtcy5nZW1zb2NpZXR5Lm9yZyUy%250BRnNlc3Npb24lMkZzc29fbG9naW4%253D%250B%26sig%3D9c9ee72cd56218de3fb5ba46b985084369874fc5f434afdb1d9b5d14d170c0a

When I do this, I get a blank screen in Discourse.


(Simon Cossar) #4

Right, that’s not going to work. If you look here: wp-discourse/discourse-sso.php at master · discourse/wp-discourse · GitHub you can see what needs to happen. If the sso and sig parameters are in the query_vars and a user is not logged in, they are redirected to the WordPress login URL with the SSO request from Discourse set as the redirect parameter. It seems like the MemberMouse plugin is overriding that redirect.

Somehow, you need to preserve the redirect (the parameter from this line: $login = wp_login_url( esc_url_raw( $redirect ) ); and redirect to it after MemberMouse has done its work. The request should then be processed by the part of the sso_parse_request function that begins here:


(Leah Kramer) #5

I got it working! Thanks very much @Simon_Cossar.

Now when someone comes into the forum; clicks the blue Login button; logs into wordpress by typing in their username/pw; they are redirected back to thread they were at in Discourse.

I think this is going to be a huge win for people who come in via the Discourse emails e.g. and want to participate in the thread. Previously, they’d lose track of where they were since they didn’t get redirected back to the thread.


(Leah Kramer) #6

P.S.

For anyone else looking into this – This “redirecting back to where you were” behavior works (thx to the WP-Discourse plugin) in the scenario #1 where you are already logged into WordPress.

But it’s scenario #2 where you need to login to WordPress that you are not automatically redirected back to the forum thread where you were. This is what I added in myself thanks to @Simon_Cossar 's help.

I thought it was due to my membership plugin but I actually just think the scenario #2 redirect functionality doesn’t exist in the WP-Discourse plugin. I could be wrong about this, feel free to correct me!


(Simon Cossar) #7

Yes, WordPress plugin has that functionality, but other plugins that do login redirects can break it.