@0xchain/with-login 1.1.0-beta.17 → 1.1.0-beta.19
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/LICENSE +21 -0
- package/dist/index.js +137 -90
- package/package.json +22 -8
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present 0xchain
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
CHANGED
|
@@ -1,103 +1,150 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { jsx
|
|
3
|
-
import
|
|
4
|
-
import { useLocale
|
|
5
|
-
import { twMerge
|
|
6
|
-
import { useTheme
|
|
7
|
-
import
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import React, { createContext, useState, useRef, useCallback, useEffect, useContext } from "react";
|
|
4
|
+
import { useLocale } from "next-intl";
|
|
5
|
+
import { twMerge } from "tailwind-merge";
|
|
6
|
+
import { useTheme } from "@0xchain/next-themes";
|
|
7
|
+
import Cookies from "js-cookie";
|
|
8
|
+
const isLogin = () => {
|
|
9
|
+
return Boolean(Cookies.get("refresh_token"));
|
|
10
|
+
};
|
|
11
|
+
var LoginMessageType = /* @__PURE__ */ ((LoginMessageType2) => {
|
|
12
|
+
LoginMessageType2["CLOSE_LOGIN_MODAL"] = "closeLoginModal";
|
|
13
|
+
LoginMessageType2["LOGIN_SUCCESS"] = "loginSuccess";
|
|
14
|
+
return LoginMessageType2;
|
|
15
|
+
})(LoginMessageType || {});
|
|
16
|
+
var OpenLoginType = /* @__PURE__ */ ((OpenLoginType2) => {
|
|
17
|
+
OpenLoginType2["LOGIN"] = "login";
|
|
18
|
+
OpenLoginType2["REGISTER"] = "register";
|
|
19
|
+
return OpenLoginType2;
|
|
20
|
+
})(OpenLoginType || {});
|
|
21
|
+
const LoginContext = createContext(void 0);
|
|
22
|
+
function LoginProvider({ children, loginSuccessCallback = () => Promise.resolve() }) {
|
|
23
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
24
|
+
const [mounted, setMounted] = useState(false);
|
|
25
|
+
const [url, setUrl] = useState(null);
|
|
26
|
+
const callbacksRef = useRef(/* @__PURE__ */ new Set());
|
|
27
|
+
const locale = useLocale();
|
|
28
|
+
const { resolvedTheme } = useTheme();
|
|
29
|
+
const currentTheme = mounted && resolvedTheme ? resolvedTheme : "light";
|
|
30
|
+
const openLogin = useCallback((type) => {
|
|
31
|
+
const redirect_uri = typeof window !== "undefined" ? window.location.href : "";
|
|
32
|
+
const loginUrl = `${process.env.NEXT_PUBLIC_AUTH_URL}/${locale}/login?mode=dialog&theme=${currentTheme}&redirect_uri=${redirect_uri}`;
|
|
33
|
+
const registerUrl = `${process.env.NEXT_PUBLIC_AUTH_URL}/${locale}/register?mode=dialog&theme=${currentTheme}&redirect_uri=${redirect_uri}`;
|
|
34
|
+
setIsOpen(true);
|
|
35
|
+
setUrl(type === "register" ? registerUrl : loginUrl);
|
|
36
|
+
}, [currentTheme, locale]);
|
|
37
|
+
const closeLogin = useCallback(() => {
|
|
38
|
+
setIsOpen(false);
|
|
39
|
+
}, []);
|
|
40
|
+
const setLoginOpen = useCallback((open) => {
|
|
41
|
+
setIsOpen(open);
|
|
19
42
|
}, []);
|
|
20
|
-
|
|
21
|
-
if (
|
|
22
|
-
const
|
|
23
|
-
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (currentTheme) {
|
|
45
|
+
const redirect_uri = typeof window !== "undefined" ? window.location.href : "";
|
|
46
|
+
const loginUrl = `${process.env.NEXT_PUBLIC_AUTH_URL}/${locale}/login?mode=dialog&theme=${currentTheme}&redirect_uri=${redirect_uri}`;
|
|
47
|
+
setUrl(loginUrl);
|
|
24
48
|
}
|
|
25
|
-
}, [
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
(
|
|
49
|
+
}, [currentTheme]);
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
setMounted(true);
|
|
52
|
+
const handleMessage = (event) => {
|
|
53
|
+
if (event.data?.type === "closeLoginModal") {
|
|
54
|
+
closeLogin();
|
|
55
|
+
}
|
|
56
|
+
if (event.data?.type === "loginSuccess") {
|
|
57
|
+
console.log("handleMessage", event.data.type);
|
|
58
|
+
closeLogin();
|
|
59
|
+
window.location.reload();
|
|
60
|
+
}
|
|
30
61
|
};
|
|
31
|
-
|
|
32
|
-
|
|
62
|
+
window.addEventListener("message", handleMessage);
|
|
63
|
+
return () => {
|
|
64
|
+
window.removeEventListener("message", handleMessage);
|
|
33
65
|
};
|
|
34
|
-
}, [
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
{
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
66
|
+
}, [closeLogin, loginSuccessCallback]);
|
|
67
|
+
const registerLoginSuccessCallback = useCallback((callback) => {
|
|
68
|
+
callbacksRef.current.add(callback);
|
|
69
|
+
return () => {
|
|
70
|
+
callbacksRef.current.delete(callback);
|
|
71
|
+
};
|
|
72
|
+
}, []);
|
|
73
|
+
const Trigger = useCallback(
|
|
74
|
+
({ children: children2, ...rest }) => /* @__PURE__ */ jsx("div", { onClick: () => openLogin(), style: { cursor: "pointer" }, ...rest, children: children2 }),
|
|
75
|
+
[openLogin]
|
|
76
|
+
);
|
|
77
|
+
const loginModalElement = React.useMemo(() => {
|
|
78
|
+
if (isLogin()) return null;
|
|
79
|
+
return /* @__PURE__ */ jsx(
|
|
80
|
+
"div",
|
|
81
|
+
{
|
|
82
|
+
className: twMerge(
|
|
83
|
+
" 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]",
|
|
84
|
+
"transition-all duration-500 zoom-in-out fixed block",
|
|
85
|
+
isOpen && "z-99999 opacity-100"
|
|
86
|
+
),
|
|
87
|
+
children: url && /* @__PURE__ */ jsx(
|
|
88
|
+
"iframe",
|
|
89
|
+
{
|
|
90
|
+
src: url ?? "",
|
|
91
|
+
className: twMerge(
|
|
92
|
+
"w-full h-full border-none relative z-10 transition-opacity duration-300",
|
|
93
|
+
"opacity-100"
|
|
94
|
+
),
|
|
95
|
+
title: "Login",
|
|
96
|
+
sandbox: "allow-scripts allow-forms allow-same-origin"
|
|
97
|
+
},
|
|
98
|
+
`login-iframe-${currentTheme}`
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
}, [
|
|
103
|
+
currentTheme,
|
|
104
|
+
isOpen,
|
|
105
|
+
url
|
|
106
|
+
]);
|
|
107
|
+
const value = {
|
|
108
|
+
isOpen,
|
|
109
|
+
openLogin,
|
|
110
|
+
closeLogin,
|
|
111
|
+
setLoginOpen,
|
|
112
|
+
url,
|
|
113
|
+
currentTheme,
|
|
114
|
+
mounted,
|
|
115
|
+
Trigger,
|
|
116
|
+
registerLoginSuccessCallback
|
|
76
117
|
};
|
|
77
|
-
return /* @__PURE__ */
|
|
78
|
-
|
|
79
|
-
|
|
118
|
+
return /* @__PURE__ */ jsxs(LoginContext.Provider, { value, children: [
|
|
119
|
+
children,
|
|
120
|
+
loginModalElement
|
|
80
121
|
] });
|
|
81
122
|
}
|
|
82
|
-
function
|
|
83
|
-
const
|
|
84
|
-
if (
|
|
123
|
+
function useLogin() {
|
|
124
|
+
const context = useContext(LoginContext);
|
|
125
|
+
if (context === void 0) {
|
|
85
126
|
throw new Error("useLogin must be used within a LoginProvider");
|
|
86
|
-
|
|
127
|
+
}
|
|
128
|
+
return context;
|
|
87
129
|
}
|
|
88
|
-
function
|
|
89
|
-
const { Trigger
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
|
|
93
|
-
|
|
130
|
+
function WithLogin({ children, onClick, ...rest }) {
|
|
131
|
+
const { Trigger, registerLoginSuccessCallback } = useLogin();
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
if (onClick) {
|
|
134
|
+
const unregister = registerLoginSuccessCallback(onClick);
|
|
135
|
+
return unregister;
|
|
136
|
+
}
|
|
137
|
+
return void 0;
|
|
138
|
+
}, [onClick, registerLoginSuccessCallback]);
|
|
139
|
+
if (!children) return null;
|
|
140
|
+
return /* @__PURE__ */ jsx(Trigger, { ...rest, children });
|
|
94
141
|
}
|
|
95
142
|
export {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
143
|
+
LoginMessageType,
|
|
144
|
+
LoginProvider,
|
|
145
|
+
OpenLoginType,
|
|
146
|
+
WithLogin,
|
|
147
|
+
WithLogin as default,
|
|
148
|
+
isLogin,
|
|
149
|
+
useLogin
|
|
103
150
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0xchain/with-login",
|
|
3
|
-
"version": "1.1.0-beta.
|
|
3
|
+
"version": "1.1.0-beta.19",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -17,16 +17,30 @@
|
|
|
17
17
|
"dist",
|
|
18
18
|
"!**/*.tsbuildinfo"
|
|
19
19
|
],
|
|
20
|
-
"
|
|
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",
|
|
20
|
+
"peerDependencies": {
|
|
26
21
|
"@0xchain/next-themes": "1.0.0",
|
|
22
|
+
"js-cookie": "3.0.5",
|
|
23
|
+
"next": "16.1.6",
|
|
27
24
|
"next-intl": "4.8.2",
|
|
25
|
+
"react": "19.1.1",
|
|
26
|
+
"react-dom": "19.1.1",
|
|
27
|
+
"tailwind-merge": "3.3.1"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@0xchain/next-themes": "1.0.0",
|
|
31
|
+
"@types/js-cookie": "3.0.6",
|
|
32
|
+
"js-cookie": "3.0.5",
|
|
28
33
|
"next": "16.1.6",
|
|
29
|
-
"
|
|
34
|
+
"next-intl": "4.8.2",
|
|
35
|
+
"react": "19.1.1",
|
|
36
|
+
"react-dom": "19.1.1",
|
|
37
|
+
"rollup-plugin-preserve-use-client": "3.0.1",
|
|
38
|
+
"tailwind-merge": "3.3.1"
|
|
39
|
+
},
|
|
40
|
+
"nx": {
|
|
41
|
+
"tags": [
|
|
42
|
+
"type:feature"
|
|
43
|
+
]
|
|
30
44
|
},
|
|
31
45
|
"publishConfig": {
|
|
32
46
|
"access": "public"
|