# Redirect to external site in a plugin?

**URL:** <https://meta.discourse.org/t/redirect-to-external-site-in-a-plugin/132932>\
**Category:** Development\
**Created:** [8 november 2019 om 15:30 UTC](https://meta.discourse.org/t/redirect-to-external-site-in-a-plugin/132932 "2019-11-08T15:30:46Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [8 november 2019 om 15:30 UTC](https://meta.discourse.org/t/redirect-to-external-site-in-a-plugin/132932/1 "2019-11-08T15:30:47Z")

</div>

I’m trying to call an external URL to log in to a remote site after users log in to Discourse. All I need to do is redirect to a URL on the remote site, which does the login there, and then redirects back to Discourse. When I do, the browser refuses to redirect because or CORS:

```plaintext
Access to XMLHttpRequest at 'https://SITE/api/sso/v2/sso/jwt?' (redirected from 'https://testing.literatehosting.com/session') from origin 'https://testing.literatehosting.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

```

Developers on the app say that it’s because I’m doing the redirect wrong (e.g., with an ajax call?), but this looks the same as, say, what I see in commits mentioned [here](https://meta.discourse.org/t/discourse-doesnt-redirect-to-return-sso-url-after-user-logs-in-on-private-site/97201/8).

I’m (once again) stumped.

I can’t tell if I’m just stupid or whether they need to be sending `Access-Control-Allow-Origin` headers (and if that’s the case, then this can’t be working for any of their clients? which seems far-fetched).

Here’s my code:

```ruby
after_initialize do
  class ::SessionController
    def login(user)
      puts "\n\n\n\LOGIN happening!\n\n\n\n"
      session.delete(ACTIVATE_USER_KEY)
      log_on_user(user)

      if payload = cookies.delete(:sso_payload)
        sso_provider(payload)
      else
        if true # plugin is enabled and has api key and url
          sign_into_thinkific(user)
        else
          render_serialized(user, UserSerializer)
        end
      end
    end

    def sign_into_thinkific(user)
      # do stuff to generate payload

      redirect_to thinkific_sso_url(payload) # also tried redirect_to 
 generate_url(thinkific_sso_url(payload))

    end

    def generate_url(url, params = {})
      puts "\n\n\nGenerate URL: #{url}\n\n\n"
      uri = URI(url)
      uri.query = params.to_query
      uri.to_s
    end

    def thinkific_sso_url(payload)
      current_url="https://#{GlobalSetting.hostname}/"
      url = "https://#{SiteSetting.thinkific_site_url}/api/sso/v2/sso/jwt?jwt=#{payload}"
      url += "&return_to=#{URI.escape(current_url)}" 

      url
    end

  end
end

```

EDIT: The developers shared a Rails app that uses `redirect_to` just as I am and they claim that it works. Is there something about Discourse that makes it do something different with a `redirect_to` because it’s. . . inside Ember, or something?

---

_[View the full topic](https://meta.discourse.org/t/redirect-to-external-site-in-a-plugin/132932)._
