@pradip1995/segment-login-template 0.5.22 → 0.5.25

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.25",
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,67 @@
1
+ "use client"
2
+
3
+ import { useEffect, useState, type ReactNode } from "react"
4
+ import { GoogleAuthProvider } from "@pradip1995/commerce-auth/components/google-oauth-provider"
5
+ import { GoogleLoginButton } from "@pradip1995/commerce-auth/components/google-login"
6
+ import { initiateGoogleAuth } from "./auth-server"
7
+
8
+ /** commerce-auth GoogleAuthProvider wraps GoogleOAuthProvider only after mount; defer UI until then. */
9
+ function DeferGoogleOAuthUi({ children }: { children: ReactNode }) {
10
+ const [ready, setReady] = useState(false)
11
+ useEffect(() => {
12
+ setReady(true)
13
+ }, [])
14
+ if (!ready) return null
15
+ return <>{children}</>
16
+ }
17
+
18
+ /**
19
+ * Google Sign-In uses Medusa's authorization-code redirect.
20
+ * The GIS credential button returns an ID token, which @medusajs/auth-google rejects.
21
+ */
22
+ export function GoogleSignInSection({
23
+ countryCode,
24
+ defaultCountryCode = "in",
25
+ credentialButtonClassName,
26
+ oauthButtonClassName,
27
+ onError,
28
+ }: {
29
+ countryCode: string
30
+ defaultCountryCode?: string
31
+ enableOneTap?: boolean
32
+ credentialButtonClassName?: string
33
+ oauthButtonClassName?: string
34
+ onError?: (message: string) => void
35
+ }) {
36
+ const clientId = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID?.trim()
37
+ const [error, setError] = useState<string | null>(null)
38
+
39
+ if (!clientId) return null
40
+
41
+ return (
42
+ <GoogleAuthProvider clientId={clientId}>
43
+ <DeferGoogleOAuthUi>
44
+ {error ? (
45
+ <p className="mb-3 text-sm text-brand-sale" role="alert">
46
+ {error}
47
+ </p>
48
+ ) : null}
49
+ <div className="google-signin-section" data-gis="oauth">
50
+ <GoogleLoginButton
51
+ countryCode={countryCode}
52
+ defaultCountryCode={defaultCountryCode}
53
+ className={oauthButtonClassName || credentialButtonClassName}
54
+ initiateAuth={async (countryCode?: string) => {
55
+ const result = await initiateGoogleAuth(countryCode)
56
+ if (result.error) {
57
+ setError(result.error)
58
+ onError?.(result.error)
59
+ }
60
+ return result
61
+ }}
62
+ />
63
+ </div>
64
+ </DeferGoogleOAuthUi>
65
+ </GoogleAuthProvider>
66
+ )
67
+ }