Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tiktok): Update TikTok provider configuration with custom Fetch #12168

Merged
merged 4 commits into from
Nov 4, 2024

Conversation

DewinU
Copy link
Contributor

@DewinU DewinU commented Nov 1, 2024

☕️ Reasoning

  • Refactor Tiktok Provider default configuration to use the new customFetch feature feat(oauth): expose custom fetch #11975
  • Added more documentation on how to setup this provider using de default configuration and also as a custom provider on your own.

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

🎫 Affected issues

📌 Resources

Video Demostration

https://youtu.be/M-PCU-e8IGI?si=uunGbOxhNXIRlnXT

Custom Provider

You dont really need to wait for this PR to be merged, if you already need the Tiktok Provider on your application, you can set it up as a custom provider

const CustomTiktok: OAuth2Config<any> & Provider =
{
  async [customFetch](...args) {
    const url = new URL(args[0] instanceof Request ? args[0].url : args[0])
    if (url.pathname.endsWith("/token/")) {
      const [url, request] = args

      const customHeaders = {
        ...request?.headers,
        "content-type": "application/x-www-form-urlencoded",
      }

      const customBody = new URLSearchParams(request?.body as string)
      customBody.append("client_key", process.env.AUTH_TIKTOK_ID!)
      const response = await fetch(url, {
        ...request,
        headers: customHeaders,
        body: customBody.toString(),
      })
      const json = await response.json()
      return Response.json({ ...json })
    }
    return fetch(...args)
  },
  id: "tiktok",
  name: "TikTok",
  type: "oauth",
  client: {
    token_endpoint_auth_method: "client_secret_post",
  },
  authorization: {
    url: "https://www.tiktok.com/v2/auth/authorize",
    params: {
      client_key: process.env.AUTH_TIKTOK_ID,
      scope: "user.info.profile",
    },
  },

  token: "https://open.tiktokapis.com/v2/oauth/token/",
  userinfo:
    "https://open.tiktokapis.com/v2/user/info/?fields=open_id,avatar_url,display_name,username",

  profile(profile) {
    return {
      id: profile.data.user.open_id,
      name: profile.data.user.display_name,
      image: profile.data.user.avatar_url,
      email: profile.data.user.email || profile.data.user.username || null,
    }
  },
}

and then add it at your list of providers

export const { handlers, auth } = NextAuth({ providers: [ GitHub, CustomTiktok ] })

Any feedback will be greatly appreciated.

Copy link

vercel bot commented Nov 1, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
auth-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 4, 2024 9:45am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
next-auth-docs ⬜️ Ignored (Inspect) Visit Preview Nov 4, 2024 9:45am

Copy link

vercel bot commented Nov 1, 2024

@DewinU is attempting to deploy a commit to the authjs Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

codecov bot commented Nov 1, 2024

Codecov Report

Attention: Patch coverage is 0% with 97 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@c5a70d3). Learn more about missing BASE report.

Files with missing lines Patch % Lines
packages/core/src/providers/tiktok.ts 0.00% 97 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #12168   +/-   ##
=======================================
  Coverage        ?   39.15%           
=======================================
  Files           ?      191           
  Lines           ?    29979           
  Branches        ?     1297           
=======================================
  Hits            ?    11738           
  Misses          ?    18241           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mattstrick
Copy link

Looking forward to this update!

@@ -153,13 +155,15 @@ export interface TiktokProfile extends Record<string, any> {
* - [TikTok app console](https://developers.tiktok.com/)
* - [TikTok login kit documentation](https://developers.tiktok.com/doc/login-kit-web/)
* - [Avaliable Scopes](https://developers.tiktok.com/doc/tiktok-api-scopes/)
* - [Sandbox for testing](https://developers.tiktok.com/blog/introducing-sandbox)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is huge! 🤯 Finally!

@balazsorban44
Copy link
Member

Amazing, thank you @DewinU!

@balazsorban44 balazsorban44 merged commit 2e78fa2 into nextauthjs:main Nov 4, 2024
9 of 12 checks passed
@DewinU DewinU deleted the fix/tiktok branch November 5, 2024 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Refers to `@auth/core` providers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants