@ogcio/sag-client 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/README.md +533 -0
- package/dist/auth.d.ts +55 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +154 -0
- package/dist/auth.js.map +1 -0
- package/dist/client.d.ts +93 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +118 -0
- package/dist/client.js.map +1 -0
- package/dist/fetcher.d.ts +39 -0
- package/dist/fetcher.d.ts.map +1 -0
- package/dist/fetcher.js +141 -0
- package/dist/fetcher.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/onboarding.d.ts +68 -0
- package/dist/onboarding.d.ts.map +1 -0
- package/dist/onboarding.js +67 -0
- package/dist/onboarding.js.map +1 -0
- package/dist/react/index.d.ts +15 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +16 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/provider.d.ts +22 -0
- package/dist/react/provider.d.ts.map +1 -0
- package/dist/react/provider.js +31 -0
- package/dist/react/provider.js.map +1 -0
- package/dist/react/use-auth.d.ts +13 -0
- package/dist/react/use-auth.d.ts.map +1 -0
- package/dist/react/use-auth.js +84 -0
- package/dist/react/use-auth.js.map +1 -0
- package/dist/react/use-gateway-fetch.d.ts +29 -0
- package/dist/react/use-gateway-fetch.d.ts.map +1 -0
- package/dist/react/use-gateway-fetch.js +47 -0
- package/dist/react/use-gateway-fetch.js.map +1 -0
- package/dist/react/use-gateway-mutation.d.ts +32 -0
- package/dist/react/use-gateway-mutation.d.ts.map +1 -0
- package/dist/react/use-gateway-mutation.js +58 -0
- package/dist/react/use-gateway-mutation.js.map +1 -0
- package/dist/react/use-onboarding-guard.d.ts +91 -0
- package/dist/react/use-onboarding-guard.d.ts.map +1 -0
- package/dist/react/use-onboarding-guard.js +140 -0
- package/dist/react/use-onboarding-guard.js.map +1 -0
- package/dist/react/use-public-servant-guard.d.ts +70 -0
- package/dist/react/use-public-servant-guard.d.ts.map +1 -0
- package/dist/react/use-public-servant-guard.js +79 -0
- package/dist/react/use-public-servant-guard.js.map +1 -0
- package/dist/roles.d.ts +67 -0
- package/dist/roles.d.ts.map +1 -0
- package/dist/roles.js +93 -0
- package/dist/roles.js.map +1 -0
- package/dist/types.d.ts +131 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +28 -0
- package/dist/types.js.map +1 -0
- package/package.json +79 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Onboarding and wrong-login-method redirect helpers.
|
|
3
|
+
*
|
|
4
|
+
* These build the URLs that the reference `authorisation` package
|
|
5
|
+
* constructs for citizen onboarding and sign-in-method validation.
|
|
6
|
+
* They are framework-agnostic — the consuming app is responsible
|
|
7
|
+
* for performing the actual redirect (e.g. `window.location.href = url`).
|
|
8
|
+
*/
|
|
9
|
+
/** Path on the profile service for the citizen onboarding page. */
|
|
10
|
+
export declare const ONBOARDING_PATH = "/onboarding";
|
|
11
|
+
/** Path on the profile service that clears stale Logto session cookies
|
|
12
|
+
* and redirects to the URL given in its `redirect` query parameter.
|
|
13
|
+
* Prevents a previous user's profile-service session from being reused
|
|
14
|
+
* during onboarding. */
|
|
15
|
+
export declare const CLEAR_SESSION_PATH = "/api/clear-session";
|
|
16
|
+
/** Query parameter name used to carry the post-onboarding sign-in URL. */
|
|
17
|
+
export declare const ONBOARDING_SOURCE_PARAM = "source";
|
|
18
|
+
/** Path on the profile service for the wrong-login-method error page. */
|
|
19
|
+
export declare const WRONG_LOGIN_METHOD_PATH = "/wrong-login-method-error";
|
|
20
|
+
/** Query parameter name for the return URL on the wrong-login-method page. */
|
|
21
|
+
export declare const WRONG_LOGIN_RETURN_URL_PARAM = "returnUrl";
|
|
22
|
+
export interface OnboardingRedirectParams {
|
|
23
|
+
/** Profile service base URL (e.g. "https://profile.example.com") */
|
|
24
|
+
profileUrl: string;
|
|
25
|
+
/** Current page path in the consuming app (e.g. "/en/messages") */
|
|
26
|
+
currentPath: string;
|
|
27
|
+
/** Secure API Gateway base URL (e.g. "http://localhost:3333") */
|
|
28
|
+
gatewayUrl: string;
|
|
29
|
+
/** Application name registered in the gateway (e.g. "messaging") */
|
|
30
|
+
appName: string;
|
|
31
|
+
/**
|
|
32
|
+
* Base URL of the consuming application (e.g. "http://localhost:3000").
|
|
33
|
+
* Used to build the post-authentication `redirectUrl` so the user
|
|
34
|
+
* lands back on the correct app page after the sign-in flow completes.
|
|
35
|
+
*/
|
|
36
|
+
appBaseUrl: string;
|
|
37
|
+
/**
|
|
38
|
+
* Logto directSignIn connector to use when the user comes back
|
|
39
|
+
* from onboarding (e.g. "social:mygovid"). Optional.
|
|
40
|
+
*/
|
|
41
|
+
connector?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Build the onboarding redirect URL.
|
|
45
|
+
*
|
|
46
|
+
* The resulting URL sends the user to the profile service's onboarding
|
|
47
|
+
* page with a `source` query parameter that encodes the gateway sign-in
|
|
48
|
+
* URL the user should be sent to *after* onboarding completes. That
|
|
49
|
+
* sign-in URL in turn includes a `redirectUrl` back to the consuming
|
|
50
|
+
* app's current page.
|
|
51
|
+
*
|
|
52
|
+
* Flow: app -> profile/api/clear-session -> profile/onboarding -> gateway/auth/sign-in (GET) -> Logto -> gateway/auth/callback -> app (currentPath)
|
|
53
|
+
*/
|
|
54
|
+
export declare function buildOnboardingRedirectUrl(params: OnboardingRedirectParams): string;
|
|
55
|
+
export interface WrongLoginMethodParams {
|
|
56
|
+
/** Profile service base URL (e.g. "https://profile.example.com") */
|
|
57
|
+
profileUrl: string;
|
|
58
|
+
/** Full current URL or path to redirect back to after the user fixes their login method */
|
|
59
|
+
currentUrl: string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Build the wrong-login-method error redirect URL.
|
|
63
|
+
*
|
|
64
|
+
* Sends the user to the profile service's error page with a `returnUrl`
|
|
65
|
+
* param so they can be redirected back after switching login methods.
|
|
66
|
+
*/
|
|
67
|
+
export declare function buildWrongLoginMethodRedirect(params: WrongLoginMethodParams): string;
|
|
68
|
+
//# sourceMappingURL=onboarding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"onboarding.d.ts","sourceRoot":"","sources":["../src/onboarding.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,mEAAmE;AACnE,eAAO,MAAM,eAAe,gBAAgB,CAAA;AAE5C;;;yBAGyB;AACzB,eAAO,MAAM,kBAAkB,uBAAuB,CAAA;AAEtD,0EAA0E;AAC1E,eAAO,MAAM,uBAAuB,WAAW,CAAA;AAE/C,yEAAyE;AACzE,eAAO,MAAM,uBAAuB,8BAA8B,CAAA;AAElE,8EAA8E;AAC9E,eAAO,MAAM,4BAA4B,cAAc,CAAA;AAIvD,MAAM,WAAW,wBAAwB;IACvC,oEAAoE;IACpE,UAAU,EAAE,MAAM,CAAA;IAClB,mEAAmE;IACnE,WAAW,EAAE,MAAM,CAAA;IACnB,iEAAiE;IACjE,UAAU,EAAE,MAAM,CAAA;IAClB,oEAAoE;IACpE,OAAO,EAAE,MAAM,CAAA;IACf;;;;OAIG;IACH,UAAU,EAAE,MAAM,CAAA;IAClB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,wBAAwB,GAC/B,MAAM,CA+BR;AAID,MAAM,WAAW,sBAAsB;IACrC,oEAAoE;IACpE,UAAU,EAAE,MAAM,CAAA;IAClB,2FAA2F;IAC3F,UAAU,EAAE,MAAM,CAAA;CACnB;AAED;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,MAAM,EAAE,sBAAsB,GAC7B,MAAM,CAIR"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Onboarding and wrong-login-method redirect helpers.
|
|
3
|
+
*
|
|
4
|
+
* These build the URLs that the reference `authorisation` package
|
|
5
|
+
* constructs for citizen onboarding and sign-in-method validation.
|
|
6
|
+
* They are framework-agnostic — the consuming app is responsible
|
|
7
|
+
* for performing the actual redirect (e.g. `window.location.href = url`).
|
|
8
|
+
*/
|
|
9
|
+
// ── Constants ───────────────────────────────────────────────
|
|
10
|
+
/** Path on the profile service for the citizen onboarding page. */
|
|
11
|
+
export const ONBOARDING_PATH = "/onboarding";
|
|
12
|
+
/** Path on the profile service that clears stale Logto session cookies
|
|
13
|
+
* and redirects to the URL given in its `redirect` query parameter.
|
|
14
|
+
* Prevents a previous user's profile-service session from being reused
|
|
15
|
+
* during onboarding. */
|
|
16
|
+
export const CLEAR_SESSION_PATH = "/api/clear-session";
|
|
17
|
+
/** Query parameter name used to carry the post-onboarding sign-in URL. */
|
|
18
|
+
export const ONBOARDING_SOURCE_PARAM = "source";
|
|
19
|
+
/** Path on the profile service for the wrong-login-method error page. */
|
|
20
|
+
export const WRONG_LOGIN_METHOD_PATH = "/wrong-login-method-error";
|
|
21
|
+
/** Query parameter name for the return URL on the wrong-login-method page. */
|
|
22
|
+
export const WRONG_LOGIN_RETURN_URL_PARAM = "returnUrl";
|
|
23
|
+
/**
|
|
24
|
+
* Build the onboarding redirect URL.
|
|
25
|
+
*
|
|
26
|
+
* The resulting URL sends the user to the profile service's onboarding
|
|
27
|
+
* page with a `source` query parameter that encodes the gateway sign-in
|
|
28
|
+
* URL the user should be sent to *after* onboarding completes. That
|
|
29
|
+
* sign-in URL in turn includes a `redirectUrl` back to the consuming
|
|
30
|
+
* app's current page.
|
|
31
|
+
*
|
|
32
|
+
* Flow: app -> profile/api/clear-session -> profile/onboarding -> gateway/auth/sign-in (GET) -> Logto -> gateway/auth/callback -> app (currentPath)
|
|
33
|
+
*/
|
|
34
|
+
export function buildOnboardingRedirectUrl(params) {
|
|
35
|
+
const { profileUrl, currentPath, gatewayUrl, appName, appBaseUrl, connector, } = params;
|
|
36
|
+
// Build the sign-in URL the user will hit after onboarding.
|
|
37
|
+
// The gateway accepts GET on /auth/sign-in for this redirect-based flow.
|
|
38
|
+
const signInUrl = new URL(`${gatewayUrl}/auth/sign-in`);
|
|
39
|
+
signInUrl.searchParams.set("app", appName);
|
|
40
|
+
// redirectUrl points to the *app* (not the gateway) so the user lands
|
|
41
|
+
// back on their original page after the OIDC callback completes.
|
|
42
|
+
signInUrl.searchParams.set("redirectUrl", `${appBaseUrl}${currentPath}`);
|
|
43
|
+
if (connector) {
|
|
44
|
+
signInUrl.searchParams.set("connector", connector);
|
|
45
|
+
}
|
|
46
|
+
// Build the onboarding URL with the source param pointing back to sign-in
|
|
47
|
+
const onboardingUrl = new URL(ONBOARDING_PATH, profileUrl);
|
|
48
|
+
onboardingUrl.searchParams.set(ONBOARDING_SOURCE_PARAM, signInUrl.toString());
|
|
49
|
+
// Route through the clear-session endpoint first. This deletes any
|
|
50
|
+
// stale Logto cookies on the profile-service domain so the onboarding
|
|
51
|
+
// page re-authenticates as the *current* user (not a previous one).
|
|
52
|
+
const clearSessionUrl = new URL(CLEAR_SESSION_PATH, profileUrl);
|
|
53
|
+
clearSessionUrl.searchParams.set("redirect", onboardingUrl.toString());
|
|
54
|
+
return clearSessionUrl.toString();
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Build the wrong-login-method error redirect URL.
|
|
58
|
+
*
|
|
59
|
+
* Sends the user to the profile service's error page with a `returnUrl`
|
|
60
|
+
* param so they can be redirected back after switching login methods.
|
|
61
|
+
*/
|
|
62
|
+
export function buildWrongLoginMethodRedirect(params) {
|
|
63
|
+
const redirectUrl = new URL(WRONG_LOGIN_METHOD_PATH, params.profileUrl);
|
|
64
|
+
redirectUrl.searchParams.set(WRONG_LOGIN_RETURN_URL_PARAM, params.currentUrl);
|
|
65
|
+
return redirectUrl.toString();
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=onboarding.js.map
|
|
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7E,oEAAoE;IACpE,sEAAsE;IACtE,oEAAoE;IACpE,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,kBAAkB,EAAE,UAAU,CAAC,CAAA;IAC/D,eAAe,CAAC,YAAY,CAAC,GAAG,CAAC,UAAU,EAAE,aAAa,CAAC,QAAQ,EAAE,CAAC,CAAA;IACtE,OAAO,eAAe,CAAC,QAAQ,EAAE,CAAA;AACnC,CAAC;AAWD;;;;;GAKG;AACH,MAAM,UAAU,6BAA6B,CAC3C,MAA8B;IAE9B,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,uBAAuB,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;IACvE,WAAW,CAAC,YAAY,CAAC,GAAG,CAAC,4BAA4B,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;IAC7E,OAAO,WAAW,CAAC,QAAQ,EAAE,CAAA;AAC/B,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { buildOnboardingRedirectUrl, buildWrongLoginMethodRedirect, CLEAR_SESSION_PATH, ONBOARDING_PATH, ONBOARDING_SOURCE_PARAM, WRONG_LOGIN_METHOD_PATH, WRONG_LOGIN_RETURN_URL_PARAM, } from "../onboarding";
|
|
2
|
+
export { ALLOWED_SIGNIN_METHODS, CONNECTOR_ENTRAID, CONNECTOR_MYGOVID, DEFAULT_PUBLIC_SERVANT_ROLES, isCitizen, isCitizenOnboarded, isInactivePublicServant, isPublicServant, ORG_ROLE_ADMIN, ORG_ROLE_MEMBER, } from "../roles";
|
|
3
|
+
export type { ActorType, AuthClaims, GatewayFetchOptions, GatewayMutationOptions, MutationMethod, OrganizationInfo, SignInOptions, UseAuthResult, } from "../types";
|
|
4
|
+
export { ACTOR_TYPE_HEADER, ORGANIZATION_ID_HEADER, SagFetchError, } from "../types";
|
|
5
|
+
export { SagClientProvider, useSagClient } from "./provider";
|
|
6
|
+
export { useAuth } from "./use-auth";
|
|
7
|
+
export type { UseGatewayFetchOptions } from "./use-gateway-fetch";
|
|
8
|
+
export { useGatewayFetch } from "./use-gateway-fetch";
|
|
9
|
+
export type { UseGatewayMutationOptions } from "./use-gateway-mutation";
|
|
10
|
+
export { useGatewayMutation } from "./use-gateway-mutation";
|
|
11
|
+
export type { UseOnboardingGuardOptions, UseOnboardingGuardResult, } from "./use-onboarding-guard";
|
|
12
|
+
export { useOnboardingGuard } from "./use-onboarding-guard";
|
|
13
|
+
export type { UsePublicServantGuardOptions, UsePublicServantGuardResult, } from "./use-public-servant-guard";
|
|
14
|
+
export { usePublicServantGuard } from "./use-public-servant-guard";
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,0BAA0B,EAC1B,6BAA6B,EAC7B,kBAAkB,EAClB,eAAe,EACf,uBAAuB,EACvB,uBAAuB,EACvB,4BAA4B,GAC7B,MAAM,eAAe,CAAA;AAEtB,OAAO,EACL,sBAAsB,EACtB,iBAAiB,EACjB,iBAAiB,EACjB,4BAA4B,EAC5B,SAAS,EACT,kBAAkB,EAClB,uBAAuB,EACvB,eAAe,EACf,cAAc,EACd,eAAe,GAChB,MAAM,UAAU,CAAA;AAEjB,YAAY,EACV,SAAS,EACT,UAAU,EACV,mBAAmB,EACnB,sBAAsB,EACtB,cAAc,EACd,gBAAgB,EAChB,aAAa,EACb,aAAa,GACd,MAAM,UAAU,CAAA;AACjB,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,aAAa,GACd,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE5D,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACpC,YAAY,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAA;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,YAAY,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAA;AACvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAC3D,YAAY,EACV,yBAAyB,EACzB,wBAAwB,GACzB,MAAM,wBAAwB,CAAA;AAE/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAC3D,YAAY,EACV,4BAA4B,EAC5B,2BAA2B,GAC5B,MAAM,4BAA4B,CAAA;AAEnC,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// ── Provider ─────────────────────────────────────────────────
|
|
2
|
+
// ── Re-export onboarding helpers for convenience ────────────
|
|
3
|
+
export { buildOnboardingRedirectUrl, buildWrongLoginMethodRedirect, CLEAR_SESSION_PATH, ONBOARDING_PATH, ONBOARDING_SOURCE_PARAM, WRONG_LOGIN_METHOD_PATH, WRONG_LOGIN_RETURN_URL_PARAM, } from "../onboarding";
|
|
4
|
+
// ── Re-export role detection utilities for convenience ──────
|
|
5
|
+
export { ALLOWED_SIGNIN_METHODS, CONNECTOR_ENTRAID, CONNECTOR_MYGOVID, DEFAULT_PUBLIC_SERVANT_ROLES, isCitizen, isCitizenOnboarded, isInactivePublicServant, isPublicServant, ORG_ROLE_ADMIN, ORG_ROLE_MEMBER, } from "../roles";
|
|
6
|
+
export { ACTOR_TYPE_HEADER, ORGANIZATION_ID_HEADER, SagFetchError, } from "../types";
|
|
7
|
+
export { SagClientProvider, useSagClient } from "./provider";
|
|
8
|
+
// ── Hooks ────────────────────────────────────────────────────
|
|
9
|
+
export { useAuth } from "./use-auth";
|
|
10
|
+
export { useGatewayFetch } from "./use-gateway-fetch";
|
|
11
|
+
export { useGatewayMutation } from "./use-gateway-mutation";
|
|
12
|
+
// ── Onboarding guard ─────────────────────────────────────────
|
|
13
|
+
export { useOnboardingGuard } from "./use-onboarding-guard";
|
|
14
|
+
// ── Public servant guard ─────────────────────────────────────
|
|
15
|
+
export { usePublicServantGuard } from "./use-public-servant-guard";
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAEhE,+DAA+D;AAC/D,OAAO,EACL,0BAA0B,EAC1B,6BAA6B,EAC7B,kBAAkB,EAClB,eAAe,EACf,uBAAuB,EACvB,uBAAuB,EACvB,4BAA4B,GAC7B,MAAM,eAAe,CAAA;AACtB,+DAA+D;AAC/D,OAAO,EACL,sBAAsB,EACtB,iBAAiB,EACjB,iBAAiB,EACjB,4BAA4B,EAC5B,SAAS,EACT,kBAAkB,EAClB,uBAAuB,EACvB,eAAe,EACf,cAAc,EACd,eAAe,GAChB,MAAM,UAAU,CAAA;AAYjB,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,aAAa,GACd,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAC5D,gEAAgE;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEpC,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAErD,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAK3D,gEAAgE;AAChE,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAK3D,gEAAgE;AAChE,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import { SagClient } from "../client";
|
|
3
|
+
import type { SagClientConfig } from "../types";
|
|
4
|
+
/**
|
|
5
|
+
* Provides a `SagClient` instance to all child hooks and components.
|
|
6
|
+
* Wrap your application layout with this provider.
|
|
7
|
+
*
|
|
8
|
+
* ```tsx
|
|
9
|
+
* <SagClientProvider gatewayUrl="http://localhost:3333" appName="cars">
|
|
10
|
+
* {children}
|
|
11
|
+
* </SagClientProvider>
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function SagClientProvider({ gatewayUrl, appName, onSessionExpired, children, }: SagClientConfig & {
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
17
|
+
/**
|
|
18
|
+
* Access the `SagClient` instance from context.
|
|
19
|
+
* Must be used within a `SagClientProvider`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function useSagClient(): SagClient;
|
|
22
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/react/provider.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAiB,KAAK,SAAS,EAAuB,MAAM,OAAO,CAAA;AAC1E,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAI/C;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,EAChC,UAAU,EACV,OAAO,EACP,gBAAgB,EAChB,QAAQ,GACT,EAAE,eAAe,GAAG;IAAE,QAAQ,EAAE,SAAS,CAAA;CAAE,2CAW3C;AAED;;;GAGG;AACH,wBAAgB,YAAY,IAAI,SAAS,CAMxC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useContext, useMemo } from "react";
|
|
4
|
+
import { SagClient } from "../client";
|
|
5
|
+
const SagClientContext = createContext(null);
|
|
6
|
+
/**
|
|
7
|
+
* Provides a `SagClient` instance to all child hooks and components.
|
|
8
|
+
* Wrap your application layout with this provider.
|
|
9
|
+
*
|
|
10
|
+
* ```tsx
|
|
11
|
+
* <SagClientProvider gatewayUrl="http://localhost:3333" appName="cars">
|
|
12
|
+
* {children}
|
|
13
|
+
* </SagClientProvider>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function SagClientProvider({ gatewayUrl, appName, onSessionExpired, children, }) {
|
|
17
|
+
const client = useMemo(() => new SagClient({ gatewayUrl, appName, onSessionExpired }), [gatewayUrl, appName, onSessionExpired]);
|
|
18
|
+
return (_jsx(SagClientContext.Provider, { value: client, children: children }));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Access the `SagClient` instance from context.
|
|
22
|
+
* Must be used within a `SagClientProvider`.
|
|
23
|
+
*/
|
|
24
|
+
export function useSagClient() {
|
|
25
|
+
const client = useContext(SagClientContext);
|
|
26
|
+
if (!client) {
|
|
27
|
+
throw new Error("useSagClient must be used within a <SagClientProvider>");
|
|
28
|
+
}
|
|
29
|
+
return client;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/react/provider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAEZ,OAAO,EAAE,aAAa,EAAkB,UAAU,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAC1E,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAGrC,MAAM,gBAAgB,GAAG,aAAa,CAAmB,IAAI,CAAC,CAAA;AAE9D;;;;;;;;;GASG;AACH,MAAM,UAAU,iBAAiB,CAAC,EAChC,UAAU,EACV,OAAO,EACP,gBAAgB,EAChB,QAAQ,GACkC;IAC1C,MAAM,MAAM,GAAG,OAAO,CACpB,GAAG,EAAE,CAAC,IAAI,SAAS,CAAC,EAAE,UAAU,EAAE,OAAO,EAAE,gBAAgB,EAAE,CAAC,EAC9D,CAAC,UAAU,EAAE,OAAO,EAAE,gBAAgB,CAAC,CACxC,CAAA;IAED,OAAO,CACL,KAAC,gBAAgB,CAAC,QAAQ,IAAC,KAAK,EAAE,MAAM,YACrC,QAAQ,GACiB,CAC7B,CAAA;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY;IAC1B,MAAM,MAAM,GAAG,UAAU,CAAC,gBAAgB,CAAC,CAAA;IAC3C,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAA;IAC3E,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { UseAuthResult } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Hook for managing authentication state via the Secure API Gateway.
|
|
4
|
+
*
|
|
5
|
+
* Must be used within a `SagClientProvider`.
|
|
6
|
+
*
|
|
7
|
+
* The hook now exposes:
|
|
8
|
+
* - `claims` — raw Logto ID-token claims (roles, organizations, organization_roles)
|
|
9
|
+
* - `signIn(options?)` — connector-aware sign-in
|
|
10
|
+
* - `invalidateSession()` — clear the server session (e.g. after onboarding)
|
|
11
|
+
*/
|
|
12
|
+
export declare function useAuth(): UseAuthResult;
|
|
13
|
+
//# sourceMappingURL=use-auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-auth.d.ts","sourceRoot":"","sources":["../../src/react/use-auth.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAIV,aAAa,EACd,MAAM,UAAU,CAAA;AAGjB;;;;;;;;;GASG;AACH,wBAAgB,OAAO,IAAI,aAAa,CA2EvC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useState } from "react";
|
|
3
|
+
import { useSagClient } from "./provider";
|
|
4
|
+
/**
|
|
5
|
+
* Hook for managing authentication state via the Secure API Gateway.
|
|
6
|
+
*
|
|
7
|
+
* Must be used within a `SagClientProvider`.
|
|
8
|
+
*
|
|
9
|
+
* The hook now exposes:
|
|
10
|
+
* - `claims` — raw Logto ID-token claims (roles, organizations, organization_roles)
|
|
11
|
+
* - `signIn(options?)` — connector-aware sign-in
|
|
12
|
+
* - `invalidateSession()` — clear the server session (e.g. after onboarding)
|
|
13
|
+
*/
|
|
14
|
+
export function useAuth() {
|
|
15
|
+
const client = useSagClient();
|
|
16
|
+
const [authenticated, setAuthenticated] = useState(false);
|
|
17
|
+
const [user, setUser] = useState(undefined);
|
|
18
|
+
const [claims, setClaims] = useState(undefined);
|
|
19
|
+
const [app, setApp] = useState(undefined);
|
|
20
|
+
const [loading, setLoading] = useState(true);
|
|
21
|
+
const [warning, setWarning] = useState(undefined);
|
|
22
|
+
const [logtoAvailable, setLogtoAvailable] = useState(true);
|
|
23
|
+
const refresh = useCallback(async () => {
|
|
24
|
+
setLoading(true);
|
|
25
|
+
try {
|
|
26
|
+
const [status, health] = await Promise.all([
|
|
27
|
+
client.checkAuth(),
|
|
28
|
+
client.checkHealth(),
|
|
29
|
+
]);
|
|
30
|
+
setLogtoAvailable(health.available);
|
|
31
|
+
if (status.authenticated) {
|
|
32
|
+
setAuthenticated(true);
|
|
33
|
+
setUser(status.user);
|
|
34
|
+
setClaims(status.claims);
|
|
35
|
+
setApp(status.app);
|
|
36
|
+
setWarning(undefined);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
setAuthenticated(false);
|
|
40
|
+
setUser(undefined);
|
|
41
|
+
setClaims(undefined);
|
|
42
|
+
setApp(undefined);
|
|
43
|
+
setWarning(status.warning);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
console.error("Error checking auth", error);
|
|
48
|
+
setAuthenticated(false);
|
|
49
|
+
setUser(undefined);
|
|
50
|
+
setClaims(undefined);
|
|
51
|
+
setApp(undefined);
|
|
52
|
+
setLogtoAvailable(false);
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
setLoading(false);
|
|
56
|
+
}
|
|
57
|
+
}, [client]);
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
refresh();
|
|
60
|
+
}, [refresh]);
|
|
61
|
+
const signIn = useCallback((options) => {
|
|
62
|
+
client.signIn(options);
|
|
63
|
+
}, [client]);
|
|
64
|
+
const signOut = useCallback(() => {
|
|
65
|
+
client.signOut();
|
|
66
|
+
}, [client]);
|
|
67
|
+
const invalidateSession = useCallback(async () => {
|
|
68
|
+
await client.invalidateSession();
|
|
69
|
+
}, [client]);
|
|
70
|
+
return {
|
|
71
|
+
authenticated,
|
|
72
|
+
user,
|
|
73
|
+
claims,
|
|
74
|
+
app,
|
|
75
|
+
loading,
|
|
76
|
+
warning,
|
|
77
|
+
logtoAvailable,
|
|
78
|
+
signIn,
|
|
79
|
+
signOut,
|
|
80
|
+
invalidateSession,
|
|
81
|
+
refresh,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=use-auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-auth.js","sourceRoot":"","sources":["../../src/react/use-auth.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAOxD,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAEzC;;;;;;;;;GASG;AACH,MAAM,UAAU,OAAO;IACrB,MAAM,MAAM,GAAG,YAAY,EAAE,CAAA;IAC7B,MAAM,CAAC,aAAa,EAAE,gBAAgB,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IACzD,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAuB,SAAS,CAAC,CAAA;IACjE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,QAAQ,CAAyB,SAAS,CAAC,CAAA;IACvE,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,GAAG,QAAQ,CAAqB,SAAS,CAAC,CAAA;IAC7D,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,IAAI,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AS,CAAC,CAAA;YACjB,iBAAiB,CAAC,KAAK,CAAC,CAAA;QAC1B,CAAC;gBAAS,CAAC;YACT,UAAU,CAAC,KAAK,CAAC,CAAA;QACnB,CAAC;IACH,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAA;IAEZ,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,EAAE,CAAA;IACX,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAA;IAEb,MAAM,MAAM,GAAG,WAAW,CACxB,CAAC,OAAuB,EAAE,EAAE;QAC1B,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;IACxB,CAAC,EACD,CAAC,MAAM,CAAC,CACT,CAAA;IAED,MAAM,OAAO,GAAG,WAAW,CAAC,GAAG,EAAE;QAC/B,MAAM,CAAC,OAAO,EAAE,CAAA;IAClB,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAA;IAEZ,MAAM,iBAAiB,GAAG,WAAW,CAAC,KAAK,IAAI,EAAE;QAC/C,MAAM,MAAM,CAAC,iBAAiB,EAAE,CAAA;IAClC,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAA;IAEZ,OAAO;QACL,aAAa;QACb,IAAI;QACJ,MAAM;QACN,GAAG;QACH,OAAO;QACP,OAAO;QACP,cAAc;QACd,MAAM;QACN,OAAO;QACP,iBAAiB;QACjB,OAAO;KACR,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ActorType } from "../types";
|
|
2
|
+
/** Options for the `useGatewayFetch` hook. */
|
|
3
|
+
export interface UseGatewayFetchOptions {
|
|
4
|
+
/** Whether to fetch (defaults to `true`). */
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Override the actor type for this request.
|
|
8
|
+
* When set to `"m2m"`, the fetcher sends `X-Request-Actor-Type: m2m`
|
|
9
|
+
* so the gateway uses client_credentials instead of the user token.
|
|
10
|
+
*/
|
|
11
|
+
actorType?: ActorType;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* SWR-based hook for fetching data from the Secure API Gateway.
|
|
15
|
+
*
|
|
16
|
+
* Must be used within a `SagClientProvider`.
|
|
17
|
+
*
|
|
18
|
+
* @param path - Gateway path (e.g. "/cars", "/weather", "/notifications")
|
|
19
|
+
* @param options - Fetch options (enabled, actorType)
|
|
20
|
+
*/
|
|
21
|
+
export declare function useGatewayFetch<T>(path: string | null, options?: UseGatewayFetchOptions): {
|
|
22
|
+
data: T | undefined;
|
|
23
|
+
error: any;
|
|
24
|
+
isLoading: boolean;
|
|
25
|
+
refresh: import("swr").KeyedMutator<{
|
|
26
|
+
data: T;
|
|
27
|
+
}>;
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=use-gateway-fetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-gateway-fetch.d.ts","sourceRoot":"","sources":["../../src/react/use-gateway-fetch.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAGzC,8CAA8C;AAC9C,MAAM,WAAW,sBAAsB;IACrC,6CAA6C;IAC7C,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;;OAIG;IACH,SAAS,CAAC,EAAE,SAAS,CAAA;CACtB;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAC/B,IAAI,EAAE,MAAM,GAAG,IAAI,EACnB,OAAO,CAAC,EAAE,sBAAsB;;;;;cAiC0B,CAAC;;EAW5D"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import useSWR from "swr";
|
|
4
|
+
import { useSagClient } from "./provider";
|
|
5
|
+
/**
|
|
6
|
+
* SWR-based hook for fetching data from the Secure API Gateway.
|
|
7
|
+
*
|
|
8
|
+
* Must be used within a `SagClientProvider`.
|
|
9
|
+
*
|
|
10
|
+
* @param path - Gateway path (e.g. "/cars", "/weather", "/notifications")
|
|
11
|
+
* @param options - Fetch options (enabled, actorType)
|
|
12
|
+
*/
|
|
13
|
+
export function useGatewayFetch(path, options) {
|
|
14
|
+
var _a;
|
|
15
|
+
const client = useSagClient();
|
|
16
|
+
const enabled = (_a = options === null || options === void 0 ? void 0 : options.enabled) !== null && _a !== void 0 ? _a : true;
|
|
17
|
+
const actorType = options === null || options === void 0 ? void 0 : options.actorType;
|
|
18
|
+
const absoluteURL = useMemo(() => {
|
|
19
|
+
if (!path || !enabled)
|
|
20
|
+
return null;
|
|
21
|
+
return `${client.gatewayUrl}${path}`;
|
|
22
|
+
}, [client.gatewayUrl, path, enabled]);
|
|
23
|
+
// SWR deduplicates by key. When different actorTypes hit the same URL
|
|
24
|
+
// (e.g. user-token then M2M fallback) the key must differ so SWR
|
|
25
|
+
// makes a separate request instead of returning the cached result.
|
|
26
|
+
const swrKey = useMemo(() => {
|
|
27
|
+
if (!absoluteURL)
|
|
28
|
+
return null;
|
|
29
|
+
return actorType ? [absoluteURL, actorType] : absoluteURL;
|
|
30
|
+
}, [absoluteURL, actorType]);
|
|
31
|
+
const fetcher = useMemo(() => client.createFetcher(actorType ? { actorType } : undefined), [client, actorType]);
|
|
32
|
+
// SWR v2 passes the key as-is to the fetcher: when the key is an
|
|
33
|
+
// array like [url, actorType], the fetcher receives the array as a
|
|
34
|
+
// single argument (NOT spread). Extract the URL from either shape.
|
|
35
|
+
const wrappedFetcher = useMemo(() => {
|
|
36
|
+
const inner = fetcher;
|
|
37
|
+
return (keyOrUrl) => inner(Array.isArray(keyOrUrl) ? keyOrUrl[0] : keyOrUrl);
|
|
38
|
+
}, [fetcher]);
|
|
39
|
+
const { data, error, isLoading, mutate } = useSWR(swrKey, wrappedFetcher);
|
|
40
|
+
return {
|
|
41
|
+
data: data === null || data === void 0 ? void 0 : data.data,
|
|
42
|
+
error,
|
|
43
|
+
isLoading,
|
|
44
|
+
refresh: mutate,
|
|
45
|
+
};
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AC,aAAa,CAAI,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,EACpE,CAAC,MAAM,EAAE,SAAS,CAAC,CACpB,CAAA;IAED,iEAAiE;IACjE,mEAAmE;IACnE,oEAAoE;IACpE,MAAM,cAAc,GAAG,OAAO,CAAC,GAAG,EAAE;QAClC,MAAM,KAAK,GAAG,OAAO,CAAA;QACrB,OAAO,CAAC,QAAyC,EAAE,EAAE,CACnD,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAA;IAC3D,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAA;IAEb,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,MAAM,CAC/C,MAAM,EACN,cAAc,CACf,CAAA;IAED,OAAO;QACL,IAAI,EAAE,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,IAAI;QAChB,KAAK;QACL,SAAS;QACT,OAAO,EAAE,MAAM;KAChB,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { GatewayMutationOptions } from "../types";
|
|
2
|
+
/** Options for the `useGatewayMutation` hook. */
|
|
3
|
+
export interface UseGatewayMutationOptions extends GatewayMutationOptions {
|
|
4
|
+
/** Whether the hook is enabled (defaults to `true`). */
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Hook for performing mutations (POST, PUT, PATCH, DELETE) through
|
|
9
|
+
* the Secure API Gateway.
|
|
10
|
+
*
|
|
11
|
+
* Must be used within a `SagClientProvider`.
|
|
12
|
+
*
|
|
13
|
+
* @param path - Gateway path (e.g. "/profile/api/v1/users/123")
|
|
14
|
+
* @param options - Mutation options (method, actorType, etc.)
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* const { trigger, isLoading, error } = useGatewayMutation<Profile>(
|
|
19
|
+
* `/profile/api/v1/users/${userId}`,
|
|
20
|
+
* { method: "PATCH" },
|
|
21
|
+
* )
|
|
22
|
+
* await trigger({ primaryUserId: currentUserId })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function useGatewayMutation<TResponse, TBody = unknown>(path: string | null, options?: UseGatewayMutationOptions): {
|
|
26
|
+
trigger: (body?: TBody) => Promise<TResponse>;
|
|
27
|
+
isLoading: boolean;
|
|
28
|
+
error: Error | null;
|
|
29
|
+
data: TResponse | undefined;
|
|
30
|
+
reset: () => void;
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=use-gateway-mutation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-gateway-mutation.d.ts","sourceRoot":"","sources":["../../src/react/use-gateway-mutation.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAA;AAGtD,iDAAiD;AACjD,MAAM,WAAW,yBAA0B,SAAQ,sBAAsB;IACvE,wDAAwD;IACxD,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,KAAK,GAAG,OAAO,EAC3D,IAAI,EAAE,MAAM,GAAG,IAAI,EACnB,OAAO,CAAC,EAAE,yBAAyB;qBAoBnB,KAAK,KAAG,OAAO,CAAC,SAAS,CAAC;;;;;EAgC3C"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useMemo, useState } from "react";
|
|
3
|
+
import { useSagClient } from "./provider";
|
|
4
|
+
/**
|
|
5
|
+
* Hook for performing mutations (POST, PUT, PATCH, DELETE) through
|
|
6
|
+
* the Secure API Gateway.
|
|
7
|
+
*
|
|
8
|
+
* Must be used within a `SagClientProvider`.
|
|
9
|
+
*
|
|
10
|
+
* @param path - Gateway path (e.g. "/profile/api/v1/users/123")
|
|
11
|
+
* @param options - Mutation options (method, actorType, etc.)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const { trigger, isLoading, error } = useGatewayMutation<Profile>(
|
|
16
|
+
* `/profile/api/v1/users/${userId}`,
|
|
17
|
+
* { method: "PATCH" },
|
|
18
|
+
* )
|
|
19
|
+
* await trigger({ primaryUserId: currentUserId })
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function useGatewayMutation(path, options) {
|
|
23
|
+
var _a;
|
|
24
|
+
const client = useSagClient();
|
|
25
|
+
const method = (_a = options === null || options === void 0 ? void 0 : options.method) !== null && _a !== void 0 ? _a : "POST";
|
|
26
|
+
const actorType = options === null || options === void 0 ? void 0 : options.actorType;
|
|
27
|
+
const organizationId = options === null || options === void 0 ? void 0 : options.organizationId;
|
|
28
|
+
const fetchOptions = useMemo(() => (Object.assign(Object.assign({}, (actorType ? { actorType } : {})), (organizationId ? { organizationId } : {}))), [actorType, organizationId]);
|
|
29
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
30
|
+
const [error, setError] = useState(null);
|
|
31
|
+
const [data, setData] = useState(undefined);
|
|
32
|
+
const trigger = useCallback(async (body) => {
|
|
33
|
+
if (!path) {
|
|
34
|
+
throw new Error("useGatewayMutation: path is required");
|
|
35
|
+
}
|
|
36
|
+
setIsLoading(true);
|
|
37
|
+
setError(null);
|
|
38
|
+
try {
|
|
39
|
+
const result = await client.mutate(path, method, body, fetchOptions);
|
|
40
|
+
setData(result.data);
|
|
41
|
+
return result.data;
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
const err = e instanceof Error ? e : new Error(String(e));
|
|
45
|
+
setError(err);
|
|
46
|
+
throw err;
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
setIsLoading(false);
|
|
50
|
+
}
|
|
51
|
+
}, [client, path, method, fetchOptions]);
|
|
52
|
+
const reset = useCallback(() => {
|
|
53
|
+
setError(null);
|
|
54
|
+
setData(undefined);
|
|
55
|
+
}, []);
|
|
56
|
+
return { trigger, isLoading, error, data, reset };
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=use-gateway-mutation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-gateway-mutation.js","sourceRoot":"","sources":["../../src/react/use-gateway-mutation.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAEtD,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAQzC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,kBAAkB,CAChC,IAAmB,EACnB,OAAmC;;IAEnC,MAAM,MAAM,GAAG,YAAY,EAAE,CAAA;IAC7B,MAAM,MAAM,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,mCAAI,MAAM,CAAA;IACxC,MAAM,SAAS,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS,CAAA;IACpC,MAAM,cAAc,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,cAAc,CAAA;IAE9C,MAAM,YAAY,GAAG,OAAO,CAC1B,GAAG,EAAE,CAAC,iCACD,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,GAChC,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAC7C,EACF,CAAC,SAAS,EAAE,cAAc,CAAC,CAC5B,CAAA;IAED,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IACjD,MAAM,CAAC,KAAK,EAAE,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0 +1,91 @@
|
|
|
1
|
+
/** Options for the `useOnboardingGuard` hook. */
|
|
2
|
+
export interface UseOnboardingGuardOptions {
|
|
3
|
+
/** Profile service base URL (e.g. "https://profile.example.com"). */
|
|
4
|
+
profileUrl: string;
|
|
5
|
+
/**
|
|
6
|
+
* Base URL of the consuming application (e.g. "http://localhost:3000").
|
|
7
|
+
* Used to build the post-authentication `redirectUrl` so the user
|
|
8
|
+
* lands back on the correct app page after the sign-in flow completes.
|
|
9
|
+
*/
|
|
10
|
+
appBaseUrl: string;
|
|
11
|
+
/**
|
|
12
|
+
* Role names that identify an active public servant.
|
|
13
|
+
* Citizens are users who do NOT hold any of these organisation roles.
|
|
14
|
+
*
|
|
15
|
+
* @default DEFAULT_PUBLIC_SERVANT_ROLES — `["Organisation Admin", "Organisation Member"]`
|
|
16
|
+
* @example ["Organisation Admin", "Organisation Member"]
|
|
17
|
+
*/
|
|
18
|
+
publicServantRoles?: string[];
|
|
19
|
+
/**
|
|
20
|
+
* Logto `directSignIn` connector to use when the user comes back
|
|
21
|
+
* from onboarding (e.g. "social:mygovid"). Optional.
|
|
22
|
+
*/
|
|
23
|
+
connector?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Time window (ms) after a redirect during which the guard will
|
|
26
|
+
* not redirect again. Prevents infinite loops when the onboarding
|
|
27
|
+
* redirect chain brings the user back still not-onboarded.
|
|
28
|
+
*
|
|
29
|
+
* @default 30000 (30 seconds)
|
|
30
|
+
*/
|
|
31
|
+
debounceMs?: number;
|
|
32
|
+
}
|
|
33
|
+
/** Return value of `useOnboardingGuard`. */
|
|
34
|
+
export interface UseOnboardingGuardResult {
|
|
35
|
+
/**
|
|
36
|
+
* `true` once the onboarding check has resolved — meaning the user
|
|
37
|
+
* is onboarded, is not a citizen, is unauthenticated, or the
|
|
38
|
+
* debounce window is active.
|
|
39
|
+
*
|
|
40
|
+
* `false` while auth is loading or the user is being redirected to
|
|
41
|
+
* the onboarding page.
|
|
42
|
+
*
|
|
43
|
+
* **Do not render data-fetching children until this is `true`** to
|
|
44
|
+
* prevent `useGatewayFetch` requests from racing with session
|
|
45
|
+
* invalidation.
|
|
46
|
+
*/
|
|
47
|
+
resolved: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Onboarding guard hook for citizen-facing applications.
|
|
51
|
+
*
|
|
52
|
+
* Must be used within a `SagClientProvider`. Internally calls
|
|
53
|
+
* `useAuth()` for auth state and `useSagClient()` for the gateway
|
|
54
|
+
* URL and app name.
|
|
55
|
+
*
|
|
56
|
+
* **Behaviour:**
|
|
57
|
+
*
|
|
58
|
+
* 1. If the user is not a citizen → resolved (let them through).
|
|
59
|
+
* 2. If the user signed in with a wrong method → redirect to the
|
|
60
|
+
* profile service's error page (checked for ALL citizens,
|
|
61
|
+
* including onboarded ones).
|
|
62
|
+
* 3. If the user is onboarded (`isCitizenOnboarded`) → resolved.
|
|
63
|
+
* 4. If a redirect happened less than `debounceMs` ago → resolved
|
|
64
|
+
* (prevents infinite loops).
|
|
65
|
+
* 5. If the user is a citizen who has not completed onboarding →
|
|
66
|
+
* invalidate the server session and redirect to the profile
|
|
67
|
+
* service's onboarding page.
|
|
68
|
+
*
|
|
69
|
+
* While the check is pending or a redirect is in flight, `resolved`
|
|
70
|
+
* remains `false`. The consuming component should gate children
|
|
71
|
+
* behind `resolved` to prevent `useGatewayFetch` from firing.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```tsx
|
|
75
|
+
* function Shell({ children }) {
|
|
76
|
+
* // publicServantRoles defaults to DEFAULT_PUBLIC_SERVANT_ROLES
|
|
77
|
+
* // (["Organisation Admin", "Organisation Member"])
|
|
78
|
+
* const { resolved } = useOnboardingGuard({
|
|
79
|
+
* profileUrl: "http://localhost:3001",
|
|
80
|
+
* appBaseUrl: "http://localhost:3000",
|
|
81
|
+
* connector: CONNECTOR_MYGOVID,
|
|
82
|
+
* })
|
|
83
|
+
* const { user, signIn, signOut } = useAuth()
|
|
84
|
+
*
|
|
85
|
+
* if (!resolved) return <Loading />
|
|
86
|
+
* return user ? <App>{children}</App> : <SignInButton />
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function useOnboardingGuard(options: UseOnboardingGuardOptions): UseOnboardingGuardResult;
|
|
91
|
+
//# sourceMappingURL=use-onboarding-guard.d.ts.map
|