@pradip1995/segment-login-popup 0.1.3 → 0.1.4
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/package.json +8 -6
- package/src/index.ts +6 -0
- package/src/login-popup-provider.tsx +13 -0
- package/src/login-required-modal.tsx +6 -0
- package/src/request-login.test.ts +27 -0
- package/src/request-login.ts +22 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pradip1995/segment-login-popup",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -16,12 +16,9 @@
|
|
|
16
16
|
"./manifest": "./src/manifest.ts",
|
|
17
17
|
"./provider": "./src/login-popup-provider.tsx",
|
|
18
18
|
"./required": "./src/login-required-modal.tsx",
|
|
19
|
+
"./request-login": "./src/request-login.ts",
|
|
19
20
|
"./types": "./src/types.ts"
|
|
20
21
|
},
|
|
21
|
-
"scripts": {
|
|
22
|
-
"typecheck": "tsc --noEmit",
|
|
23
|
-
"lint": "tsc --noEmit"
|
|
24
|
-
},
|
|
25
22
|
"peerDependencies": {
|
|
26
23
|
"@pradip1995/commerce-auth": "^4.0.0",
|
|
27
24
|
"@pradip1995/commerce-core": "^4.0.0",
|
|
@@ -42,5 +39,10 @@
|
|
|
42
39
|
"@types/react": "^19",
|
|
43
40
|
"react": "19.0.3",
|
|
44
41
|
"typescript": "^5.7.2"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"typecheck": "tsc --noEmit",
|
|
45
|
+
"lint": "tsc --noEmit",
|
|
46
|
+
"test": "node --experimental-strip-types --test src/*.test.ts"
|
|
45
47
|
}
|
|
46
|
-
}
|
|
48
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -6,6 +6,12 @@ export {
|
|
|
6
6
|
useLoginPopup,
|
|
7
7
|
} from "./login-popup-provider"
|
|
8
8
|
export { default as LoginRequiredModal } from "./login-required-modal"
|
|
9
|
+
export {
|
|
10
|
+
LOGIN_POPUP_OPEN_EVENT,
|
|
11
|
+
isAuthRequiredError,
|
|
12
|
+
promptLoginForWishlist,
|
|
13
|
+
requestLoginPopup,
|
|
14
|
+
} from "./request-login"
|
|
9
15
|
export {
|
|
10
16
|
DEFAULT_LOGIN_POPUP_FEATURE_CARDS,
|
|
11
17
|
normalizeLoginPopupConfig,
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
normalizeLoginPopupConfig,
|
|
16
16
|
type LoginPopupConfig,
|
|
17
17
|
} from "./types"
|
|
18
|
+
import { LOGIN_POPUP_OPEN_EVENT } from "./request-login"
|
|
18
19
|
|
|
19
20
|
type LoginPopupContextValue = {
|
|
20
21
|
isPopupOpen: boolean
|
|
@@ -77,6 +78,18 @@ export function LoginPopupProvider({
|
|
|
77
78
|
setIsPopupOpen(true)
|
|
78
79
|
}, [])
|
|
79
80
|
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
if (isLoggedIn) return
|
|
83
|
+
|
|
84
|
+
const onOpen = (event: Event) => {
|
|
85
|
+
event.preventDefault()
|
|
86
|
+
setIsPopupOpen(true)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
window.addEventListener(LOGIN_POPUP_OPEN_EVENT, onOpen)
|
|
90
|
+
return () => window.removeEventListener(LOGIN_POPUP_OPEN_EVENT, onOpen)
|
|
91
|
+
}, [isLoggedIn])
|
|
92
|
+
|
|
80
93
|
useEffect(() => {
|
|
81
94
|
setHydrated(true)
|
|
82
95
|
document.body.classList.remove("login-popup-open")
|
|
@@ -6,6 +6,12 @@ import {
|
|
|
6
6
|
GOOGLE_LOGIN_COUNTRY_CODE_KEY,
|
|
7
7
|
} from "@pradip1995/commerce-auth/util/google-auth-client"
|
|
8
8
|
|
|
9
|
+
export {
|
|
10
|
+
isAuthRequiredError,
|
|
11
|
+
promptLoginForWishlist,
|
|
12
|
+
requestLoginPopup,
|
|
13
|
+
} from "./request-login"
|
|
14
|
+
|
|
9
15
|
type LoginRequiredModalProps = {
|
|
10
16
|
isOpen: boolean
|
|
11
17
|
onClose: () => void
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import { isAuthRequiredError } from "./request-login.ts"
|
|
4
|
+
|
|
5
|
+
const CASES: Array<{
|
|
6
|
+
name: string
|
|
7
|
+
error: string | null | undefined
|
|
8
|
+
want: boolean
|
|
9
|
+
}> = [
|
|
10
|
+
{ name: "login in please login message", error: "Please login to add items to wishlist.", want: true },
|
|
11
|
+
{ name: "login required", error: "Login required", want: true },
|
|
12
|
+
{ name: "unauthorized", error: "Unauthorized: No valid session found", want: true },
|
|
13
|
+
{ name: "unauthorised spelling", error: "Unauthorised", want: true },
|
|
14
|
+
{ name: "not authenticated", error: "Not authenticated", want: true },
|
|
15
|
+
{ name: "generic failure", error: "Could not update wishlist", want: false },
|
|
16
|
+
{ name: "empty string", error: "", want: false },
|
|
17
|
+
{ name: "null", error: null, want: false },
|
|
18
|
+
{ name: "undefined", error: undefined, want: false },
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
describe("isAuthRequiredError", () => {
|
|
22
|
+
for (const row of CASES) {
|
|
23
|
+
it(row.name, () => {
|
|
24
|
+
assert.equal(isAuthRequiredError(row.error), row.want)
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
})
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const LOGIN_POPUP_OPEN_EVENT = "storefront:login-popup-open"
|
|
2
|
+
|
|
3
|
+
export function isAuthRequiredError(error?: string | null): boolean {
|
|
4
|
+
const text = (error || "").toLowerCase()
|
|
5
|
+
return (
|
|
6
|
+
text.includes("login") ||
|
|
7
|
+
text.includes("unauthorized") ||
|
|
8
|
+
text.includes("unauthorised") ||
|
|
9
|
+
text.includes("not authenticated")
|
|
10
|
+
)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function requestLoginPopup(): boolean {
|
|
14
|
+
if (typeof window === "undefined") return false
|
|
15
|
+
const event = new CustomEvent(LOGIN_POPUP_OPEN_EVENT, { cancelable: true })
|
|
16
|
+
window.dispatchEvent(event)
|
|
17
|
+
return event.defaultPrevented
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function promptLoginForWishlist(fallback: () => void) {
|
|
21
|
+
if (!requestLoginPopup()) fallback()
|
|
22
|
+
}
|