11/* eslint-disable @silverhand/fp/no-mutation */
2- import { condString } from '@silverhand/essentials' ;
2+ import { condString , type Optional } from '@silverhand/essentials' ;
33import { useCallback , useEffect , useMemo , useState } from 'react' ;
44
55import { createAuthStatusChecker } from './auth-status' ;
@@ -13,31 +13,55 @@ import {
1313 initialAuthCheckDelay ,
1414 authCheckFallbackTimeout ,
1515} from './constants' ;
16+ import { verifyGoogleOneTapCredential } from './credential-verifier' ;
1617import { createDebugLogger , type DebugLogger } from './debug-logger' ;
1718import type { GoogleOneTapConfig } from './google-one-tap' ;
18- import type { SiteConfig } from './types' ;
19+ import type {
20+ SiteConfig ,
21+ GoogleOneTapCredentialResponse ,
22+ GoogleOneTapVerifyResponse ,
23+ } from './types' ;
1924
2025export function useDebugLogger ( siteConfig : SiteConfig ) : DebugLogger {
2126 const isDebugMode = Boolean ( siteConfig . customFields ?. isDebuggingEnabled ) ;
2227
2328 return useMemo ( ( ) => createDebugLogger ( isDebugMode ) , [ isDebugMode ] ) ;
2429}
2530
26- export function useApiBaseUrl ( siteConfig : SiteConfig ) : string {
31+ export function useApiBaseUrl ( siteConfig : SiteConfig ) : {
32+ baseUrl : string ;
33+ authUrl : string ;
34+ redirectUri : string ;
35+ logtoAdminConsoleUrl ?: string ;
36+ } {
2737 return useMemo ( ( ) => {
2838 const logtoApiBaseUrl = siteConfig . customFields ?. logtoApiBaseUrl ;
29- return typeof logtoApiBaseUrl === 'string'
30- ? logtoApiBaseUrl
31- : siteConfig . customFields ?. isDevFeatureEnabled
32- ? defaultApiBaseDevUrl
33- : defaultApiBaseProdUrl ;
34- } , [ siteConfig . customFields ?. logtoApiBaseUrl , siteConfig . customFields ?. isDevFeatureEnabled ] ) ;
39+ const baseUrl =
40+ typeof logtoApiBaseUrl === 'string'
41+ ? logtoApiBaseUrl
42+ : siteConfig . customFields ?. isDevFeatureEnabled
43+ ? defaultApiBaseDevUrl
44+ : defaultApiBaseProdUrl ;
45+ const authUrl = `${ baseUrl } /oidc/auth` ;
46+ const redirectUri = `${ typeof logtoApiBaseUrl === 'string' ? `${ logtoApiBaseUrl } /${ new URL ( logtoApiBaseUrl ) . hostname === 'localhost' ? 'demo-app' : 'callback' } ` : `${ defaultApiBaseProdUrl } /callback` } ` ;
47+ const logtoAdminConsoleUrl = siteConfig . customFields ?. logtoAdminConsoleUrl ;
48+ return {
49+ baseUrl,
50+ authUrl,
51+ redirectUri,
52+ logtoAdminConsoleUrl,
53+ } ;
54+ } , [
55+ siteConfig . customFields ?. logtoApiBaseUrl ,
56+ siteConfig . customFields ?. isDevFeatureEnabled ,
57+ siteConfig . customFields ?. logtoAdminConsoleUrl ,
58+ ] ) ;
3559}
3660
3761export function useGoogleOneTapConfig (
3862 apiBaseUrl : string ,
3963 debugLogger : DebugLogger
40- ) : GoogleOneTapConfig | undefined {
64+ ) : Optional < GoogleOneTapConfig > {
4165 const [ config , setConfig ] = useState < GoogleOneTapConfig > ( ) ;
4266
4367 useEffect ( ( ) => {
@@ -65,10 +89,24 @@ export function useGoogleOneTapConfig(
6589 return config ;
6690}
6791
92+ export function useGoogleOneTapVerify (
93+ apiBaseUrl : string ,
94+ debugLogger : DebugLogger
95+ ) : ( response : GoogleOneTapCredentialResponse ) => Promise < Optional < GoogleOneTapVerifyResponse > > {
96+ return useCallback (
97+ async ( response : GoogleOneTapCredentialResponse ) => {
98+ return verifyGoogleOneTapCredential ( { apiBaseUrl, debugLogger } , response ) ;
99+ } ,
100+ [ apiBaseUrl , debugLogger ]
101+ ) ;
102+ }
103+
68104export type AuthStatusResult = {
69105 authStatus ?: boolean ;
70106 authCheckError ?: string ;
71107 checkAdminTokenStatus : ( ) => Promise < boolean > ;
108+ checkStorageAccess : ( ) => Promise < boolean > ;
109+ requestStorageAccess : ( ) => Promise < boolean > ;
72110} ;
73111
74112export function useAuthStatus ( siteConfig : SiteConfig , debugLogger : DebugLogger ) : AuthStatusResult {
@@ -81,18 +119,19 @@ export function useAuthStatus(siteConfig: SiteConfig, debugLogger: DebugLogger):
81119 const isDebugMode = Boolean ( siteConfig . customFields ?. isDebuggingEnabled ) ;
82120 const isIframeVisible = Boolean ( siteConfig . customFields ?. isIframeVisible ) ;
83121
84- const { checkAdminTokenStatus, authStatusCheckerHost } = useMemo (
85- ( ) =>
86- createAuthStatusChecker ( {
87- logtoAdminConsoleUrl :
88- typeof logtoAdminConsoleUrl === 'string' ? logtoAdminConsoleUrl : undefined ,
89- enableAuthStatusCheck : Boolean ( enableAuthStatusCheck ) ,
90- isDebugMode,
91- isIframeVisible,
92- debugLogger,
93- } ) ,
94- [ logtoAdminConsoleUrl , enableAuthStatusCheck , isDebugMode , isIframeVisible , debugLogger ]
95- ) ;
122+ const { checkAdminTokenStatus, checkStorageAccess, requestStorageAccess, authStatusCheckerHost } =
123+ useMemo (
124+ ( ) =>
125+ createAuthStatusChecker ( {
126+ logtoAdminConsoleUrl :
127+ typeof logtoAdminConsoleUrl === 'string' ? logtoAdminConsoleUrl : undefined ,
128+ enableAuthStatusCheck : Boolean ( enableAuthStatusCheck ) ,
129+ isDebugMode,
130+ isIframeVisible,
131+ debugLogger,
132+ } ) ,
133+ [ logtoAdminConsoleUrl , enableAuthStatusCheck , isDebugMode , isIframeVisible , debugLogger ]
134+ ) ;
96135
97136 const performAuthCheckWithRetry = useCallback (
98137 async ( retryCount = 0 ) : Promise < void > => {
@@ -185,9 +224,11 @@ export function useAuthStatus(siteConfig: SiteConfig, debugLogger: DebugLogger):
185224 authStatus,
186225 authCheckError,
187226 checkAdminTokenStatus,
227+ checkStorageAccess,
228+ requestStorageAccess,
188229 } ;
189230 }
190- } , [ authStatus , authCheckError , checkAdminTokenStatus ] ) ;
231+ } , [ authStatus , authCheckError , checkAdminTokenStatus , checkStorageAccess , requestStorageAccess ] ) ;
191232
192233 // Debug logging
193234 useEffect ( ( ) => {
@@ -219,6 +260,8 @@ export function useAuthStatus(siteConfig: SiteConfig, debugLogger: DebugLogger):
219260 authStatus,
220261 authCheckError,
221262 checkAdminTokenStatus,
263+ checkStorageAccess,
264+ requestStorageAccess,
222265 } ;
223266}
224267/* eslint-enable @silverhand/fp/no-mutation */
0 commit comments