# 无法在 localhost 上测试 Discourse Connect

**URL:** https://meta.discourse.org/t/unable-to-test-discourse-connect-on-localhost/202986
**Category:** SSO
**Created:** [2021年九月8日 16:04 UTC](https://meta.discourse.org/t/unable-to-test-discourse-connect-on-localhost/202986 "2021-09-08T16:04:50Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Sky020](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sky020/32/189842_2.png) [@Sky020](https://meta.discourse.org/u/Sky020)
#### Post date: [2021年九月8日 16:04 UTC](https://meta.discourse.org/t/unable-to-test-discourse-connect-on-localhost/202986/1 "2021-09-08T16:04:50Z")

</div>

大家好，

我们计划将 Discourse 徽章与我们的平台集成。也就是说，允许用户将其 Discourse 账户与我们平台上的账户进行关联。

为了链接账户，我们原本打算使用 [Discourse Connect](https://meta.discourse.org/t/13045?silent=true)。然而，在重定向过程中我们遇到了 CORS 错误：

```console
Access to fetch at 'https://forum.freecodecamp.org/session/sso_provider?sso=<hash>'
(redirected from 'http://localhost:3000/discourse/connect') from origin
'http://localhost:8000' 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.
If an opaque response serves your needs, set the request's mode to 'no-cors'
to fetch the resource with CORS disabled.

```

我尝试了多种 [Discourse Connect](https://meta.discourse.org/t/13045?silent=true) 的设置，包括将服务器、客户端和 Discourse 地址添加到 `discourse connect provider secrets` 中，但均无济于事。

**澄清一下** ：我们并不打算使用 [Discourse Connect](https://meta.discourse.org/t/13045?silent=true) 来登录任何内容。

**相关位置**

- `freecodecamp.org` - 平台域名
- `forum.freecodecamp.org` - Discourse 实例子域名
- `localhost:3000` 和 `localhost:8000` - `freecodecamp.org` 开发环境的服务器和客户端

**相关代码：**

> <https://github.com/freeCodeCamp/freeCodeCamp/pull/43168/files#diff-c99b1f4c50468e6835c7456cff5841cdd1cbe76ca7a2ac1d15893f839ff37799R278>
>
> Checklist:
> 
> 
> 
> \- \[x\] I have read \[freeCodeCamp's contribution guidelines\](htt…ps://contribute.freecodecamp.org).
> \- \[x\] My pull request has a descriptive title (not a vague title like \`Update index.md\`)
> \- \[x\] My pull request targets the \`main\` branch of freeCodeCamp.
> \- \[x\] I have tested these changes either locally on my machine, or GitPod.
> 
> 
> 
> Closes #42837 
> 
> \*\*TODO\*\*
> \- \[\] Look at potentially creating an epic for data handling
> \- \[x\] Integrate method to link Discourse and \`/learn\` accounts
> \- \[\] Clean up styling to be responsive
> \- \[x\] Change badge background to be same as container
> \- \[x\] Get new badge description and long description
> \- \[\] Link display of badges to existing privacy setting (My Portfolio?)
> \- \[\] Finalise how to display/order badges
> \- - \[\] Collapsable?
> \- - \[\] Show favourites above other Profile content?
> \- - \[\] Add more \`badge\_groups\` to meta on Discourse?
> 
> As of \_08/09/2021\_
> \- My forum account is the only one with a new Badge awarded - for testing
> \- Discourse Connect does not have correct Headers to allow \`localhost:8000\` to be redirected
> 
> CC:/ @RafaelDavisH

```javascript
return res.redirect(
    `${FORUM_LOCATION}/session/sso_provider?sso=${URL_ENCODED_PAYLOAD}&sig=${HEX_SIGNATURE}`
  );

```

任何帮助都将不胜感激。

---

<div class="post-metadata">

### Author: ![blattersturm](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blattersturm/32/76134_2.png) [@blattersturm](https://meta.discourse.org/u/blattersturm)
#### Post date: [2021年九月8日 16:12 UTC](https://meta.discourse.org/t/unable-to-test-discourse-connect-on-localhost/202986/2 "2021-09-08T16:12:17Z")

</div>

我认为这种重定向流程是用于内容框架的，而不是 `fetch` 调用。例如，任何需要重定向到登录页面且无法重定向回来的情况都将无法工作，此外，跨域 Cookie 在这种情况下也会成为问题。

不如试试传统的弹窗窗口？😛
