Skip to content

Commit

Permalink
feat(providers): add wechat provider (#10236)
Browse files Browse the repository at this point in the history
* feat(core/providers): add wechat provider

* Update wechat.ts

* Update .env.local.example

* Update wechat.ts

* Update auth.ts

* Update wechat.ts

* Update auth.ts

---------

Co-authored-by: Balázs Orbán <[email protected]>
  • Loading branch information
KaygNas and balazsorban44 authored Aug 15, 2024
1 parent 51c0bbd commit d1dfb52
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/static/img/providers/wechat.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
145 changes: 145 additions & 0 deletions packages/core/src/providers/wechat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* <div style={{backgroundColor: "#24292f", display: "flex", justifyContent: "space-between", color: "#fff", padding: 16}}>
* <span>Built-in <b>WeChat</b> integration.</span>
* <a href="https://www.wechat.com/">
* <img style={{display: "block"}} src="https://authjs.dev/img/providers/wechat.svg" height="48" width="48"/>
* </a>
* </div>
*
* @module providers/wechat
*/
import type {
AuthorizationEndpointHandler,
OAuthConfig,
OAuthUserConfig,
TokenEndpointHandler,
UserinfoEndpointHandler,
} from "./"

/** @see [Get the authenticated user](https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Authorized_Interface_Calling_UnionID.html) */
export interface WeChatProfile {
openid: string
nickname: string
sex: number
province: string
city: string
country: string
headimgurl: string
privilege: string[]
unionid: string
[claim: string]: unknown
}

/**
* Add WeChat login to your page and make requests to [WeChat APIs](https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Authorized_Interface_Calling_UnionID.html).
*
* ### Setup
*
* #### Callback URL
* ```
* https://example.com/api/auth/callback/wechat
* ```
*
* #### Configuration
* ```ts
* import { Auth } from "@auth/core"
* import WeChat from "@auth/core/providers/wechat"
*
* const request = new Request(origin)
* const response = await Auth(request, {
* providers: [WeChat({
* clientId: AUTH_WECHAT_APP_ID,
* clientSecret: AUTH_WECHAT_APP_SECRET,
* platformType: "OfficialAccount",
* })],
* })
* ```
*
* ### Resources
*
* - [WeChat Official Account](https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html)
* - [WeChat Official Account - Webpage Authorization](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html)
* - [WeChat Official Account Test Account](https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login)
* - [WeChat WebsiteApp Login](https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html)
* - [使用微信测试账号对网页进行授权](https://cloud.tencent.com/developer/article/1703167)
*
* :::tip
*
* The WeChat provider comes with a [default configuration](https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/wechat.ts).
* To override the defaults for your use case, check out [customizing a built-in OAuth provider](https://authjs.dev/guides/providers/custom-provider#override-default-options).
*
* :::
*
* :::info **Disclaimer**
*
* If you think you found a bug in the default configuration, you can [open an issue](https://authjs.dev/new/provider-issue).
*
* Auth.js strictly adheres to the specification and it cannot take responsibility for any deviation from
* the spec by the provider. You can open an issue, but if the problem is non-compliance with the spec,
* we might not pursue a resolution. You can ask for more help in [Discussions](https://authjs.dev/new/github-discussions).
*
* :::
*/

export default function WeChat<P extends WeChatProfile>(
options: OAuthUserConfig<P> & {
platformType?: "OfficialAccount" | "WebsiteApp"
}
): OAuthConfig<P> {
const { clientId, clientSecret, platformType = "OfficialAccount" } = options
console.log(options)
return {
id: "wechat",
name: "WeChat",
type: "oauth",
style: { logo: "/wechat.svg", bg: "#fff", text: "#000" },
checks: ["state"],
authorization: {
url:
platformType === "OfficialAccount"
? "https://open.weixin.qq.com/connect/oauth2/authorize"
: "https://open.weixin.qq.com/connect/qrconnect",
params: {
appid: clientId,
scope:
platformType === "OfficialAccount"
? "snsapi_userinfo"
: "snsapi_login",
},
},
token: {
url: "https://api.weixin.qq.com/sns/oauth2/access_token",
params: { appid: clientId, secret: clientSecret },
conform: async (response) => {
const data = await response.json()
if (data.token_type === "bearer") {
console.warn(
"token_type is 'bearer'. Redundant workaround, please open an issue."
)
return response
}
return Response.json({ ...data, token_type: "bearer" }, response)
},
},
userinfo: {
url: "https://api.weixin.qq.com/sns/userinfo",
request: async ({ tokens, provider }) => {
const url = new URL(provider.userinfo?.url!)
url.searchParams.set("access_token", tokens.access_token!)
url.searchParams.set("openid", String(tokens.openid))
url.searchParams.set("lang", "zh_CN")
const response = await fetch(url)
return response.json()
},
},
profile: (profile: WeChatProfile) => {
return {
id: profile.unionid,
name: profile.nickname,
email: null,
image: profile.headimgurl,
}
},
options,
}
}

0 comments on commit d1dfb52

Please sign in to comment.