next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

GithubProvider does not have an `email_verified` field

Open tautastic opened this issue 1 year ago • 0 comments

Provider type

GitHub

Environment

System: OS: macOS 14.3.1 CPU: (14) arm64 Apple M3 Max Memory: 75.42 GB / 96.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.11.0 - /usr/local/bin/node Yarn: 1.22.21 - /usr/local/bin/yarn npm: 10.2.4 - /usr/local/bin/npm pnpm: 8.15.1 - /usr/local/bin/pnpm Watchman: 2024.01.22.00 - /opt/homebrew/bin/watchman Browsers: Chrome: 121.0.6167.160 Safari: 17.3.1 npmPackages: @auth/prisma-adapter: ^1.3.3 => 1.3.3 next: ^14.1.0 => 14.1.0 next-auth: ^4.24.5 => 4.24.5 react: 18.2.0 => 18.2.0

Reproduction URL

https://github.com/tautastic/ltx

Describe the issue

The GithubProvider does not have an email_verified field, although this information can be extracted from the GithubEmail type.

How to reproduce

Just use the GithubProvider and try to access any information about wether the users email is verified or not.

Expected behavior

I would expect the Provider to use the information that it got from Github and use it.

I think this can be done by adding an email_verified field to the GithubProfile interface and then in the GithubProvider:

userinfo: {
  url: "https://api.github.com/user",
  async request({ client, tokens }) {
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    const profile = await client.userinfo(tokens.access_token!);

    if (!profile.email) {
      // If the user does not have a public email, get another via the GitHub API
      // See https://docs.github.com/en/rest/users/emails#list-public-email-addresses-for-the-authenticated-user
      const res = await fetch("https://api.github.com/user/emails", {
        headers: { Authorization: `token ${tokens.access_token}` },
      });

      if (res.ok) {
        const emails: GithubEmail[] = await res.json();
        const githubEmail = emails.find((e) => e.primary) ?? emails[0];
        profile.email = githubEmail!.email;
        
        // This here should do it
        profile.email_verified = githubEmail!.verified;
      }
    }

    return profile;
  },
}

tautastic avatar Feb 13 '24 12:02 tautastic