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

fix(provider): update tiktok token #11308

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

DewinU
Copy link
Contributor

@DewinU DewinU commented Jul 2, 2024

☕️ Reasoning

  • Added the default issuer since tiktok does not have one
  • Added token url correction (these fixes issues on v4)
  • Added username to the default username request as fallback, since tiktok does not support email.

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

🎫 Affected issues

📌 Resources

Video Demostration

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

To make this work i also need to take control of the request with the conform function, since the token request manipulation was disable (see #11057)

let tkCode: string | undefined;
let tkCallback: string | undefined;
export const { handlers, auth, signIn, signOut } = NextAuth(req =>{
  if (req?.method === 'GET' && req?.headers.get('referer')?.includes('tiktok')) {
    const url = new URL(req?.url);
    tkCode = url.searchParams.get('code') as string;
    tkCallback = url.pathname
  }
  return {
    theme: { logo: "https://authjs.dev/img/logo-sm.png" },
    adapter: UnstorageAdapter(storage),
    providers: [
      GitHub,
      TikTok({
        clientId: process.env.AUTH_TIKTOK_KEY,
        clientSecret: process.env.AUTH_TIKTOK_SECRET,
        token: {
          async conform(response: Response) {
            const res = await fetch(response.url, {
              method: 'POST',
              headers: {
                'Cache-Control': 'no-cache',
                'Content-Type': 'application/x-www-form-urlencoded',
              },
              body: new URLSearchParams({
                client_key: process.env.AUTH_TIKTOK_KEY!,
                client_secret: process.env.AUTH_TIKTOK_SECRET!,
                code: tkCode!,
                grant_type: 'authorization_code',
                redirect_uri: process.env.AUTH_URL! + tkCallback ,
              }),
            });
            return res;
          },
        }
      })
    ],
    basePath: "/auth",
    callbacks: {
      authorized({ request, auth }) {
        const { pathname } = request.nextUrl
        if (pathname === "/middleware-example") return !!auth
        return true
      },
      jwt({ token, trigger, session, account }) {
        if (trigger === "update") token.name = session.user.name
        if (account?.provider === "keycloak") {
          return { ...token, accessToken: account.access_token }
        }
        return token
      },
      async session({ session, token }) {
        if (token?.accessToken) {
          session.accessToken = token.accessToken
        }
        return session
      },
    },
    experimental: {
      enableWebAuthn: true,
    },
    debug: process.env.NODE_ENV !== "production" ? true : false,
  } satisfies NextAuthConfig
})

@DewinU DewinU requested a review from ThangHuuVu as a code owner July 2, 2024 01:54
Copy link

vercel bot commented Jul 2, 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 Jul 2, 2024 2:02am
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
next-auth-docs ⬜️ Ignored (Inspect) Visit Preview Jul 2, 2024 2:02am

Copy link

vercel bot commented Jul 2, 2024

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

A member of the Team first needs to authorize it.

@github-actions github-actions bot added providers core Refers to `@auth/core` labels Jul 2, 2024
Copy link

codecov bot commented Jul 2, 2024

Codecov Report

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

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

Files Patch % Lines
packages/core/src/providers/tiktok.ts 0.00% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #11308   +/-   ##
=======================================
  Coverage        ?   39.91%           
=======================================
  Files           ?      179           
  Lines           ?    28671           
  Branches        ?     1249           
=======================================
  Hits            ?    11445           
  Misses          ?    17226           
  Partials        ?        0           

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

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.

None yet

1 participant