Create a DiscourseConnect login link

Sites using DiscourseConnect sometimes want to include links on their DiscourseConnect provider site that will log users into Discourse and redirect them to a specific Discourse URL. This can be done by creating an anchor on the SSO provider site with an href property that points to /session/sso and has a return_path parameter set to the path of the Discourse page you want users to end up on.

The link’s href property should be in this form:

https://forum.example.com/session/sso?return_path=/relative_path

Here’s a full anchor that will log in a user and redirect them to your Discourse site’s homepage:

<a href="https://forum.example.com/session/sso?return_path=/">Community</a>

Here’s a link that will log in a user and redirect them to your Top Topics page:

<a href="https://forum.example.com/session/sso?return_path=/top">Top Topics</a>

Discourse allows you to login a user and redirect them to a non-Discourse URL if you configure the discourse connect allowed redirect domains site setting. By default this setting is blank - preventing redirects to non-Discourse URLs. If you enable it, be sure to use the absolute URL in the return_path parameter for any non-Discourse URLs that you want to direct users to.

Last edited by @JammyDodger 2024-05-26T07:21:09Z

Check documentPerform check on document:
20 Likes

I’m using this method but each time I need to login via sso.

How can we make it seamless, meaning if I have already logged in to discourse, there would be no need to go to sso page to login again?

1 Like

Unless something has changed, that is how the link is expected to work. For example, if you are logged into Discourse and click a link on the SSO provider site that points to https://forum.example.com/session/sso?return_path=/t/some-slug/23, you should be seamlessly redirected to /t/some-slug/23 without having to visit the login page first.

1 Like

I’m already on the latest update of discourse and this is how the sso works for me:

As you can see I’m already logged in, but when I enter a url like https://forum.example.com/session/sso?return_path=/t/some-slug/23, I would be redirected to sso login page again.

I think what is happening is that when you visit a route like https://forum.example.com/session/sso?return_path=/t/some-slug/23, Discourse redirects you to the discourse connect url, regardless of whether you are logged into Discourse or not. That happens here:

The SSO provider site is then expected to handle the case of users who are already logged into the site. Here’s how the WP Discourse plugin handles it:

That code (what follows the else statement) handles the case of users who are already logged into WordPress. They are redirected back to the URL that’s supplied by the return_path query param. So from the user’s point of view, they are taken directly to the return path URL, but what actually happens is that they are redirected to the SSO provider site, then back to Discourse.

I think the problem on your site is that your SSO code isn’t handling the case of users who are already logged into the site.

I don’t have things setup to test this right now. It’s possible that I’m reading the code incorrectly. Before looking at the code, I thought that a check was run on the Discourse end to see if the user was already logged into Discourse, but that does not seem to be the way it works.

2 Likes

thank you very much for your explanation.

Yes that is a thing we are going to fix in our sso.

However

I think having this checked on discourse side would have created a better user experience.

1 Like