@0xchain/with-login 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @0xchain/with-login
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Running unit tests
6
+
7
+ Run `nx test @0xchain/with-login` to execute the unit tests via [Vitest](https://vitest.dev/).
@@ -0,0 +1,46 @@
1
+ import { default as React } from 'react';
2
+ export declare const isLogin: () => boolean;
3
+ export declare enum LoginMessageType {
4
+ CLOSE_LOGIN_MODAL = "closeLoginModal",
5
+ LOGIN_SUCCESS = "loginSuccess"
6
+ }
7
+ export declare enum OpenLoginType {
8
+ LOGIN = "login",
9
+ REGISTER = "register"
10
+ }
11
+ export interface LoginContextValue {
12
+ isOpen: boolean;
13
+ openLogin: (type?: OpenLoginType) => void;
14
+ closeLogin: () => void;
15
+ setLoginOpen: (open: boolean) => void;
16
+ url: string | null;
17
+ currentTheme: string;
18
+ mounted: boolean;
19
+ Trigger: React.FC<React.HTMLAttributes<HTMLDivElement>>;
20
+ registerLoginSuccessCallback: (callback: () => void | Promise<void>) => () => void;
21
+ }
22
+ interface LoginProviderProps {
23
+ children: React.ReactNode;
24
+ loginSuccessCallback?: () => Promise<void>;
25
+ }
26
+ /**
27
+ * LoginProvider 组件:提供全局登录状态和功能
28
+ */
29
+ export declare function LoginProvider({ children, loginSuccessCallback }: LoginProviderProps): import("react/jsx-runtime").JSX.Element;
30
+ /**
31
+ * useLogin hook:消费 LoginContext
32
+ */
33
+ export declare function useLogin(): LoginContextValue;
34
+ interface WithLoginProps extends React.PropsWithChildren, React.HTMLAttributes<HTMLElement> {
35
+ children?: React.ReactNode;
36
+ onClick?: () => void;
37
+ }
38
+ /**
39
+ * 一个简单封装:
40
+ * - children 作为触发区域
41
+ * - 使用全局 LoginProvider 的登录功能
42
+ * - 如果提供了 onClick,会在登录成功后调用
43
+ */
44
+ export declare function WithLogin({ children, onClick, ...rest }: WithLoginProps): import("react/jsx-runtime").JSX.Element | null;
45
+ export default WithLogin;
46
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAA8E,MAAM,OAAO,CAAC;AAMnG,eAAO,MAAM,OAAO,eAGnB,CAAC;AAEF,oBAAY,gBAAgB;IAC1B,iBAAiB,oBAAoB;IACrC,aAAa,iBAAiB;CAC/B;AAED,oBAAY,aAAa;IACvB,KAAK,UAAU;IACf,QAAQ,aAAa;CACtB;AAED,MAAM,WAAW,iBAAiB;IAEhC,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,CAAC,IAAI,CAAC,EAAE,aAAa,KAAK,IAAI,CAAC;IAC1C,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,YAAY,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IACtC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IAEjB,OAAO,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC;IAExD,4BAA4B,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,MAAM,IAAI,CAAC;CACpF;AAED,UAAU,kBAAkB;IAC1B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,oBAAoB,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5C;AAKD;;GAEG;AACH,wBAAgB,aAAa,CAAC,EAAE,QAAQ,EAAE,oBAA8C,EAAE,EAAE,kBAAkB,2CA0H7G;AAED;;GAEG;AACH,wBAAgB,QAAQ,IAAI,iBAAiB,CAM5C;AAED,UAAU,cACR,SAAQ,KAAK,CAAC,iBAAiB,EAC7B,KAAK,CAAC,cAAc,CAAC,WAAW,CAAC;IACnC,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACtB;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,cAAc,kDAevE;AAED,eAAe,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,103 @@
1
+ 'use client';
2
+ import { jsx as m, jsxs as y } from "react/jsx-runtime";
3
+ import I, { createContext as M, useState as w, useRef as N, useCallback as c, useEffect as p, useContext as R } from "react";
4
+ import { useLocale as O } from "next-intl";
5
+ import { twMerge as $ } from "tailwind-merge";
6
+ import { useTheme as P } from "@0xchain/next-themes";
7
+ import k from "js-cookie";
8
+ const z = () => !!k.get("refresh_token");
9
+ var A = /* @__PURE__ */ ((e) => (e.CLOSE_LOGIN_MODAL = "closeLoginModal", e.LOGIN_SUCCESS = "loginSuccess", e))(A || {}), B = /* @__PURE__ */ ((e) => (e.LOGIN = "login", e.REGISTER = "register", e))(B || {});
10
+ const C = M(void 0);
11
+ function F({ children: e, loginSuccessCallback: n = () => Promise.resolve() }) {
12
+ const [i, s] = w(!1), [l, h] = w(!1), [a, v] = w(null), _ = N(/* @__PURE__ */ new Set()), u = O(), { resolvedTheme: U } = P(), t = l && U ? U : "light", L = c((o) => {
13
+ const r = typeof window < "u" ? window.location.href : "", g = `${process.env.NEXT_PUBLIC_AUTH_URL}/${u}/login?mode=dialog&theme=${t}&redirect_uri=${r}`, f = `${process.env.NEXT_PUBLIC_AUTH_URL}/${u}/register?mode=dialog&theme=${t}&redirect_uri=${r}`;
14
+ s(!0), v(o === "register" ? f : g);
15
+ }, [t, u]), d = c(() => {
16
+ s(!1);
17
+ }, []), E = c((o) => {
18
+ s(o);
19
+ }, []);
20
+ p(() => {
21
+ if (t) {
22
+ const o = typeof window < "u" ? window.location.href : "", r = `${process.env.NEXT_PUBLIC_AUTH_URL}/${u}/login?mode=dialog&theme=${t}&redirect_uri=${o}`;
23
+ v(r);
24
+ }
25
+ }, [t]), p(() => {
26
+ h(!0);
27
+ const o = (r) => {
28
+ var g, f;
29
+ ((g = r.data) == null ? void 0 : g.type) === "closeLoginModal" && d(), ((f = r.data) == null ? void 0 : f.type) === "loginSuccess" && (console.log("handleMessage", r.data.type), d(), window.location.reload());
30
+ };
31
+ return window.addEventListener("message", o), () => {
32
+ window.removeEventListener("message", o);
33
+ };
34
+ }, [d, n]);
35
+ const S = c((o) => (_.current.add(o), () => {
36
+ _.current.delete(o);
37
+ }), []), x = c(
38
+ ({ children: o, ...r }) => /* @__PURE__ */ m("div", { onClick: () => L(), style: { cursor: "pointer" }, ...r, children: o }),
39
+ [L]
40
+ ), T = I.useMemo(() => z() ? null : /* @__PURE__ */ m(
41
+ "div",
42
+ {
43
+ className: $(
44
+ " left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 w-full h-full bg-[rgba(0,0,0,0.7)] opacity-0 z-[-1]",
45
+ "transition-all duration-500 zoom-in-out fixed block",
46
+ i && "z-99999 opacity-100"
47
+ ),
48
+ children: a && /* @__PURE__ */ m(
49
+ "iframe",
50
+ {
51
+ src: a ?? "",
52
+ className: $(
53
+ "w-full h-full border-none relative z-10 transition-opacity duration-300",
54
+ "opacity-100"
55
+ ),
56
+ title: "Login",
57
+ sandbox: "allow-scripts allow-forms allow-same-origin"
58
+ },
59
+ `login-iframe-${t}`
60
+ )
61
+ }
62
+ ), [
63
+ t,
64
+ i,
65
+ a
66
+ ]), b = {
67
+ isOpen: i,
68
+ openLogin: L,
69
+ closeLogin: d,
70
+ setLoginOpen: E,
71
+ url: a,
72
+ currentTheme: t,
73
+ mounted: l,
74
+ Trigger: x,
75
+ registerLoginSuccessCallback: S
76
+ };
77
+ return /* @__PURE__ */ y(C.Provider, { value: b, children: [
78
+ e,
79
+ T
80
+ ] });
81
+ }
82
+ function G() {
83
+ const e = R(C);
84
+ if (e === void 0)
85
+ throw new Error("useLogin must be used within a LoginProvider");
86
+ return e;
87
+ }
88
+ function J({ children: e, onClick: n, ...i }) {
89
+ const { Trigger: s, registerLoginSuccessCallback: l } = G();
90
+ return p(() => {
91
+ if (n)
92
+ return l(n);
93
+ }, [n, l]), e ? /* @__PURE__ */ m(s, { ...i, children: e }) : null;
94
+ }
95
+ export {
96
+ A as LoginMessageType,
97
+ F as LoginProvider,
98
+ B as OpenLoginType,
99
+ J as WithLogin,
100
+ J as default,
101
+ z as isLogin,
102
+ G as useLogin
103
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@0xchain/with-login",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ "./package.json": "./package.json",
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "!**/*.tsbuildinfo"
19
+ ],
20
+ "devDependencies": {
21
+ "rollup-plugin-preserve-use-client": "3.0.1",
22
+ "@types/js-cookie": "3.0.6"
23
+ },
24
+ "dependencies": {
25
+ "tailwind-merge": "3.3.1",
26
+ "@0xchain/next-themes": "1.0.0",
27
+ "next-intl": "4.6.1",
28
+ "next": "15.5.6",
29
+ "js-cookie": "3.0.5"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ }
34
+ }