@pradip1995/segment-facebook-login 0.2.0
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 +37 -0
- package/src/index.ts +2 -0
- package/src/manifest.ts +11 -0
- package/src/segment.tsx +96 -0
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pradip1995/segment-facebook-login",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"files": [
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/index.ts",
|
|
14
|
+
"./manifest": "./src/manifest.ts"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@pradip1995/commerce-core": "^4.0.0",
|
|
18
|
+
"@pradip1995/plugin-sdk": "^0.2.0",
|
|
19
|
+
"react": ">=19",
|
|
20
|
+
"react-dom": ">=19",
|
|
21
|
+
"next": ">=15"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@pradip1995/segment-primitives": "0.3.0",
|
|
25
|
+
"@pradip1995/segment-tokens": "0.3.2"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@pradip1995/plugin-sdk": "^0.2.0",
|
|
29
|
+
"@types/react": "^19",
|
|
30
|
+
"react": "19.0.3",
|
|
31
|
+
"typescript": "^5.7.2"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"typecheck": "tsc --noEmit",
|
|
35
|
+
"lint": "tsc --noEmit"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/index.ts
ADDED
package/src/manifest.ts
ADDED
package/src/segment.tsx
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useCallback, useState } from "react"
|
|
4
|
+
import type { HttpTypes } from "@medusajs/types"
|
|
5
|
+
import { loaderBus } from "@pradip1995/commerce-core/util/loader-bus"
|
|
6
|
+
|
|
7
|
+
function resolveRedirectUri(): string {
|
|
8
|
+
const envBase =
|
|
9
|
+
process.env.NEXT_PUBLIC_BASE_URL ||
|
|
10
|
+
process.env.NEXT_PUBLIC_MEDUSA_STOREFRONT_URL ||
|
|
11
|
+
(typeof window !== "undefined" ? window.location.origin : "")
|
|
12
|
+
|
|
13
|
+
const base = envBase.replace(/\/$/, "")
|
|
14
|
+
return `${base}/auth/customer/facebook/callback`
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function initiateFacebookLogin(): Promise<string> {
|
|
18
|
+
const baseUrl =
|
|
19
|
+
process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL || process.env.MEDUSA_BACKEND_URL || "http://localhost:9000"
|
|
20
|
+
const publishableKey = process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY
|
|
21
|
+
|
|
22
|
+
loaderBus.begin()
|
|
23
|
+
try {
|
|
24
|
+
const response = await fetch(`${baseUrl}/auth/customer/facebook`, {
|
|
25
|
+
method: "POST",
|
|
26
|
+
headers: {
|
|
27
|
+
"Content-Type": "application/json",
|
|
28
|
+
...(publishableKey ? { "x-publishable-api-key": publishableKey } : {}),
|
|
29
|
+
},
|
|
30
|
+
body: JSON.stringify({ redirect_uri: resolveRedirectUri() }),
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
if (!response.ok) {
|
|
34
|
+
throw new Error(`Facebook auth failed with status ${response.status}`)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const result = await response.json()
|
|
38
|
+
const redirectUrl = typeof result === "string" ? result : result.location
|
|
39
|
+
if (!redirectUrl) {
|
|
40
|
+
throw new Error("No redirect location returned from authentication provider")
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const url = new URL(redirectUrl)
|
|
44
|
+
url.searchParams.set("auth_type", "reauthenticate")
|
|
45
|
+
return url.toString()
|
|
46
|
+
} finally {
|
|
47
|
+
loaderBus.end()
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function FacebookIcon() {
|
|
52
|
+
return (
|
|
53
|
+
<svg viewBox="0 0 24 24" fill="none" aria-hidden className="h-5 w-5 shrink-0">
|
|
54
|
+
<path
|
|
55
|
+
d="M24 12.073C24 5.405 18.627 0 12 0S0 5.405 0 12.073C0 18.1 4.388 23.094 10.125 24v-8.437H7.078v-3.49h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.49h-2.796V24C19.612 23.094 24 18.1 24 12.073Z"
|
|
56
|
+
fill="#1877F2"
|
|
57
|
+
/>
|
|
58
|
+
</svg>
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default function FacebookLogin({
|
|
63
|
+
customer,
|
|
64
|
+
}: {
|
|
65
|
+
customer?: HttpTypes.StoreCustomer | null
|
|
66
|
+
}) {
|
|
67
|
+
const [isLoading, setIsLoading] = useState(false)
|
|
68
|
+
|
|
69
|
+
const loginWithFacebook = useCallback(async () => {
|
|
70
|
+
if (isLoading) return
|
|
71
|
+
setIsLoading(true)
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const redirectUrl = await initiateFacebookLogin()
|
|
75
|
+
window.location.href = redirectUrl
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error("Facebook authentication failed", error)
|
|
78
|
+
setIsLoading(false)
|
|
79
|
+
}
|
|
80
|
+
}, [isLoading])
|
|
81
|
+
|
|
82
|
+
if (customer) return null
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<button
|
|
86
|
+
type="button"
|
|
87
|
+
onClick={loginWithFacebook}
|
|
88
|
+
disabled={isLoading}
|
|
89
|
+
data-testid="facebook-login-button"
|
|
90
|
+
className="btn-outline w-full flex items-center justify-center gap-3 py-3 px-4"
|
|
91
|
+
>
|
|
92
|
+
<FacebookIcon />
|
|
93
|
+
<span>{isLoading ? "Redirecting…" : "Continue with Facebook"}</span>
|
|
94
|
+
</button>
|
|
95
|
+
)
|
|
96
|
+
}
|