0

我正在尝试使用 GraphCool 实现 GitHub 登录。至今:

脚步

  1. 我跑过:graphcool add-template auth/github
  2. 我取消注释graphcool.ymltyps.graphql文件中的链接
  3. 我设置了一个 GitHub OAuth 应用程序
  4. 我将我的客户端名称和密码添加到本地环境变量中。
  5. 我跑了graphcool deploy

结果

在功能下它说我有loggedInUsergithubAuthentication。我似乎也添加了一个 githubUserIdcreateUser

问题

从这里我被困住了。我不知道该怎么办。我不确定从哪里获得 github 代码。我之前直接通过他们的 API 和 Firebase 使用 Github 实现了 OAuth。我有以下问题:

  1. 在 Github 中,我应该如何设置授权回调 URL?那不需要指向graphcool吗?

  2. 如何启动 OAuth 弹出窗口以便我可以批准我的应用程序?

  3. 如何获取 Github 用户数据,如displayNamephotoUrl. 我可以在createUser运行时将该数据添加到新用户吗?

过去,我已将用户名密码模板修改为带有附加字段的 createUsers。我知道我必须修改模板中的类型和 .ts 文件。似乎我可以在githubAuthentication.ts文件的第 29 行附近获得特定的用户详细信息:

const graphcool = fromEvent(event)
const api = graphcool.api('simple/v1')
const { githubCode } = event.data
const githubToken: string = await getGithubToken(githubCode)
const githubUser = await getGithubUser(githubToken)
const user: User = await getGraphcoolUser(api, githubUser.id)
  .then(r => r.User)
let userId: string | null = null
4

2 回答 2

0

我不确定从哪里获得 github 代码。

简短回答:无论您通常以哪种方式获得 Github 代码。

长答案:

您可以按照本指南查看如何授权 Github OAuth 应用程序:https ://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/about-authorization-options-用于 oauth 应用程序/

或者,按照模板中的说明进行操作README.md

获取测试代码

首先,在您刚刚创建的 Github 上更新 OAuth 应用的配置:

这是index.html

<html>

<body>
  <script>
    // Github client id
    const client_id = '__CLIENT_ID__'
    // Will extract code from current url
    const githubCode = window.location.search.substring(1).split('&')[0].split('code=')[1]
    if (githubCode) {
      // call Graphcool authenticateGithubUser mutation
      console.log(githubCode)
    }
    function getgithubCode() {
      window.location = `https://github.com/login/oauth/authorize?client_id=${client_id}&scope=user`
    }
  </script>
  <button onclick="getgithubCode();">Authenticate with Github</button>
</body>

</html>
于 2017-11-16T09:34:38.137 回答
0

github的文档令人困惑。如果你使用http-server启动服务器,端口必须是localhost:8080,所以你可以更改端口8000->8080

此服务器用于获取 githubCode。在代码中,如果您的应用的客户端 ID 有效,您可以在控制台中找到 githubCode。我可以得到 githubCode,但在那之后,突变有错误。

于 2018-01-18T12:57:59.303 回答