微信
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import { createAuthClient } from "better-auth/react";
|
||||
|
||||
const baseURL = ( "https://duooomi.com/") as string;
|
||||
|
||||
export const authClient = createAuthClient({
|
||||
baseURL,
|
||||
baseURL:"https://api.mixvideo.bowong.cc",
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
import { getCurrentUrl } from "@/lib/url";
|
||||
|
||||
export const wechatSignIn = async () => {
|
||||
// Per Better Auth docs: WeChat uses the social provider flow.
|
||||
@@ -9,19 +8,3 @@ export const wechatSignIn = async () => {
|
||||
});
|
||||
};
|
||||
|
||||
// If you prefer Generic OAuth style, you can use this variant instead.
|
||||
// (Requires your auth server to support the OAuth2 endpoint for providerId="wechat".)
|
||||
export const wechatSignInOAuth2 = async () => {
|
||||
// Note: Depending on your Better Auth client configuration (plugins),
|
||||
// `signIn.oauth2` may not be present in types.
|
||||
// This cast keeps the demo code working while still using your providerId="wechat".
|
||||
const signInAny = authClient as unknown as {
|
||||
signIn: { oauth2: (args: { providerId: string; callbackURL: string }) => Promise<unknown> };
|
||||
};
|
||||
|
||||
return signInAny.signIn.oauth2({
|
||||
providerId: "wechat",
|
||||
callbackURL: getCurrentUrl(),
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { useState } from "react";
|
||||
import { wechatSignIn, wechatSignInOAuth2 } from "@/lib/wechat";
|
||||
import { getCurrentUrl } from "@/lib/url";
|
||||
import { useCallback, useState } from "react";
|
||||
import { wechatSignIn } from "@/lib/wechat";
|
||||
|
||||
const WechatDemo = () => {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [result, setResult] = useState<string | null>(null);
|
||||
|
||||
const start = async (fn: () => Promise<unknown>) => {
|
||||
const start = useCallback(async (fn: () => Promise<unknown>) => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
setResult(null);
|
||||
@@ -22,7 +21,7 @@ const WechatDemo = () => {
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -48,25 +47,8 @@ const WechatDemo = () => {
|
||||
<h1 style={{ margin: "0 0 8px" }}>微信登录 Demo (Better Auth)</h1>
|
||||
<p style={{ margin: "0 0 16px", color: "#444" }}>
|
||||
需要你本地/线上先启动 Better Auth Server,并在服务端配置 `socialProviders.wechat`
|
||||
(`clientId/clientSecret`)。客户端再点击按钮触发登录跳转。
|
||||
(`clientId/clientSecret`)。点击下方按钮后才会发起微信登录跳转。
|
||||
</p>
|
||||
|
||||
<div style={{ marginBottom: 16 }}>
|
||||
<div style={{ fontSize: 13, color: "#555", marginBottom: 8 }}>当前 URL</div>
|
||||
<pre
|
||||
style={{
|
||||
margin: 0,
|
||||
padding: 12,
|
||||
background: "#f3f3f3",
|
||||
borderRadius: 8,
|
||||
overflow: "auto",
|
||||
color: "#222",
|
||||
}}
|
||||
>
|
||||
{getCurrentUrl()}
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<div style={{ display: "flex", gap: 12, flexWrap: "wrap" }}>
|
||||
<button
|
||||
onClick={() => start(wechatSignIn)}
|
||||
@@ -80,22 +62,7 @@ const WechatDemo = () => {
|
||||
cursor: "pointer",
|
||||
}}
|
||||
>
|
||||
微信登录(social)
|
||||
</button>
|
||||
|
||||
<button
|
||||
onClick={() => start(wechatSignInOAuth2)}
|
||||
disabled={loading}
|
||||
style={{
|
||||
padding: "10px 14px",
|
||||
borderRadius: 8,
|
||||
border: "1px solid #999",
|
||||
background: "#fff",
|
||||
color: "#111",
|
||||
cursor: "pointer",
|
||||
}}
|
||||
>
|
||||
微信登录(oauth2)
|
||||
微信登录
|
||||
</button>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -10,7 +10,17 @@ export default defineConfig({
|
||||
},
|
||||
},
|
||||
server: {
|
||||
allowedHosts: ["duooomi.com"]
|
||||
allowedHosts: ["duooomi.com"],
|
||||
proxy: {
|
||||
// Avoid browser CORS by tunneling requests to the upstream API.
|
||||
// `src/lib/auth-client.ts` uses baseURL `/mixvideo` in dev.
|
||||
"/mixvideo": {
|
||||
target: "https://api.mixvideo.bowong.cc",
|
||||
changeOrigin: true,
|
||||
secure: true,
|
||||
rewrite: (path) => path.replace(/^\/mixvideo/, ""),
|
||||
},
|
||||
},
|
||||
},
|
||||
preview: {
|
||||
allowedHosts: ["duooomi.com"]
|
||||
|
||||
Reference in New Issue
Block a user