-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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(core/providers): add wechat provider #10236
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Ignored Deployment
|
@KaygNas is attempting to deploy a commit to the authjs Team on Vercel. A member of the Team first needs to authorize it. |
Thanks this look dope |
Hey @balazsorban44, noticed many WeChat PRs got staled earlier on. Mind giving this one a look? Thanks for the help! |
@tonyabracadabra Hi there, you may adopt the provider into your app by yourself, have a look at this repo, and follow the steps below:
After all there done, start your app and it should be work.😉 |
@balazsorban44 @ndom91 @ThangHuuVu WeChat has 1.36 billion users worldwide, and I believe integrating this provider into NextAuth would benefit a lot of people. It would also enhance the usability of NextAuth. I hope you'll consider merging this PR. |
Hello, I am a pure beginner. I have implemented username and email login in my project. Now I am trying to use the file you provided to complete the third-party login of WeChat, but after I put the wechat.ts file in to the root directory of my project, the following error occurred: Object literal may only specify known properties, and 'options' does not exist in type 'OAuth2Config '.ts(2353) & { Please tell me if there is any step that I forgot to complete. I would be very grateful if I could get your guidance. |
@swingislee It can be a problem caused by your dependencies, please make sure |
@KaygNas When I cloned your project and tried to run it on my local windows PC, I got the same error. I checked that my version was indeed next-auth@5.0.0-beta.15. Being a pure beginner, I did some simple tests. If I create a new project in the @auth/core directory, an error will appear for the option value. If I copy your wechat code intact to other ts documents in the same directory, the error will disappear, so I am guessing whether it is because a new oauth policy has been added, but auth.v5 also needs to update settings in some places that I don't know to make this policy take effect? |
@swingislee It turns out |
You can do |
there was a few eslint issues in the original code and I've fixed them and compiled a new version you can try that out import type { OAuthConfig, OAuthUserConfig } from "next-auth/providers";
import { env } from "~/env";
export interface WeChatProfile {
openid: string;
nickname: string;
sex: number;
province: string;
city: string;
country: string;
headimgurl: string;
privilege: string[];
unionid: string;
[claim: string]: unknown;
}
interface WeChatTokenResponse {
access_token: string;
expires_in: number;
refresh_token: string;
openid: string;
scope: string;
unionid: string;
}
export default function WeChat<P extends WeChatProfile>(
options: OAuthUserConfig<P> & {
platformType: "OfficialAccount" | "WebsiteApp";
},
): OAuthConfig<P> {
const {
clientId = env.WECHAT_APP_ID,
clientSecret = env.WECHAT_APP_SECRET,
platformType,
} = options;
if (platformType !== "OfficialAccount" && platformType !== "WebsiteApp") {
throw new Error("Invalid plaformType");
}
const authorizationEndpointUrl =
platformType === "OfficialAccount"
? "https://open.weixin.qq.com/connect/oauth2/authorize"
: "https://open.weixin.qq.com/connect/qrconnect";
const authorizationScope =
platformType === "OfficialAccount" ? "snsapi_userinfo" : "snsapi_login";
const authorization = {
url: authorizationEndpointUrl,
params: {
appid: clientId,
response_type: "code",
scope: authorizationScope,
state: Math.random().toString(),
},
};
const tokenEndpointUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
const token = {
url: tokenEndpointUrl,
params: {
appid: clientId,
secret: clientSecret,
code: "CODE",
grant_type: "authorization_code",
},
conform: async (response: Response) => {
const data = (await response.json()) as WeChatTokenResponse;
const conformedResponse = new Response(
JSON.stringify({
...data,
token_type: "bearer",
}),
response,
);
return conformedResponse;
},
};
const userinfo = {
url: "https://api.weixin.qq.com/sns/userinfo",
request: async ({
tokens,
provider,
}: {
tokens: { access_token: string; openid: string };
provider: { userinfo?: { url?: string } };
}) => {
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() as Promise<P>;
},
};
const profile = (profile: WeChatProfile) => {
return {
id: profile.unionid,
name: profile.nickname,
email: null,
image: profile.headimgurl,
};
};
return {
id: "wechat",
name: "WeChat",
type: "oauth",
style: { logo: "/wechat.svg", bg: "#fff", text: "#000" },
checks: ["pkce", "state"],
clientId,
clientSecret,
authorization,
token,
userinfo,
profile,
options,
} as OAuthConfig<P>;
} |
I still have type errors. Do you have a working repo? |
☕️ Reasoning
Support WeChat Login
FYI: Getting a
WECHAT_APP_ID
andWECHAT_APP_SCRECT
is a bit difficult because audition is required for every application. However there are TEST ACCOUNT provided, follow the instruction below would help to do that:0. Apply a WeChat Account
Download WeChat and apply an account.
1. Apply a test account
Open the WeChat Official Account Test Account and scan the QRCode with WeChat on your phone.
2. Copy the
APP_ID
andAPP_SCRET
to the.env.local
fileOnce you login the website, the
APP_ID
andAPP_ACCOUNT
would generated automatically, just copy them to the.env.local
file.3. Set the
redirect_uri
with your local IPMake sure your
redirct_uri
is set correctly4. Download WeChat Dev Tool
The login can be only done in the environment of WeChat Browser, therefore it would be convenient to development using the WeChat Dev Tool. Accurately, it's not really convenient 🥲.
Download WeChat Dev Tool, select the stable version.
5. Access Your Localhost by the IP Using the Dev Tool
Click the Official Account Tab,
Start the
app/examples/nextjs
project, and input your localhost IP and click Sign In buttonClick Sign In With WeChat
6. Sign In Succeed
🧢 Checklist
🎫 Affected issues
Fixes: #7960
📌 Resources