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

feat(providers): add WeChat #2519

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/providers/wechat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export default function WeChat(options) {
return {
id: "wechat",

name: "WeChat",
type: "oauth",
version: "2.0",
scope: options.scope,
params: { grant_type: "authorization_code" },
authorizationParams: {
appid: options.clientId
},
protection: "state",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
protection: "state",
protection: ["state"],


accessTokenUrl: "https://api.weixin.qq.com/sns/oauth2/access_token",
authorizationUrl:
"https://open.weixin.qq.com/connect/oauth2/authorize?response_type=code",
profileUrl: "https://api.weixin.qq.com/sns/userinfo",

async profile(profile) {
return {
id: profile.openid,
nickname: profile.nickname,
avatar: profile.headimgurl
Comment on lines +22 to +24
Copy link
Member

Choose a reason for hiding this comment

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

The upcoming version will require all built-in providers to return a unified output here by default, which is id, name, email, image, defaulting to null. Read #2411 and the corresponding RFC #1846 for context.

}
},
clientId: options.clientId,
clientSecret: options.clientSecret
}
}
14 changes: 14 additions & 0 deletions src/server/lib/oauth/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ async function getOAuth2AccessToken (code, provider, codeVerifier) {
{ algorithm: 'ES256', keyid: keyId }
)
params.client_secret = clientSecret
} else if (provider.id === 'wechat') {
params.appid = provider.clientId || provider.appid
params.secret = provider.clientSecret;
Comment on lines +134 to +136
Copy link
Member

@balazsorban44 balazsorban44 Aug 13, 2021

Choose a reason for hiding this comment

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

Custom behavior in the core like this won't be supported in the upcoming major release. Read #2411 and the corresponding RFC #1846 for context and to see how you can mitigate this.

} else {
params.client_secret = provider.clientSecret
}
Expand Down Expand Up @@ -232,6 +235,17 @@ async function getOAuth2 (provider, accessToken, results) {
if (provider.id === 'twitch') {
headers['Client-ID'] = provider.clientId
}

// WeChat has different url params design
if (provider.id === 'wechat') {
const wechatProfileURL = new URL(url)

wechatProfileURL.searchParams.append('access_token', results.accessToken)
wechatProfileURL.searchParams.append('openid', results.openid)

url = wechatProfileURL.href
}
Comment on lines +239 to +247
Copy link
Member

@balazsorban44 balazsorban44 Aug 13, 2021

Choose a reason for hiding this comment

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

Custom behavior in the core like this won't be supported in the upcoming major release. Read #2411 and the corresponding RFC #1846 for context and to see how you can mitigate this.


accessToken = null
}

Expand Down
1 change: 1 addition & 0 deletions types/providers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export type OAuthProviderType =
| "Twitch"
| "Twitter"
| "VK"
| "WeChat"
| "WordPress"
| "WorkOS"
| "Yandex"
Expand Down
30 changes: 30 additions & 0 deletions www/docs/providers/wechat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
id: wechat
title: WeChat
---

## Documentation

https://developers.weixin.qq.com/doc/offiaccount/en/OA_Web_Apps/Wechat_webpage_authorization.html

Copy link
Member

Choose a reason for hiding this comment

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

Please also add a Configuration section

## Options

The **WeChat Provider** comes with a set of default options:

- [WeChat Provider options](https://github.com/nextauthjs/next-auth/blob/main/src/providers/wechat.js)

You can override any of the options to suit your own use case.

## Example

```js
import Providers from `next-auth/providers`
...
providers: [
Providers.WeChat({
clientId: process.env.WECHAT_APP_ID,
clientSecret: process.env.WECHAT_APP_SECRET
})
]
...
```