@pradip1995/segment-login-template 0.5.22 → 0.5.24

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pradip1995/segment-login-template",
3
- "version": "0.5.22",
3
+ "version": "0.5.24",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -23,6 +23,7 @@
23
23
  "./profile-phone-field": "./src/profile-phone-field.tsx",
24
24
  "./profile-email-field": "./src/profile-email-field.tsx",
25
25
  "./google-auth-section": "./src/google-auth-section.tsx",
26
+ "./google-sign-in-section": "./src/google-sign-in-section.tsx",
26
27
  "./otp-input": "./src/otp-input.tsx"
27
28
  },
28
29
  "peerDependencies": {
@@ -36,8 +37,8 @@
36
37
  "dependencies": {
37
38
  "@pradip1995/commerce-auth": "^4.0.0",
38
39
  "@pradip1995/commerce-core": "^4.0.0",
39
- "@pradip1995/segment-primitives": "^0.4.12",
40
- "@pradip1995/segment-tokens": "^0.3.14"
40
+ "@pradip1995/segment-primitives": "^0.4.14",
41
+ "@pradip1995/segment-tokens": "^0.3.19"
41
42
  },
42
43
  "devDependencies": {
43
44
  "@pradip1995/plugin-sdk": "^0.2.0",
@@ -8,7 +8,11 @@ import {
8
8
  sendCustomerOTP,
9
9
  verifyCustomerOTP,
10
10
  } from "@pradip1995/commerce-core/data/customer-registration"
11
- import { transferCart } from "@pradip1995/commerce-core/data/customer"
11
+ import {
12
+ authenticateGoogleCredential as authenticateGoogleCredentialCore,
13
+ handleGoogleCallback as handleGoogleCallbackCore,
14
+ transferCart,
15
+ } from "@pradip1995/commerce-core/data/customer"
12
16
  import { getAuthHeaders, getCacheTag, setAuthToken } from "@pradip1995/commerce-core/data/cookies"
13
17
  import { sendOTP, verifyOTP } from "@pradip1995/commerce-core/data/guest"
14
18
  import { getGoogleOAuthCallbackUrl } from "@pradip1995/commerce-core/util/google-oauth"
@@ -371,6 +375,21 @@ export async function verifyContactChange(input: { otpToken: string; code: strin
371
375
  return { success: true as const }
372
376
  }
373
377
 
378
+ /** GIS One Tap / credential button → Medusa POST /auth/customer/google/callback { credential } */
379
+ export async function authenticateGoogleCredential(credential: string) {
380
+ return authenticateGoogleCredentialCore(credential)
381
+ }
382
+
383
+ /** After OAuth JWT: link or create customer (guest upgrade via customer-registration). */
384
+ export async function handleGoogleCallback(
385
+ token: string,
386
+ email?: string,
387
+ first_name?: string,
388
+ last_name?: string
389
+ ) {
390
+ return handleGoogleCallbackCore(token, email, first_name, last_name)
391
+ }
392
+
374
393
  export async function initiateGoogleAuth(countryCode?: string) {
375
394
  try {
376
395
  const backendUrl = getBaseUrl()
@@ -1,84 +1,22 @@
1
1
  "use client"
2
2
 
3
- import { useState } from "react"
4
- import {
5
- clearMedusaAuthCookies,
6
- GOOGLE_LOGIN_COUNTRY_CODE_KEY,
7
- } from "@pradip1995/commerce-auth/util/google-auth-client"
8
- import { initiateGoogleAuth } from "./auth-server"
3
+ import { GoogleSignInSection } from "./google-sign-in-section"
9
4
  import { auth } from "./account-theme"
10
5
 
11
- export default function GoogleAuthSection({ countryCode }: { countryCode: string }) {
12
- const clientId = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID?.trim()
13
- const [loading, setLoading] = useState(false)
14
- const [error, setError] = useState<string | null>(null)
15
-
16
- if (!clientId) return null
17
-
18
- const handleClick = async () => {
19
- setError(null)
20
- setLoading(true)
21
-
22
- try {
23
- localStorage.setItem(GOOGLE_LOGIN_COUNTRY_CODE_KEY, countryCode)
24
- clearMedusaAuthCookies()
25
-
26
- const result = await initiateGoogleAuth(countryCode)
27
-
28
- if (result.error) {
29
- setError(result.error)
30
- setLoading(false)
31
- return
32
- }
33
-
34
- if (result.redirectUrl) {
35
- window.location.href = result.redirectUrl
36
- return
37
- }
38
-
39
- setLoading(false)
40
- } catch {
41
- setError("Google sign-in failed. Please try again.")
42
- setLoading(false)
43
- }
44
- }
45
-
6
+ export default function GoogleAuthSection({
7
+ countryCode,
8
+ enableOneTap = true,
9
+ }: {
10
+ countryCode: string
11
+ enableOneTap?: boolean
12
+ }) {
46
13
  return (
47
- <div>
48
- {error && (
49
- <p className="mb-3 text-sm text-brand-sale" role="alert">
50
- {error}
51
- </p>
52
- )}
53
- <button
54
- type="button"
55
- onClick={handleClick}
56
- disabled={loading}
57
- className={`${auth.btnOutline} sr-google-btn`}
58
- aria-label="Continue with Google"
59
- >
60
- <svg width="18" height="18" viewBox="0 0 24 24" aria-hidden="true">
61
- <path
62
- fill="#4285F4"
63
- d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
64
- />
65
- <path
66
- fill="#34A853"
67
- d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
68
- />
69
- <path
70
- fill="#FBBC05"
71
- d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
72
- />
73
- <path
74
- fill="#EA4335"
75
- d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
76
- />
77
- </svg>
78
- <span className="sr-google-btn__label">
79
- {loading ? "Redirecting…" : "Continue with Google"}
80
- </span>
81
- </button>
82
- </div>
14
+ <GoogleSignInSection
15
+ countryCode={countryCode}
16
+ defaultCountryCode="in"
17
+ enableOneTap={enableOneTap}
18
+ credentialButtonClassName={`${auth.btnOutline} sr-google-btn`}
19
+ oauthButtonClassName={`${auth.btnOutline} sr-google-btn`}
20
+ />
83
21
  )
84
22
  }
@@ -0,0 +1,142 @@
1
+ "use client"
2
+
3
+ import { useEffect, useMemo, useState } from "react"
4
+ import { GoogleAuthProvider } from "@pradip1995/commerce-auth/components/google-oauth-provider"
5
+ import { GoogleCredentialButton } from "@pradip1995/commerce-auth/components/google-credential-button"
6
+ import { GoogleIdentityServicesScript } from "@pradip1995/commerce-auth/components/google-identity-script"
7
+ import { GoogleLoginButton } from "@pradip1995/commerce-auth/components/google-login"
8
+ import { GoogleOneTapLogin } from "@pradip1995/commerce-auth/components/google-one-tap"
9
+ import {
10
+ authenticateGoogleCredential,
11
+ handleGoogleCallback,
12
+ initiateGoogleAuth,
13
+ } from "./auth-server"
14
+
15
+ type GisUiState = "loading" | "ready" | "fallback"
16
+
17
+ const ORIGIN_REJECTED = /origin is not allowed|origin_mismatch|unregistered_origin/i
18
+
19
+ function useGsiOriginWatch(onRejected: () => void) {
20
+ useEffect(() => {
21
+ if (typeof window === "undefined" || window.__vnGsiConsolePatched) return
22
+ window.__vnGsiConsolePatched = true
23
+ const original = console.error
24
+ console.error = (...args: unknown[]) => {
25
+ const text = args.map((a) => (typeof a === "string" ? a : String(a))).join(" ")
26
+ if (text.includes("[GSI_LOGGER]") && ORIGIN_REJECTED.test(text)) {
27
+ onRejected()
28
+ console.warn(...args)
29
+ return
30
+ }
31
+ original(...args)
32
+ }
33
+ return () => {
34
+ console.error = original
35
+ window.__vnGsiConsolePatched = false
36
+ }
37
+ }, [onRejected])
38
+ }
39
+
40
+ declare global {
41
+ interface Window {
42
+ __vnGsiConsolePatched?: boolean
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Google Sign-In for Medusa storefronts: GIS "Continue as {email}" button,
48
+ * One Tap prompt, and OAuth redirect fallback (same pattern as VyaparNext deployer).
49
+ */
50
+ export function GoogleSignInSection({
51
+ countryCode,
52
+ defaultCountryCode = "in",
53
+ enableOneTap = true,
54
+ credentialButtonClassName,
55
+ oauthButtonClassName,
56
+ onError,
57
+ }: {
58
+ countryCode: string
59
+ defaultCountryCode?: string
60
+ enableOneTap?: boolean
61
+ credentialButtonClassName?: string
62
+ oauthButtonClassName?: string
63
+ onError?: (message: string) => void
64
+ }) {
65
+ const clientId = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID?.trim()
66
+ const [gis, setGis] = useState<GisUiState>("loading")
67
+ const [error, setError] = useState<string | null>(null)
68
+
69
+ const handlers = useMemo(
70
+ () => ({
71
+ authenticateCredential: authenticateGoogleCredential,
72
+ handleCustomerCallback: handleGoogleCallback,
73
+ }),
74
+ []
75
+ )
76
+
77
+ useGsiOriginWatch(() => setGis("fallback"))
78
+
79
+ useEffect(() => {
80
+ if (!clientId || gis === "fallback") return
81
+ const timer = window.setTimeout(() => {
82
+ setGis((s) => (s === "fallback" ? s : "ready"))
83
+ }, 1200)
84
+ return () => window.clearTimeout(timer)
85
+ }, [clientId, gis])
86
+
87
+ if (!clientId) return null
88
+
89
+ const showOAuth = gis !== "ready"
90
+ const showCredential = gis !== "fallback"
91
+
92
+ const reportError = (message: string) => {
93
+ setError(message)
94
+ onError?.(message)
95
+ }
96
+
97
+ return (
98
+ <GoogleAuthProvider clientId={clientId}>
99
+ <GoogleIdentityServicesScript />
100
+ {error ? (
101
+ <p className="mb-3 text-sm text-brand-sale" role="alert">
102
+ {error}
103
+ </p>
104
+ ) : null}
105
+ <div className="google-signin-section" data-gis={gis}>
106
+ {showCredential ? (
107
+ <GoogleCredentialButton
108
+ handlers={handlers}
109
+ countryCode={countryCode}
110
+ defaultCountryCode={defaultCountryCode}
111
+ className={credentialButtonClassName}
112
+ onError={(msg) => {
113
+ setGis("fallback")
114
+ reportError(msg)
115
+ }}
116
+ />
117
+ ) : null}
118
+ {showOAuth ? (
119
+ <GoogleLoginButton
120
+ countryCode={countryCode}
121
+ defaultCountryCode={defaultCountryCode}
122
+ className={oauthButtonClassName}
123
+ initiateAuth={initiateGoogleAuth}
124
+ />
125
+ ) : null}
126
+ {enableOneTap ? (
127
+ <GoogleOneTapLogin
128
+ handlers={handlers}
129
+ countryCode={countryCode}
130
+ defaultCountryCode={defaultCountryCode}
131
+ useFedcmForPrompt={true}
132
+ onError={(msg) => {
133
+ if (process.env.NODE_ENV === "development") {
134
+ console.info("[One Tap]", msg)
135
+ }
136
+ }}
137
+ />
138
+ ) : null}
139
+ </div>
140
+ </GoogleAuthProvider>
141
+ )
142
+ }