-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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", | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
}, | ||
clientId: options.clientId, | ||
clientSecret: options.clientSecret | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} else { | ||
params.client_secret = provider.clientSecret | ||
} | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
accessToken = null | ||
} | ||
|
||
|
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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
}) | ||
] | ||
... | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.