|
| 1 | +/** |
| 2 | + * <div style={{backgroundColor: "#000", display: "flex", justifyContent: "space-between", color: "#fff", padding: 16}}> |
| 3 | + * <span>Built-in <b>Threads</b> integration.</span> |
| 4 | + * <a href="https://www.threads.net/"> |
| 5 | + * <img style={{display: "block"}} src="https://authjs.dev/img/providers/threads.svg" height="48" /> |
| 6 | + * </a> |
| 7 | + * </div> |
| 8 | + * |
| 9 | + * @module providers/threads |
| 10 | + */ |
| 11 | +import type { OAuthConfig, OAuthUserConfig } from "./index.js" |
| 12 | + |
| 13 | +/** |
| 14 | + * [User](https://developers.facebook.com/docs/threads/reference/user) |
| 15 | + */ |
| 16 | +export interface ThreadsProfile { |
| 17 | + data: { |
| 18 | + /** |
| 19 | + * Unique identifier of this user. This is returned as a string in order to avoid complications with languages and tools |
| 20 | + * that cannot handle large integers. |
| 21 | + */ |
| 22 | + id: string |
| 23 | + /** |
| 24 | + * The Threads handle (username) of this user. |
| 25 | + * |
| 26 | + * To return this field, add `fields=username` in the authorization request's query parameter. |
| 27 | + */ |
| 28 | + username?: string |
| 29 | + /** |
| 30 | + * The URL to the profile image for this user, as shown on the user's profile. |
| 31 | + * |
| 32 | + * To return this field, add `fields=threads_profile_picture_url` in the authorization request's query parameter. |
| 33 | + */ |
| 34 | + threads_profile_picture_url?: string |
| 35 | + /** |
| 36 | + * The text of this user's profile biography (also known as bio), if the user provided one. |
| 37 | + * |
| 38 | + * To return this field, add `fields=threads_biography` in the authorization request's query parameter. |
| 39 | + */ |
| 40 | + threads_biography?: string |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Add Threads login to your page. |
| 46 | + * |
| 47 | + * ### Setup |
| 48 | + * |
| 49 | + * #### Callback URL |
| 50 | + * ``` |
| 51 | + * https://example.com/api/auth/callback/threads |
| 52 | + * ``` |
| 53 | + * |
| 54 | + * #### Configuration |
| 55 | + *```ts |
| 56 | + * import { Auth } from "@auth/core" |
| 57 | + * import Threads from "@auth/core/providers/threads" |
| 58 | + * |
| 59 | + * const request = new Request(origin) |
| 60 | + * const response = await Auth(request, { |
| 61 | + * providers: [ |
| 62 | + * Threads({ |
| 63 | + * clientId: THREADS_CLIENT_ID, |
| 64 | + * clientSecret: THREADS_CLIENT_SECRET, |
| 65 | + * }), |
| 66 | + * ], |
| 67 | + * }) |
| 68 | + * ``` |
| 69 | + * |
| 70 | + * ### Resources |
| 71 | + * |
| 72 | + * - [Threads OAuth documentation](https://developers.facebook.com/docs/threads) |
| 73 | + * - [Threads OAuth apps](https://developers.facebook.com/apps/) |
| 74 | + * |
| 75 | + * ### Notes |
| 76 | + * |
| 77 | + * :::warning |
| 78 | + * |
| 79 | + * Email address is not returned by the Threads API. |
| 80 | + * |
| 81 | + * ::: |
| 82 | + * |
| 83 | + * :::tip |
| 84 | + * |
| 85 | + * Threads required callback URL to be configured in your Facebook app and Facebook required you to use **https** even for localhost! In order to do that, you either need to [add an SSL to your localhost](https://www.freecodecamp.org/news/how-to-get-https-working-on-your-local-development-environment-in-5-minutes-7af615770eec/) or use a proxy such as [ngrok](https://ngrok.com/docs). |
| 86 | + * |
| 87 | + * ::: |
| 88 | + * |
| 89 | + * By default, Auth.js assumes that the Threads provider is |
| 90 | + * based on the [OAuth 2](https://www.rfc-editor.org/rfc/rfc6749.html) specification. |
| 91 | + * |
| 92 | + * :::tip |
| 93 | + * |
| 94 | + * The Threads provider comes with a [default configuration](https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/threads.ts). |
| 95 | + * To override the defaults for your use case, check out [customizing a built-in OAuth provider](https://authjs.dev/guides/configuring-oauth-providers). |
| 96 | + * |
| 97 | + * ::: |
| 98 | + * |
| 99 | + * :::info **Disclaimer** |
| 100 | + * |
| 101 | + * If you think you found a bug in the default configuration, you can [open an issue](https://authjs.dev/new/provider-issue). |
| 102 | + * |
| 103 | + * Auth.js strictly adheres to the specification and it cannot take responsibility for any deviation from |
| 104 | + * the spec by the provider. You can open an issue, but if the problem is non-compliance with the spec, |
| 105 | + * we might not pursue a resolution. You can ask for more help in [Discussions](https://authjs.dev/new/github-discussions). |
| 106 | + * |
| 107 | + * ::: |
| 108 | + */ |
| 109 | +export default function Threads( |
| 110 | + config: OAuthUserConfig<ThreadsProfile> |
| 111 | +): OAuthConfig<ThreadsProfile> { |
| 112 | + return { |
| 113 | + id: "threads", |
| 114 | + name: "Threads", |
| 115 | + type: "oauth", |
| 116 | + checks: ["state"], |
| 117 | + authorization: "https://threads.net/oauth/authorize?scope=threads_basic", |
| 118 | + token: "https://graph.threads.net/oauth/access_token", |
| 119 | + userinfo: |
| 120 | + "https://graph.threads.net/v1.0/me?fields=id,username,threads_profile_picture_url", |
| 121 | + client: { |
| 122 | + token_endpoint_auth_method: "client_secret_post", |
| 123 | + }, |
| 124 | + profile({ data }) { |
| 125 | + return { |
| 126 | + id: data.id, |
| 127 | + name: data.username || null, |
| 128 | + email: null, |
| 129 | + image: data.threads_profile_picture_url || null, |
| 130 | + } |
| 131 | + }, |
| 132 | + style: { bg: "#000", text: "#fff" }, |
| 133 | + options: config, |
| 134 | + } |
| 135 | +} |
0 commit comments