@ichaingo/with-login 1.4.31 → 1.4.33
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/dist/index.d.ts +12 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +71 -133
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
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
|
+
}
|
|
3
11
|
export interface UseLoginResult {
|
|
4
12
|
isOpen: boolean;
|
|
5
13
|
openLogin: () => void;
|
|
@@ -13,12 +21,15 @@ export interface UseLoginResult {
|
|
|
13
21
|
Trigger: React.FC<React.HTMLAttributes<HTMLDivElement>>;
|
|
14
22
|
LoginModal: React.FC;
|
|
15
23
|
}
|
|
24
|
+
interface UseLoginProps {
|
|
25
|
+
loginSuccessCallback?: () => Promise<void>;
|
|
26
|
+
}
|
|
16
27
|
/**
|
|
17
28
|
* 登录逻辑 + UI 全都在这个 hook 里面
|
|
18
29
|
* - 返回 Trigger:包裹触发登录的元素
|
|
19
30
|
* - 返回 LoginModal:实际渲染登录弹窗(使用 createPortal)
|
|
20
31
|
*/
|
|
21
|
-
export declare function useLogin(): UseLoginResult;
|
|
32
|
+
export declare function useLogin({ loginSuccessCallback }: UseLoginProps): UseLoginResult;
|
|
22
33
|
interface WithLoginProps extends React.PropsWithChildren, React.HTMLAttributes<any> {
|
|
23
34
|
children?: React.ReactNode;
|
|
24
35
|
}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAA2C,MAAM,OAAO,CAAC;AAOhE,eAAO,MAAM,OAAO,eAEnB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAA2C,MAAM,OAAO,CAAC;AAOhE,eAAO,MAAM,OAAO,eAEnB,CAAC;AACF,oBAAY,gBAAgB;IAC1B,iBAAiB,oBAAoB;IACrC,aAAa,iBAAiB;CAC/B;AACD,oBAAY,aAAa;IACvB,KAAK,UAAU;IACf,QAAQ,aAAa;CACtB;AACD,MAAM,WAAW,cAAc;IAE7B,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,MAAM,IAAI,CAAC;IACtB,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,YAAY,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IACtC,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,OAAO,CAAC;IACtB,gBAAgB,EAAE,MAAM,IAAI,CAAC;IAC7B,OAAO,EAAE,OAAO,CAAC;IAEjB,OAAO,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC;IACxD,UAAU,EAAE,KAAK,CAAC,EAAE,CAAC;CACtB;AACD,UAAU,aAAa;IACrB,oBAAoB,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5C;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,EAAC,oBAA8C,EAAC,EAAE,aAAa,GAAG,cAAc,CAgIxG;AAED,UAAU,cACR,SAAQ,KAAK,CAAC,iBAAiB,EAC7B,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC;IAC3B,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC5B;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAE,EAAE,cAAc,2CAS9D;AAED,eAAe,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,133 +1,69 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { jsx as
|
|
3
|
-
import { useState as
|
|
4
|
-
import { createPortal as
|
|
5
|
-
import { useLocale as
|
|
6
|
-
import { twMerge as
|
|
7
|
-
import { useTheme as
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return e[0] === '"' && (e = e.slice(1, -1)), e.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);
|
|
20
|
-
},
|
|
21
|
-
write: function(e) {
|
|
22
|
-
return encodeURIComponent(e).replace(
|
|
23
|
-
/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,
|
|
24
|
-
decodeURIComponent
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
function C(e, r) {
|
|
29
|
-
function t(o, c, n) {
|
|
30
|
-
if (!(typeof document > "u")) {
|
|
31
|
-
n = g({}, r, n), typeof n.expires == "number" && (n.expires = new Date(Date.now() + n.expires * 864e5)), n.expires && (n.expires = n.expires.toUTCString()), o = encodeURIComponent(o).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent).replace(/[()]/g, escape);
|
|
32
|
-
var s = "";
|
|
33
|
-
for (var i in n)
|
|
34
|
-
n[i] && (s += "; " + i, n[i] !== !0 && (s += "=" + n[i].split(";")[0]));
|
|
35
|
-
return document.cookie = o + "=" + e.write(c, o) + s;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
function a(o) {
|
|
39
|
-
if (!(typeof document > "u" || arguments.length && !o)) {
|
|
40
|
-
for (var c = document.cookie ? document.cookie.split("; ") : [], n = {}, s = 0; s < c.length; s++) {
|
|
41
|
-
var i = c[s].split("="), u = i.slice(1).join("=");
|
|
42
|
-
try {
|
|
43
|
-
var l = decodeURIComponent(i[0]);
|
|
44
|
-
if (n[l] = e.read(u, l), o === l)
|
|
45
|
-
break;
|
|
46
|
-
} catch {
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return o ? n[o] : n;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return Object.create(
|
|
53
|
-
{
|
|
54
|
-
set: t,
|
|
55
|
-
get: a,
|
|
56
|
-
remove: function(o, c) {
|
|
57
|
-
t(
|
|
58
|
-
o,
|
|
59
|
-
"",
|
|
60
|
-
g({}, c, {
|
|
61
|
-
expires: -1
|
|
62
|
-
})
|
|
63
|
-
);
|
|
64
|
-
},
|
|
65
|
-
withAttributes: function(o) {
|
|
66
|
-
return C(this.converter, g({}, this.attributes, o));
|
|
67
|
-
},
|
|
68
|
-
withConverter: function(o) {
|
|
69
|
-
return C(g({}, this.converter, o), this.attributes);
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
attributes: { value: Object.freeze(r) },
|
|
74
|
-
converter: { value: Object.freeze(e) }
|
|
75
|
-
}
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
var z = C(O, { path: "/" });
|
|
79
|
-
const b = () => !!z.get("id_token");
|
|
80
|
-
function A() {
|
|
81
|
-
const [e, r] = h(!1), [t, a] = h(!1), [o, c] = h(!1), n = R(), { resolvedTheme: s } = B();
|
|
82
|
-
w(() => {
|
|
83
|
-
a(!0);
|
|
2
|
+
import { jsx as a, jsxs as U, Fragment as T } from "react/jsx-runtime";
|
|
3
|
+
import { useState as f, useCallback as i, useEffect as h } from "react";
|
|
4
|
+
import { createPortal as C } from "react-dom";
|
|
5
|
+
import { useLocale as M } from "next-intl";
|
|
6
|
+
import { twMerge as v } from "tailwind-merge";
|
|
7
|
+
import { useTheme as O } from "@ichaingo/next-themes";
|
|
8
|
+
import S from "js-cookie";
|
|
9
|
+
const H = () => !!S.get("id_token");
|
|
10
|
+
var $ = /* @__PURE__ */ ((e) => (e.CLOSE_LOGIN_MODAL = "closeLoginModal", e.LOGIN_SUCCESS = "loginSuccess", e))($ || {}), b = /* @__PURE__ */ ((e) => (e.LOGIN = "login", e.REGISTER = "register", e))(b || {});
|
|
11
|
+
function x({ loginSuccessCallback: e = () => Promise.resolve() }) {
|
|
12
|
+
const [t, l] = f(!1), [r, w] = f(!1), [c, d] = f(!1), [G, _] = f(null), g = M(), { resolvedTheme: u } = O(), n = r && u ? u : "light", L = i((o) => {
|
|
13
|
+
const s = `${process.env.NEXT_PUBLIC_AUTH_URL}/${g}/login?mode=dialog&theme=${n}`, N = `${process.env.NEXT_PUBLIC_AUTH_URL}/${g}/register?mode=dialog&theme=${n}`;
|
|
14
|
+
l(!0), d(!1), _(o === "register" ? N : s);
|
|
15
|
+
}, [n, g]), m = i(() => {
|
|
16
|
+
l(!1);
|
|
17
|
+
}, []), E = i((o) => {
|
|
18
|
+
l(o), o && d(!1);
|
|
84
19
|
}, []);
|
|
85
|
-
|
|
86
|
-
r
|
|
87
|
-
}, []),
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
20
|
+
h(() => {
|
|
21
|
+
t && r && d(!1);
|
|
22
|
+
}, [u, t, r]), h(() => {
|
|
23
|
+
w(!0);
|
|
24
|
+
const o = (s) => {
|
|
25
|
+
s.data.type === "closeLoginModal" && m(), s.data.type === "loginSuccess" && (m(), e && e());
|
|
26
|
+
};
|
|
27
|
+
return window.addEventListener("message", o), () => {
|
|
28
|
+
window.removeEventListener("message", o);
|
|
29
|
+
};
|
|
91
30
|
}, []);
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}, [
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
[l]
|
|
100
|
-
), I = f(() => e ? k(
|
|
101
|
-
/* @__PURE__ */ x(
|
|
31
|
+
const p = i(() => {
|
|
32
|
+
d(!0);
|
|
33
|
+
}, []), I = i(
|
|
34
|
+
({ children: o, ...s }) => /* @__PURE__ */ a("div", { onClick: L, style: { cursor: "pointer" }, ...s, children: o }),
|
|
35
|
+
[L]
|
|
36
|
+
), y = i(() => t ? C(
|
|
37
|
+
/* @__PURE__ */ U(
|
|
102
38
|
"div",
|
|
103
39
|
{
|
|
104
|
-
className:
|
|
40
|
+
className: v(
|
|
105
41
|
"fixed w-0 h-9 bg-[rgba(0,0,0,0.7)] z-1000",
|
|
106
|
-
|
|
42
|
+
t && "top-0 left-0 w-full h-full"
|
|
107
43
|
),
|
|
108
44
|
children: [
|
|
109
|
-
/* @__PURE__ */
|
|
45
|
+
/* @__PURE__ */ a(
|
|
110
46
|
"div",
|
|
111
47
|
{
|
|
112
|
-
className:
|
|
48
|
+
className: v(
|
|
113
49
|
"absolute inset-0 transition-opacity duration-300",
|
|
114
|
-
|
|
50
|
+
c ? "opacity-0 pointer-events-none" : "opacity-100 z-30"
|
|
115
51
|
)
|
|
116
52
|
}
|
|
117
53
|
),
|
|
118
|
-
/* @__PURE__ */
|
|
54
|
+
/* @__PURE__ */ a(
|
|
119
55
|
"iframe",
|
|
120
56
|
{
|
|
121
|
-
src:
|
|
122
|
-
className:
|
|
57
|
+
src: loginUrl,
|
|
58
|
+
className: v(
|
|
123
59
|
"w-full h-full border-none relative z-10 transition-opacity duration-300",
|
|
124
|
-
|
|
60
|
+
c ? "opacity-100" : "opacity-0"
|
|
125
61
|
),
|
|
126
62
|
title: "Login",
|
|
127
|
-
onLoad:
|
|
63
|
+
onLoad: p,
|
|
128
64
|
sandbox: "allow-scripts allow-forms allow-same-origin"
|
|
129
65
|
},
|
|
130
|
-
`login-iframe-${
|
|
66
|
+
`login-iframe-${n}`
|
|
131
67
|
)
|
|
132
68
|
]
|
|
133
69
|
}
|
|
@@ -135,36 +71,38 @@ function A() {
|
|
|
135
71
|
document.body
|
|
136
72
|
) : null, [
|
|
137
73
|
m,
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
74
|
+
n,
|
|
75
|
+
p,
|
|
76
|
+
c,
|
|
77
|
+
t,
|
|
78
|
+
loginUrl
|
|
143
79
|
]);
|
|
144
80
|
return {
|
|
145
|
-
isOpen:
|
|
146
|
-
openLogin:
|
|
81
|
+
isOpen: t,
|
|
82
|
+
openLogin: L,
|
|
147
83
|
closeLogin: m,
|
|
148
|
-
setLoginOpen:
|
|
149
|
-
loginUrl
|
|
150
|
-
currentTheme:
|
|
151
|
-
iframeLoaded:
|
|
152
|
-
handleIframeLoad:
|
|
153
|
-
mounted:
|
|
154
|
-
Trigger:
|
|
155
|
-
LoginModal:
|
|
84
|
+
setLoginOpen: E,
|
|
85
|
+
loginUrl,
|
|
86
|
+
currentTheme: n,
|
|
87
|
+
iframeLoaded: c,
|
|
88
|
+
handleIframeLoad: p,
|
|
89
|
+
mounted: r,
|
|
90
|
+
Trigger: I,
|
|
91
|
+
LoginModal: y
|
|
156
92
|
};
|
|
157
93
|
}
|
|
158
|
-
function
|
|
159
|
-
const { Trigger:
|
|
160
|
-
return /* @__PURE__ */
|
|
161
|
-
e && /* @__PURE__ */
|
|
162
|
-
/* @__PURE__ */
|
|
94
|
+
function X({ children: e, ...t }) {
|
|
95
|
+
const { Trigger: l, LoginModal: r } = x();
|
|
96
|
+
return /* @__PURE__ */ U(T, { children: [
|
|
97
|
+
e && /* @__PURE__ */ a(l, { ...t, children: e }),
|
|
98
|
+
/* @__PURE__ */ a(r, {})
|
|
163
99
|
] });
|
|
164
100
|
}
|
|
165
101
|
export {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
102
|
+
$ as LoginMessageType,
|
|
103
|
+
b as OpenLoginType,
|
|
104
|
+
X as WithLogin,
|
|
105
|
+
X as default,
|
|
106
|
+
H as isLogin,
|
|
107
|
+
x as useLogin
|
|
170
108
|
};
|