@gahojin-inc/amplify-auth-hooks 2026.4.0 → 2026.5.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/dist/authenticator/defaultAuthHubHandler.d.ts +3 -0
- package/dist/{defaultAuthHubHandler-rX4tAlCj.js → authenticator/defaultAuthHubHandler.js} +4 -4
- package/dist/authenticator/defaultAuthHubHandler.js.map +1 -0
- package/dist/authenticator/index.d.ts +1 -0
- package/dist/authenticator.js +1 -1
- package/dist/context/AuthenticatorContext.d.ts +7 -0
- package/dist/context/AuthenticatorContext.js +7 -0
- package/dist/context/AuthenticatorContext.js.map +1 -0
- package/dist/context/AuthenticatorProvider.d.ts +7 -0
- package/dist/context/AuthenticatorProvider.js +28 -0
- package/dist/context/AuthenticatorProvider.js.map +1 -0
- package/dist/context/index.d.ts +2 -0
- package/dist/hooks/facade.d.ts +6 -0
- package/dist/hooks/facade.js +76 -0
- package/dist/hooks/facade.js.map +1 -0
- package/dist/hooks/useAuth.d.ts +12 -0
- package/dist/hooks/useAuth.js +62 -0
- package/dist/hooks/useAuth.js.map +1 -0
- package/dist/hooks/useAuthenticator.d.ts +2 -0
- package/dist/hooks/useAuthenticator.js +21 -0
- package/dist/hooks/useAuthenticator.js.map +1 -0
- package/dist/hooks/utils.d.ts +87 -0
- package/dist/hooks/utils.js +52 -0
- package/dist/hooks/utils.js.map +1 -0
- package/dist/index.d.ts +7 -66
- package/dist/index.js +7 -313
- package/dist/machines/actions.d.ts +22 -0
- package/dist/machines/actions.js +58 -0
- package/dist/machines/actions.js.map +1 -0
- package/dist/machines/defaultHandlers.d.ts +2 -0
- package/dist/machines/defaultHandlers.js +22 -0
- package/dist/machines/defaultHandlers.js.map +1 -0
- package/dist/machines/forgotPassword/actor.d.ts +1 -0
- package/dist/machines/forgotPassword/actor.js +136 -0
- package/dist/machines/forgotPassword/actor.js.map +1 -0
- package/dist/machines/guards.d.ts +23 -0
- package/dist/machines/guards.js +78 -0
- package/dist/machines/guards.js.map +1 -0
- package/dist/machines/index.d.ts +6 -0
- package/dist/machines/signIn/actor.d.ts +1 -0
- package/dist/machines/signIn/actor.js +264 -0
- package/dist/machines/signIn/actor.js.map +1 -0
- package/dist/machines/signOut/actor.d.ts +1 -0
- package/dist/machines/signOut/actor.js +29 -0
- package/dist/machines/signOut/actor.js.map +1 -0
- package/dist/machines/signUp/actor.d.ts +1 -0
- package/dist/machines/signUp/actor.js +250 -0
- package/dist/machines/signUp/actor.js.map +1 -0
- package/dist/machines/verifyUserAttributes/actor.d.ts +1 -0
- package/dist/machines/verifyUserAttributes/actor.js +119 -0
- package/dist/machines/verifyUserAttributes/actor.js.map +1 -0
- package/dist/machines.js +223 -1
- package/dist/machines.js.map +1 -0
- package/dist/types/authenticator.d.ts +11 -0
- package/dist/types/hooks.d.ts +30 -0
- package/dist/types/machines.d.ts +84 -0
- package/dist/types/user.d.ts +14 -0
- package/dist/types/user.js +12 -0
- package/dist/types/user.js.map +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +8 -0
- package/dist/utils.js.map +1 -0
- package/package.json +8 -8
- package/dist/authenticator.d.ts +0 -7
- package/dist/defaultAuthHubHandler-rX4tAlCj.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/machines-BRfb3dTb.js +0 -1131
- package/dist/machines-BRfb3dTb.js.map +0 -1
- package/dist/machines.d.ts +0 -2
- package/dist/types-BQSOxe-R.d.ts +0 -116
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { AuthActor, AuthMachineHubHandler, StopListenerCallback } from '../types/authenticator.js';
|
|
2
|
+
export declare const defaultAuthHubHandler: AuthMachineHubHandler;
|
|
3
|
+
export declare const listenToAuthHub: (actor: AuthActor, handler?: AuthMachineHubHandler) => StopListenerCallback;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Hub } from "@aws-amplify/core";
|
|
2
2
|
import { AmplifyErrorCode } from "@aws-amplify/core/internals/utils";
|
|
3
3
|
//#region src/authenticator/defaultAuthHubHandler.ts
|
|
4
|
-
|
|
4
|
+
var defaultAuthHubHandler = ({ payload }, actor, options) => {
|
|
5
5
|
const { data, event } = payload;
|
|
6
6
|
const { send } = actor;
|
|
7
7
|
const { onSignIn, onSignOut } = options ?? {};
|
|
@@ -23,11 +23,11 @@ const defaultAuthHubHandler = ({ payload }, actor, options) => {
|
|
|
23
23
|
default: break;
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
|
-
|
|
26
|
+
var listenToAuthHub = (actor, handler = defaultAuthHubHandler) => {
|
|
27
27
|
const eventHandler = (data) => handler(data, actor);
|
|
28
28
|
return Hub.listen("auth", eventHandler, "authenticator-hub-handler");
|
|
29
29
|
};
|
|
30
30
|
//#endregion
|
|
31
|
-
export {
|
|
31
|
+
export { defaultAuthHubHandler, listenToAuthHub };
|
|
32
32
|
|
|
33
|
-
//# sourceMappingURL=defaultAuthHubHandler
|
|
33
|
+
//# sourceMappingURL=defaultAuthHubHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaultAuthHubHandler.js","names":[],"sources":["../../src/authenticator/defaultAuthHubHandler.ts"],"sourcesContent":["/*\n * Copyright 2017 - 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.\n * (C) 2025 GAHOJIN, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n */\n\nimport { Hub } from '@aws-amplify/core'\nimport { AmplifyErrorCode } from '@aws-amplify/core/internals/utils'\nimport type { AuthActor, AuthMachineHubHandler, StopListenerCallback } from '~/types/authenticator.js'\n\nexport const defaultAuthHubHandler: AuthMachineHubHandler = ({ payload }, actor, options) => {\n const { data, event } = payload\n const { send } = actor\n const { onSignIn, onSignOut } = options ?? {}\n\n switch (event) {\n case 'signedIn': {\n if (typeof onSignIn === 'function') {\n onSignIn(payload)\n }\n break\n }\n case 'signInWithRedirect': {\n send({ type: 'SIGN_IN_WITH_REDIRECT' })\n break\n }\n case 'signedOut': {\n if (typeof onSignOut === 'function') {\n onSignOut()\n }\n send({ type: 'SIGN_OUT' })\n break\n }\n case 'tokenRefresh_failure': {\n if (data?.error?.name === AmplifyErrorCode.NetworkError) {\n return\n }\n send({ type: 'SIGN_OUT' })\n break\n }\n default: {\n break\n }\n }\n}\n\nexport const listenToAuthHub = (actor: AuthActor, handler: AuthMachineHubHandler = defaultAuthHubHandler): StopListenerCallback => {\n const eventHandler: Parameters<typeof Hub.listen>[1] = (data) => handler(data, actor)\n return Hub.listen('auth', eventHandler, 'authenticator-hub-handler')\n}\n"],"mappings":";;;AAeA,IAAa,yBAAgD,EAAE,WAAW,OAAO,YAAY;CAC3F,MAAM,EAAE,MAAM,UAAU;CACxB,MAAM,EAAE,SAAS;CACjB,MAAM,EAAE,UAAU,cAAc,WAAW,EAAE;AAE7C,SAAQ,OAAR;EACE,KAAK;AACH,OAAI,OAAO,aAAa,WACtB,UAAS,QAAQ;AAEnB;EAEF,KAAK;AACH,QAAK,EAAE,MAAM,yBAAyB,CAAC;AACvC;EAEF,KAAK;AACH,OAAI,OAAO,cAAc,WACvB,YAAW;AAEb,QAAK,EAAE,MAAM,YAAY,CAAC;AAC1B;EAEF,KAAK;AACH,OAAI,MAAM,OAAO,SAAS,iBAAiB,aACzC;AAEF,QAAK,EAAE,MAAM,YAAY,CAAC;AAC1B;EAEF,QACE;;;AAKN,IAAa,mBAAmB,OAAkB,UAAiC,0BAAgD;CACjI,MAAM,gBAAkD,SAAS,QAAQ,MAAM,MAAM;AACrF,QAAO,IAAI,OAAO,QAAQ,cAAc,4BAA4B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { listenToAuthHub } from './defaultAuthHubHandler.js';
|
package/dist/authenticator.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { listenToAuthHub } from "./authenticator/defaultAuthHubHandler.js";
|
|
2
2
|
export { listenToAuthHub };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthenticatorContext.js","names":[],"sources":["../../src/context/AuthenticatorContext.ts"],"sourcesContent":["import { type Context, createContext } from 'react'\nimport type { AuthActor } from '~/types/authenticator.js'\n\ntype AuthenticatorContextType = {\n actor: AuthActor\n}\n\nexport const AuthenticatorContext: Context<AuthenticatorContextType | null> = createContext<AuthenticatorContextType | null>(null)\n"],"mappings":";;AAOA,IAAa,uBAAiE,cAA+C,KAAK"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { JSX, PropsWithChildren } from 'react';
|
|
2
|
+
import { AuthenticatorMachineOptions } from '../machines/index.js';
|
|
3
|
+
type Props = PropsWithChildren & {
|
|
4
|
+
options?: AuthenticatorMachineOptions;
|
|
5
|
+
};
|
|
6
|
+
declare const _default: ({ options, children }: Props) => JSX.Element;
|
|
7
|
+
export default _default;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AuthenticatorContext } from "./AuthenticatorContext.js";
|
|
2
|
+
import { defaultAuthHubHandler, listenToAuthHub } from "../authenticator/defaultAuthHubHandler.js";
|
|
3
|
+
import { createAuthenticatorMachine } from "../machines.js";
|
|
4
|
+
import { useContext, useEffect, useMemo } from "react";
|
|
5
|
+
import { useActor } from "@xstate/react";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
//#region src/context/AuthenticatorProvider.tsx
|
|
8
|
+
var createHubHandler = (options) => {
|
|
9
|
+
return (data, service) => defaultAuthHubHandler(data, service, options);
|
|
10
|
+
};
|
|
11
|
+
var AuthenticatorProvider_default = ({ options, children }) => {
|
|
12
|
+
const machine = useMemo(() => createAuthenticatorMachine(options), [options]);
|
|
13
|
+
const parentProvider = useContext(AuthenticatorContext);
|
|
14
|
+
const [, , actor] = useActor(machine);
|
|
15
|
+
const value = useMemo(() => parentProvider ?? { actor }, [parentProvider, actor]);
|
|
16
|
+
const { actor: activeActor } = value;
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
return listenToAuthHub(activeActor, createHubHandler(options));
|
|
19
|
+
}, [activeActor, options]);
|
|
20
|
+
return jsx(AuthenticatorContext.Provider, {
|
|
21
|
+
value,
|
|
22
|
+
children
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export { AuthenticatorProvider_default as default };
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=AuthenticatorProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthenticatorProvider.js","names":[],"sources":["../../src/context/AuthenticatorProvider.tsx"],"sourcesContent":["import { useActor } from '@xstate/react'\nimport { type JSX, type PropsWithChildren, useContext, useEffect, useMemo } from 'react'\nimport { defaultAuthHubHandler, listenToAuthHub } from '~/authenticator/defaultAuthHubHandler.js'\nimport { AuthenticatorContext } from '~/context/AuthenticatorContext.js'\nimport { type AuthenticatorMachineOptions, createAuthenticatorMachine } from '~/machines/index.js'\nimport type { AuthMachineHubHandler, AuthMachineHubHandlerOptions } from '~/types/authenticator.js'\n\nconst createHubHandler = (options?: AuthMachineHubHandlerOptions): AuthMachineHubHandler => {\n return (data, service) => defaultAuthHubHandler(data, service, options)\n}\n\ntype Props = PropsWithChildren & {\n options?: AuthenticatorMachineOptions\n}\n\nexport default ({ options, children }: Props): JSX.Element => {\n const machine = useMemo(() => createAuthenticatorMachine(options), [options])\n\n // 階層構造にAuthenticatorContextを使用している場合、親Contextの認証状態を継承する\n const parentProvider = useContext(AuthenticatorContext)\n const [, , actor] = useActor(machine)\n const value = useMemo(() => parentProvider ?? { actor }, [parentProvider, actor])\n\n const { actor: activeActor } = value\n\n useEffect(() => {\n return listenToAuthHub(activeActor, createHubHandler(options))\n }, [activeActor, options])\n\n return <AuthenticatorContext.Provider value={value}>{children}</AuthenticatorContext.Provider>\n}\n"],"mappings":";;;;;;;AAOA,IAAM,oBAAoB,YAAkE;AAC1F,SAAQ,MAAM,YAAY,sBAAsB,MAAM,SAAS,QAAQ;;AAOzE,IAAA,iCAAgB,EAAE,SAAS,eAAmC;CAC5D,MAAM,UAAU,cAAc,2BAA2B,QAAQ,EAAE,CAAC,QAAQ,CAAC;CAG7E,MAAM,iBAAiB,WAAW,qBAAqB;CACvD,MAAM,KAAK,SAAS,SAAS,QAAQ;CACrC,MAAM,QAAQ,cAAc,kBAAkB,EAAE,OAAO,EAAE,CAAC,gBAAgB,MAAM,CAAC;CAEjF,MAAM,EAAE,OAAO,gBAAgB;AAE/B,iBAAgB;AACd,SAAO,gBAAgB,aAAa,iBAAiB,QAAQ,CAAC;IAC7D,CAAC,aAAa,QAAQ,CAAC;AAE1B,QAAO,IAAC,qBAAqB,UAAtB;EAAsC;EAAQ;EAAyC,CAAA"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { SnapshotFrom } from 'xstate';
|
|
2
|
+
import { AuthActor } from '../types/authenticator.js';
|
|
3
|
+
import { AuthenticatorSendEventAliases, AuthenticatorServiceContextFacade } from '../types/hooks.js';
|
|
4
|
+
import { AuthEvent } from '../types/machines.js';
|
|
5
|
+
export declare const getSendEventAliases: (send: (event: AuthEvent) => void) => AuthenticatorSendEventAliases;
|
|
6
|
+
export declare const getServiceContextFacade: (actor: SnapshotFrom<AuthActor>) => AuthenticatorServiceContextFacade;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//#region src/hooks/facade.ts
|
|
2
|
+
var NAVIGABLE_ROUTE_EVENT = {
|
|
3
|
+
forgotPassword: "FORGOT_PASSWORD",
|
|
4
|
+
signIn: "SIGN_IN",
|
|
5
|
+
signUp: "SIGN_UP",
|
|
6
|
+
signOut: "SIGN_OUT"
|
|
7
|
+
};
|
|
8
|
+
var getSendEventAliases = (send) => {
|
|
9
|
+
return {
|
|
10
|
+
handleSubmit: (data) => send({
|
|
11
|
+
type: "SUBMIT",
|
|
12
|
+
data
|
|
13
|
+
}),
|
|
14
|
+
resendConfirmationCode: () => send({ type: "RESEND" }),
|
|
15
|
+
refreshUser: () => send({ type: "TOKEN_REFRESH" }),
|
|
16
|
+
setRoute: (route) => send({ type: NAVIGABLE_ROUTE_EVENT[route] }),
|
|
17
|
+
skipAttributeVerification: () => send({ type: "SKIP" }),
|
|
18
|
+
toFederatedSignIn: () => send({ type: "FEDERATED_SIGN_IN" })
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
var getChildActor = (snapshot) => {
|
|
22
|
+
return snapshot.children?.childActor?.getSnapshot();
|
|
23
|
+
};
|
|
24
|
+
var getRoute = (actor, childActor) => {
|
|
25
|
+
if (childActor?.matches("federatedSignIn")) {
|
|
26
|
+
if (actor.matches("signUpActor")) return "signUp";
|
|
27
|
+
if (actor.matches("signInActor")) return "signIn";
|
|
28
|
+
}
|
|
29
|
+
switch (true) {
|
|
30
|
+
case actor.matches("idle"): return "idle";
|
|
31
|
+
case actor.matches("setup"): return "setup";
|
|
32
|
+
case actor.matches("signOut"): return "signOut";
|
|
33
|
+
case actor.matches("authenticated"): return "authenticated";
|
|
34
|
+
case childActor?.matches("confirmSignUp"):
|
|
35
|
+
case childActor?.matches("resendSignUpCode"): return "confirmSignUp";
|
|
36
|
+
case childActor?.matches("confirmSignIn"): return "confirmSignIn";
|
|
37
|
+
case childActor?.matches("setupTotp"): return "setupTotp";
|
|
38
|
+
case childActor?.matches("signIn"): return "signIn";
|
|
39
|
+
case childActor?.matches("signUp"):
|
|
40
|
+
case childActor?.matches("autoSignIn"): return "signUp";
|
|
41
|
+
case childActor?.matches("forceChangePassword"): return "forceNewPassword";
|
|
42
|
+
case childActor?.matches("forgotPassword"): return "forgotPassword";
|
|
43
|
+
case childActor?.matches("confirmResetPassword"): return "confirmResetPassword";
|
|
44
|
+
case childActor?.matches("selectUserAttributes"): return "verifyUser";
|
|
45
|
+
case childActor?.matches("confirmVerifyUserAttribute"): return "confirmVerifyUser";
|
|
46
|
+
case childActor?.matches("setupEmail"): return "setupEmail";
|
|
47
|
+
case childActor?.matches("selectMfaType"): return "selectMfaType";
|
|
48
|
+
case actor.matches("getCurrentUser"):
|
|
49
|
+
case childActor?.matches("fetchUserAttributes"): return "transition";
|
|
50
|
+
default: return null;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
var getServiceContextFacade = (actor) => {
|
|
54
|
+
const childActor = getChildActor(actor);
|
|
55
|
+
const isPending = (actor.hasTag("pending") || childActor?.hasTag("pending")) ?? false;
|
|
56
|
+
const route = getRoute(actor, childActor);
|
|
57
|
+
const context = actor.context;
|
|
58
|
+
const actorContext = childActor?.context ?? {};
|
|
59
|
+
const user = actorContext?.user ?? context.user;
|
|
60
|
+
const { allowedMfaTypes, codeDeliveryDetails, remoteError: errorMessage, totpSecretCode, unverifiedUserAttributes, username } = actorContext;
|
|
61
|
+
return {
|
|
62
|
+
allowedMfaTypes,
|
|
63
|
+
codeDeliveryDetails,
|
|
64
|
+
errorMessage,
|
|
65
|
+
isPending,
|
|
66
|
+
route,
|
|
67
|
+
totpSecretCode,
|
|
68
|
+
unverifiedUserAttributes,
|
|
69
|
+
user,
|
|
70
|
+
username
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
//#endregion
|
|
74
|
+
export { getSendEventAliases, getServiceContextFacade };
|
|
75
|
+
|
|
76
|
+
//# sourceMappingURL=facade.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"facade.js","names":[],"sources":["../../src/hooks/facade.ts"],"sourcesContent":["import type { AnyMachineSnapshot, SnapshotFrom } from 'xstate'\nimport type { AuthActor } from '~/types/authenticator.js'\nimport type { AuthenticatorRoute, AuthenticatorSendEventAliases, AuthenticatorServiceContextFacade } from '~/types/hooks.js'\nimport type { AuthActorContext, AuthEvent, AuthEventTypes, NavigableRoute } from '~/types/machines.js'\n\nconst NAVIGABLE_ROUTE_EVENT: Record<NavigableRoute, AuthEventTypes> = {\n forgotPassword: 'FORGOT_PASSWORD',\n signIn: 'SIGN_IN',\n signUp: 'SIGN_UP',\n signOut: 'SIGN_OUT',\n}\n\nexport const getSendEventAliases = (send: (event: AuthEvent) => void): AuthenticatorSendEventAliases => {\n return {\n handleSubmit: (data) => send({ type: 'SUBMIT', data }),\n resendConfirmationCode: () => send({ type: 'RESEND' }),\n refreshUser: () => send({ type: 'TOKEN_REFRESH' }),\n setRoute: (route) => send({ type: NAVIGABLE_ROUTE_EVENT[route] }),\n skipAttributeVerification: () => send({ type: 'SKIP' }),\n toFederatedSignIn: () => send({ type: 'FEDERATED_SIGN_IN' }),\n }\n}\n\nconst getChildActor = (snapshot: SnapshotFrom<AuthActor>): AnyMachineSnapshot | undefined => {\n return snapshot.children?.childActor?.getSnapshot() as AnyMachineSnapshot\n}\n\nconst getRoute = (actor: SnapshotFrom<AuthActor>, childActor: AnyMachineSnapshot | undefined): AuthenticatorRoute | null => {\n // 'federatedSignIn' exists as a state on both the 'signInActor' and 'signUpActor',\n // match against the `actorState` initially to determine if the federated sign in flow\n // has begun, then which actor has begun the flow and return the corresponding `route`\n if (childActor?.matches('federatedSignIn')) {\n if (actor.matches('signUpActor')) {\n return 'signUp'\n }\n if (actor.matches('signInActor')) {\n return 'signIn'\n }\n }\n\n switch (true) {\n case actor.matches('idle'):\n return 'idle'\n case actor.matches('setup'):\n return 'setup'\n case actor.matches('signOut'):\n return 'signOut'\n case actor.matches('authenticated'):\n return 'authenticated'\n case childActor?.matches('confirmSignUp'):\n case childActor?.matches('resendSignUpCode'):\n return 'confirmSignUp'\n case childActor?.matches('confirmSignIn'):\n return 'confirmSignIn'\n case childActor?.matches('setupTotp'):\n return 'setupTotp'\n case childActor?.matches('signIn'):\n return 'signIn'\n case childActor?.matches('signUp'):\n case childActor?.matches('autoSignIn'):\n return 'signUp'\n case childActor?.matches('forceChangePassword'):\n return 'forceNewPassword'\n case childActor?.matches('forgotPassword'):\n return 'forgotPassword'\n case childActor?.matches('confirmResetPassword'):\n return 'confirmResetPassword'\n case childActor?.matches('selectUserAttributes'):\n return 'verifyUser'\n case childActor?.matches('confirmVerifyUserAttribute'):\n return 'confirmVerifyUser'\n case childActor?.matches('setupEmail'):\n return 'setupEmail'\n case childActor?.matches('selectMfaType'):\n return 'selectMfaType'\n case actor.matches('getCurrentUser'):\n case childActor?.matches('fetchUserAttributes'):\n /**\n * This route is needed for autoSignIn to capture both the\n * autoSignIn.pending and the resolved states when the\n * signIn actor is running.\n */\n return 'transition'\n default:\n return null\n }\n}\n\nexport const getServiceContextFacade = (actor: SnapshotFrom<AuthActor>): AuthenticatorServiceContextFacade => {\n const childActor = getChildActor(actor)\n const isPending = (actor.hasTag('pending') || childActor?.hasTag('pending')) ?? false\n const route = getRoute(actor, childActor)\n const context = actor.context\n const actorContext: AuthActorContext = childActor?.context ?? {}\n const user = actorContext?.user ?? context.user\n\n const { allowedMfaTypes, codeDeliveryDetails, remoteError: errorMessage, totpSecretCode, unverifiedUserAttributes, username } = actorContext\n\n return {\n allowedMfaTypes,\n codeDeliveryDetails,\n errorMessage,\n isPending,\n route,\n totpSecretCode,\n unverifiedUserAttributes,\n user,\n username,\n }\n}\n"],"mappings":";AAKA,IAAM,wBAAgE;CACpE,gBAAgB;CAChB,QAAQ;CACR,QAAQ;CACR,SAAS;CACV;AAED,IAAa,uBAAuB,SAAoE;AACtG,QAAO;EACL,eAAe,SAAS,KAAK;GAAE,MAAM;GAAU;GAAM,CAAC;EACtD,8BAA8B,KAAK,EAAE,MAAM,UAAU,CAAC;EACtD,mBAAmB,KAAK,EAAE,MAAM,iBAAiB,CAAC;EAClD,WAAW,UAAU,KAAK,EAAE,MAAM,sBAAsB,QAAQ,CAAC;EACjE,iCAAiC,KAAK,EAAE,MAAM,QAAQ,CAAC;EACvD,yBAAyB,KAAK,EAAE,MAAM,qBAAqB,CAAC;EAC7D;;AAGH,IAAM,iBAAiB,aAAsE;AAC3F,QAAO,SAAS,UAAU,YAAY,aAAa;;AAGrD,IAAM,YAAY,OAAgC,eAA0E;AAI1H,KAAI,YAAY,QAAQ,kBAAkB,EAAE;AAC1C,MAAI,MAAM,QAAQ,cAAc,CAC9B,QAAO;AAET,MAAI,MAAM,QAAQ,cAAc,CAC9B,QAAO;;AAIX,SAAQ,MAAR;EACE,KAAK,MAAM,QAAQ,OAAO,CACxB,QAAO;EACT,KAAK,MAAM,QAAQ,QAAQ,CACzB,QAAO;EACT,KAAK,MAAM,QAAQ,UAAU,CAC3B,QAAO;EACT,KAAK,MAAM,QAAQ,gBAAgB,CACjC,QAAO;EACT,KAAK,YAAY,QAAQ,gBAAgB;EACzC,KAAK,YAAY,QAAQ,mBAAmB,CAC1C,QAAO;EACT,KAAK,YAAY,QAAQ,gBAAgB,CACvC,QAAO;EACT,KAAK,YAAY,QAAQ,YAAY,CACnC,QAAO;EACT,KAAK,YAAY,QAAQ,SAAS,CAChC,QAAO;EACT,KAAK,YAAY,QAAQ,SAAS;EAClC,KAAK,YAAY,QAAQ,aAAa,CACpC,QAAO;EACT,KAAK,YAAY,QAAQ,sBAAsB,CAC7C,QAAO;EACT,KAAK,YAAY,QAAQ,iBAAiB,CACxC,QAAO;EACT,KAAK,YAAY,QAAQ,uBAAuB,CAC9C,QAAO;EACT,KAAK,YAAY,QAAQ,uBAAuB,CAC9C,QAAO;EACT,KAAK,YAAY,QAAQ,6BAA6B,CACpD,QAAO;EACT,KAAK,YAAY,QAAQ,aAAa,CACpC,QAAO;EACT,KAAK,YAAY,QAAQ,gBAAgB,CACvC,QAAO;EACT,KAAK,MAAM,QAAQ,iBAAiB;EACpC,KAAK,YAAY,QAAQ,sBAAsB,CAM7C,QAAO;EACT,QACE,QAAO;;;AAIb,IAAa,2BAA2B,UAAsE;CAC5G,MAAM,aAAa,cAAc,MAAM;CACvC,MAAM,aAAa,MAAM,OAAO,UAAU,IAAI,YAAY,OAAO,UAAU,KAAK;CAChF,MAAM,QAAQ,SAAS,OAAO,WAAW;CACzC,MAAM,UAAU,MAAM;CACtB,MAAM,eAAiC,YAAY,WAAW,EAAE;CAChE,MAAM,OAAO,cAAc,QAAQ,QAAQ;CAE3C,MAAM,EAAE,iBAAiB,qBAAqB,aAAa,cAAc,gBAAgB,0BAA0B,aAAa;AAEhI,QAAO;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACD"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AuthUser } from '@aws-amplify/auth';
|
|
2
|
+
import { Handlers } from '../types/machines.js';
|
|
3
|
+
export type UseAuthResult = {
|
|
4
|
+
user?: AuthUser;
|
|
5
|
+
isLoading: boolean;
|
|
6
|
+
error?: Error;
|
|
7
|
+
};
|
|
8
|
+
type Props = {
|
|
9
|
+
getCurrentUser?: Handlers['getCurrentUser'];
|
|
10
|
+
};
|
|
11
|
+
export declare const useAuth: (handlers?: Props) => UseAuthResult;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { Hub } from "@aws-amplify/core";
|
|
3
|
+
import { getCurrentUser } from "@aws-amplify/auth";
|
|
4
|
+
//#region src/hooks/useAuth.ts
|
|
5
|
+
var useAuth = (handlers) => {
|
|
6
|
+
const [result, setResult] = useState({ isLoading: true });
|
|
7
|
+
const fetchCurrentUser = useCallback(() => {
|
|
8
|
+
setResult((prev) => ({
|
|
9
|
+
...prev,
|
|
10
|
+
isLoading: true
|
|
11
|
+
}));
|
|
12
|
+
(handlers?.getCurrentUser ?? getCurrentUser)().then((user) => setResult({
|
|
13
|
+
user,
|
|
14
|
+
isLoading: false
|
|
15
|
+
})).catch((e) => setResult({
|
|
16
|
+
error: e,
|
|
17
|
+
isLoading: false
|
|
18
|
+
}));
|
|
19
|
+
}, [handlers?.getCurrentUser]);
|
|
20
|
+
const handleAuth = useCallback(({ payload }) => {
|
|
21
|
+
switch (payload.event) {
|
|
22
|
+
case "signedIn":
|
|
23
|
+
case "signUp":
|
|
24
|
+
case "autoSignIn":
|
|
25
|
+
setResult({
|
|
26
|
+
user: payload.data,
|
|
27
|
+
isLoading: false
|
|
28
|
+
});
|
|
29
|
+
break;
|
|
30
|
+
case "signedOut":
|
|
31
|
+
setResult({ isLoading: false });
|
|
32
|
+
break;
|
|
33
|
+
case "tokenRefresh_failure":
|
|
34
|
+
case "signIn_failure":
|
|
35
|
+
setResult({
|
|
36
|
+
error: payload.data,
|
|
37
|
+
isLoading: false
|
|
38
|
+
});
|
|
39
|
+
break;
|
|
40
|
+
case "autoSignIn_failure":
|
|
41
|
+
setResult({
|
|
42
|
+
error: new Error(payload.message),
|
|
43
|
+
isLoading: false
|
|
44
|
+
});
|
|
45
|
+
break;
|
|
46
|
+
case "tokenRefresh":
|
|
47
|
+
fetchCurrentUser();
|
|
48
|
+
break;
|
|
49
|
+
default: break;
|
|
50
|
+
}
|
|
51
|
+
}, [fetchCurrentUser]);
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
const unsubscribe = Hub.listen("auth", handleAuth, "useAuth");
|
|
54
|
+
fetchCurrentUser();
|
|
55
|
+
return unsubscribe;
|
|
56
|
+
}, [handleAuth, fetchCurrentUser]);
|
|
57
|
+
return result;
|
|
58
|
+
};
|
|
59
|
+
//#endregion
|
|
60
|
+
export { useAuth };
|
|
61
|
+
|
|
62
|
+
//# sourceMappingURL=useAuth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAuth.js","names":[],"sources":["../../src/hooks/useAuth.ts"],"sourcesContent":["/*\n * Copyright 2017 - 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.\n * (C) 2025 GAHOJIN, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n */\n\nimport { type AuthUser, getCurrentUser } from '@aws-amplify/auth'\nimport { Hub, type HubCallback } from '@aws-amplify/core'\nimport { useCallback, useEffect, useState } from 'react'\nimport type { Handlers } from '~/types/machines.js'\n\nexport type UseAuthResult = {\n user?: AuthUser\n isLoading: boolean\n error?: Error\n}\n\ntype Props = {\n getCurrentUser?: Handlers['getCurrentUser']\n}\n\nexport const useAuth = (handlers?: Props): UseAuthResult => {\n const [result, setResult] = useState<UseAuthResult>({\n isLoading: true,\n })\n\n const fetchCurrentUser = useCallback(() => {\n // loading\n setResult((prev) => ({ ...prev, isLoading: true }))\n\n void (handlers?.getCurrentUser ?? getCurrentUser)()\n .then((user) => setResult({ user, isLoading: false }))\n .catch((e) => setResult({ error: e as Error, isLoading: false }))\n }, [handlers?.getCurrentUser])\n\n const handleAuth: HubCallback = useCallback(\n ({ payload }) => {\n switch (payload.event) {\n // success events\n case 'signedIn':\n case 'signUp':\n case 'autoSignIn':\n setResult({ user: payload.data as AuthUser, isLoading: false })\n break\n case 'signedOut':\n setResult({ isLoading: false })\n break\n\n // failure events\n case 'tokenRefresh_failure':\n case 'signIn_failure':\n setResult({ error: payload.data as Error, isLoading: false })\n break\n case 'autoSignIn_failure':\n // autoSignIn just returns error message. Wrap it to an Error object\n setResult({ error: new Error(payload.message), isLoading: false })\n break\n\n // events that need another fetch\n case 'tokenRefresh':\n void fetchCurrentUser()\n break\n\n default:\n // we do not handle other hub events like `configured`.\n break\n }\n },\n [fetchCurrentUser],\n )\n\n useEffect(() => {\n const unsubscribe = Hub.listen('auth', handleAuth, 'useAuth')\n void fetchCurrentUser() // on init, see if user is already logged in\n\n return unsubscribe\n }, [handleAuth, fetchCurrentUser])\n\n return result\n}\n"],"mappings":";;;;AA0BA,IAAa,WAAW,aAAoC;CAC1D,MAAM,CAAC,QAAQ,aAAa,SAAwB,EAClD,WAAW,MACZ,CAAC;CAEF,MAAM,mBAAmB,kBAAkB;AAEzC,aAAW,UAAU;GAAE,GAAG;GAAM,WAAW;GAAM,EAAE;AAEnD,GAAM,UAAU,kBAAkB,iBAAiB,CAChD,MAAM,SAAS,UAAU;GAAE;GAAM,WAAW;GAAO,CAAC,CAAC,CACrD,OAAO,MAAM,UAAU;GAAE,OAAO;GAAY,WAAW;GAAO,CAAC,CAAC;IAClE,CAAC,UAAU,eAAe,CAAC;CAE9B,MAAM,aAA0B,aAC7B,EAAE,cAAc;AACf,UAAQ,QAAQ,OAAhB;GAEE,KAAK;GACL,KAAK;GACL,KAAK;AACH,cAAU;KAAE,MAAM,QAAQ;KAAkB,WAAW;KAAO,CAAC;AAC/D;GACF,KAAK;AACH,cAAU,EAAE,WAAW,OAAO,CAAC;AAC/B;GAGF,KAAK;GACL,KAAK;AACH,cAAU;KAAE,OAAO,QAAQ;KAAe,WAAW;KAAO,CAAC;AAC7D;GACF,KAAK;AAEH,cAAU;KAAE,OAAO,IAAI,MAAM,QAAQ,QAAQ;KAAE,WAAW;KAAO,CAAC;AAClE;GAGF,KAAK;AACE,sBAAkB;AACvB;GAEF,QAEE;;IAGN,CAAC,iBAAiB,CACnB;AAED,iBAAgB;EACd,MAAM,cAAc,IAAI,OAAO,QAAQ,YAAY,UAAU;AACxD,oBAAkB;AAEvB,SAAO;IACN,CAAC,YAAY,iBAAiB,CAAC;AAElC,QAAO"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { AuthenticatorContext } from "../context/AuthenticatorContext.js";
|
|
2
|
+
import { getComparator } from "./utils.js";
|
|
3
|
+
import { getSendEventAliases, getServiceContextFacade } from "./facade.js";
|
|
4
|
+
import { useContext } from "react";
|
|
5
|
+
import { useSelector } from "@xstate/react";
|
|
6
|
+
//#region src/hooks/useAuthenticator.ts
|
|
7
|
+
var defaultComparator = () => false;
|
|
8
|
+
var useAuthenticator = (selector) => {
|
|
9
|
+
const context = useContext(AuthenticatorContext);
|
|
10
|
+
if (!context) throw new Error("`useAuthenticator` must be used inside an `AuthenticatorProvider`.");
|
|
11
|
+
const { actor } = context;
|
|
12
|
+
const { send } = actor;
|
|
13
|
+
return useSelector(actor, (snapshot) => ({
|
|
14
|
+
...getSendEventAliases(send),
|
|
15
|
+
...getServiceContextFacade(snapshot)
|
|
16
|
+
}), selector ? getComparator(selector) : defaultComparator);
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
export { useAuthenticator };
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=useAuthenticator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAuthenticator.js","names":[],"sources":["../../src/hooks/useAuthenticator.ts"],"sourcesContent":["import { useSelector } from '@xstate/react'\nimport { useContext } from 'react'\nimport { AuthenticatorContext } from '~/context/index.js'\nimport { getComparator } from '~/hooks/utils.js'\nimport type { UseAuthenticator, UseAuthenticatorSelector } from '~/types/hooks.js'\nimport { getSendEventAliases, getServiceContextFacade } from './facade.js'\n\nconst defaultComparator = (): false => false\n\nexport const useAuthenticator = (selector?: UseAuthenticatorSelector): UseAuthenticator => {\n const context = useContext(AuthenticatorContext)\n\n if (!context) {\n throw new Error('`useAuthenticator` must be used inside an `AuthenticatorProvider`.')\n }\n\n const { actor } = context\n const { send } = actor\n\n const comparator = selector ? getComparator(selector) : defaultComparator\n\n return useSelector(\n actor,\n (snapshot) => ({\n ...getSendEventAliases(send),\n ...getServiceContextFacade(snapshot),\n }),\n comparator,\n )\n}\n"],"mappings":";;;;;;AAOA,IAAM,0BAAiC;AAEvC,IAAa,oBAAoB,aAA0D;CACzF,MAAM,UAAU,WAAW,qBAAqB;AAEhD,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,qEAAqE;CAGvF,MAAM,EAAE,UAAU;CAClB,MAAM,EAAE,SAAS;AAIjB,QAAO,YACL,QACC,cAAc;EACb,GAAG,oBAAoB,KAAK;EAC5B,GAAG,wBAAwB,SAAS;EACrC,GAPgB,WAAW,cAAc,SAAS,GAAG,kBASvD"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Comparator, UseAuthenticatorSelector } from '../types/hooks.js';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if `value` is an Object (non-primitive, non-array, non-function)
|
|
4
|
+
* Will return false for Arrays and functions
|
|
5
|
+
*
|
|
6
|
+
*
|
|
7
|
+
* @param {unknown} value The value to check
|
|
8
|
+
* @returns {boolean} Returns `true` if `value` is an object, `false` otherwise
|
|
9
|
+
*/
|
|
10
|
+
export declare function isObject(value: unknown): value is object;
|
|
11
|
+
/**
|
|
12
|
+
* Checks if `value` is a string primitive or object
|
|
13
|
+
*
|
|
14
|
+
* @param {unknown} value The value to check
|
|
15
|
+
* @returns {boolean} Returns `true` if `value` is a string, `false` otherwise
|
|
16
|
+
*/
|
|
17
|
+
export declare function isString(value: unknown): value is string;
|
|
18
|
+
/**
|
|
19
|
+
* Checks if `value` is a Map
|
|
20
|
+
*
|
|
21
|
+
* @param {unknown} value The value to check
|
|
22
|
+
* @returns {boolean} Returns `true` if `value` is a Map, `false` otherwise
|
|
23
|
+
*/
|
|
24
|
+
export declare function isMap(value: unknown): value is Map<unknown, unknown>;
|
|
25
|
+
/**
|
|
26
|
+
* Checks if `value` is a Set
|
|
27
|
+
*
|
|
28
|
+
* @param {unknown} value The value to check
|
|
29
|
+
* @returns {boolean} Returns `true` if `value` is a Set, `false` otherwise
|
|
30
|
+
*/
|
|
31
|
+
export declare function isSet<T>(value: unknown): value is Set<T>;
|
|
32
|
+
/**
|
|
33
|
+
* Checks if `value` is undefined
|
|
34
|
+
*
|
|
35
|
+
* @param {unknown} value The value to check
|
|
36
|
+
* @returns {boolean} Returns `true` if `value` is undefined, `false` otherwise
|
|
37
|
+
*/
|
|
38
|
+
export declare function isUndefined(value: unknown): value is undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Checks if `value` is nullish
|
|
41
|
+
*
|
|
42
|
+
* @param {unknown} value The value to check
|
|
43
|
+
* @returns {boolean} Returns `true` if `value` is nullish, `false` otherwise
|
|
44
|
+
*/
|
|
45
|
+
export declare function isNil(value: unknown): value is null | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Checks if `value` is empty
|
|
48
|
+
*
|
|
49
|
+
* @param {unknown} value The value to check
|
|
50
|
+
* @returns {boolean} Returns `true` if `value` is empty, `false` otherwise
|
|
51
|
+
*/
|
|
52
|
+
export declare function isEmpty<T>(value: T): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Checks if all members of the `values` param are empty arrays
|
|
55
|
+
*
|
|
56
|
+
* @param {unknown} value The values to check
|
|
57
|
+
* @returns {boolean} Returns `true` if all members of `values` are empty, `false` otherwise
|
|
58
|
+
*/
|
|
59
|
+
export declare function areEmptyArrays<T>(...values: T[]): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Checks if `value` is an empty object
|
|
62
|
+
*
|
|
63
|
+
* @param {unknown} value The value to check
|
|
64
|
+
* @returns {boolean} Returns `true` if `value` is empty, `false` otherwise
|
|
65
|
+
*/
|
|
66
|
+
export declare function isEmptyObject<T>(value: T): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Checks if all members of the `values` param are empty objects
|
|
69
|
+
*
|
|
70
|
+
* @param {unknown} values The values to check
|
|
71
|
+
* @returns {boolean} Returns `true` if all members of the `values` param are empty, `false` otherwise
|
|
72
|
+
*/
|
|
73
|
+
export declare function areEmptyObjects<T>(...values: T[]): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Checks if `key` is a direct property of `value`
|
|
76
|
+
*
|
|
77
|
+
* @param {unknown} value `object` potentially containing property
|
|
78
|
+
* @param {string} key property key
|
|
79
|
+
* @returns whether `key` param is a property of the `obj` param
|
|
80
|
+
*/
|
|
81
|
+
export declare function has(value: unknown, key: string): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Does an ordering and shallow comparison of each array value,
|
|
84
|
+
* plus a value equality check for empty objects and arrays.
|
|
85
|
+
*/
|
|
86
|
+
export declare function areSelectorDepsEqual<T>(currentDeps: T[], nextDeps: T[]): boolean;
|
|
87
|
+
export declare const getComparator: (selector: UseAuthenticatorSelector) => Comparator;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//#region src/hooks/utils.ts
|
|
2
|
+
function isObject(value) {
|
|
3
|
+
return value != null && !Array.isArray(value) && typeof value === "object";
|
|
4
|
+
}
|
|
5
|
+
function isString(value) {
|
|
6
|
+
return typeof value === "string" || typeof value === "object" && Object.prototype.toString.call(value) === "[object String]";
|
|
7
|
+
}
|
|
8
|
+
function isMap(value) {
|
|
9
|
+
return isObject(value) && Object.prototype.toString.call(value) === "[object Map]";
|
|
10
|
+
}
|
|
11
|
+
function isSet(value) {
|
|
12
|
+
return isObject(value) && Object.prototype.toString.call(value) === "[object Set]";
|
|
13
|
+
}
|
|
14
|
+
function isEmpty(value) {
|
|
15
|
+
if (value === null || value === void 0) return true;
|
|
16
|
+
if (isObject(value) && (isMap(value) || isSet(value))) return !value.size;
|
|
17
|
+
if (isObject(value) && (isString(value) || Array.isArray(value))) return !value.length;
|
|
18
|
+
for (const key in value) if (has(value, key)) return false;
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
function isEmptyArray(value) {
|
|
22
|
+
return Array.isArray(value) && isEmpty(value);
|
|
23
|
+
}
|
|
24
|
+
function areEmptyArrays(...values) {
|
|
25
|
+
return values.every(isEmptyArray);
|
|
26
|
+
}
|
|
27
|
+
function isEmptyObject(value) {
|
|
28
|
+
return isObject(value) && isEmpty(value);
|
|
29
|
+
}
|
|
30
|
+
function areEmptyObjects(...values) {
|
|
31
|
+
return values.every(isEmptyObject);
|
|
32
|
+
}
|
|
33
|
+
function has(value, key) {
|
|
34
|
+
return value != null && Object.hasOwn(value, key);
|
|
35
|
+
}
|
|
36
|
+
function areSelectorDepsEqual(currentDeps, nextDeps) {
|
|
37
|
+
if (currentDeps.length !== nextDeps.length) return false;
|
|
38
|
+
return currentDeps.every((currentDep, index) => {
|
|
39
|
+
const nextDep = nextDeps[index];
|
|
40
|
+
if (areEmptyArrays(currentDep, nextDep) || areEmptyObjects(currentDep, nextDep)) return true;
|
|
41
|
+
return currentDep === nextDep;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
var getComparator = (selector) => {
|
|
45
|
+
return (currentFacade, nextFacade) => {
|
|
46
|
+
return areSelectorDepsEqual(selector(currentFacade), selector(nextFacade));
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
//#endregion
|
|
50
|
+
export { getComparator };
|
|
51
|
+
|
|
52
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","names":[],"sources":["../../src/hooks/utils.ts"],"sourcesContent":["/*\n * Copyright 2017 - 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.\n * (C) 2025 GAHOJIN, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n */\n\nimport type { Comparator, UseAuthenticatorSelector } from '~/types/hooks.js'\n\n/**\n * Checks if `value` is an Object (non-primitive, non-array, non-function)\n * Will return false for Arrays and functions\n *\n *\n * @param {unknown} value The value to check\n * @returns {boolean} Returns `true` if `value` is an object, `false` otherwise\n */\nexport function isObject(value: unknown): value is object {\n return value != null && !Array.isArray(value) && typeof value === 'object'\n}\n\n/**\n * Checks if `value` is a string primitive or object\n *\n * @param {unknown} value The value to check\n * @returns {boolean} Returns `true` if `value` is a string, `false` otherwise\n */\nexport function isString(value: unknown): value is string {\n return typeof value === 'string' || (typeof value === 'object' && Object.prototype.toString.call(value) === '[object String]')\n}\n\n/**\n * Checks if `value` is a Map\n *\n * @param {unknown} value The value to check\n * @returns {boolean} Returns `true` if `value` is a Map, `false` otherwise\n */\nexport function isMap(value: unknown): value is Map<unknown, unknown> {\n return isObject(value) && Object.prototype.toString.call(value) === '[object Map]'\n}\n\n/**\n * Checks if `value` is a Set\n *\n * @param {unknown} value The value to check\n * @returns {boolean} Returns `true` if `value` is a Set, `false` otherwise\n */\nexport function isSet<T>(value: unknown): value is Set<T> {\n return isObject(value) && Object.prototype.toString.call(value) === '[object Set]'\n}\n\n/**\n * Checks if `value` is undefined\n *\n * @param {unknown} value The value to check\n * @returns {boolean} Returns `true` if `value` is undefined, `false` otherwise\n */\nexport function isUndefined(value: unknown): value is undefined {\n return value === undefined\n}\n\n/**\n * Checks if `value` is nullish\n *\n * @param {unknown} value The value to check\n * @returns {boolean} Returns `true` if `value` is nullish, `false` otherwise\n */\nexport function isNil(value: unknown): value is null | undefined {\n return value == null\n}\n\n/**\n * Checks if `value` is empty\n *\n * @param {unknown} value The value to check\n * @returns {boolean} Returns `true` if `value` is empty, `false` otherwise\n */\nexport function isEmpty<T>(value: T): boolean {\n if (value === null || value === undefined) return true\n\n if (isObject(value) && (isMap(value) || isSet(value))) {\n return !value.size\n }\n if (isObject(value) && (isString(value) || Array.isArray(value))) {\n return !value.length\n }\n for (const key in value) {\n if (has(value, key)) {\n return false\n }\n }\n return true\n}\n\n/**\n * Checks if `value` is an empty array\n *\n * @param {unknown} value The value to check\n * @returns {boolean} Returns `true` if `value` is a empty, `false` otherwise\n */\nfunction isEmptyArray<T>(value: T): boolean {\n return Array.isArray(value) && isEmpty(value)\n}\n\n/**\n * Checks if all members of the `values` param are empty arrays\n *\n * @param {unknown} value The values to check\n * @returns {boolean} Returns `true` if all members of `values` are empty, `false` otherwise\n */\nexport function areEmptyArrays<T>(...values: T[]): boolean {\n return values.every(isEmptyArray)\n}\n\n/**\n * Checks if `value` is an empty object\n *\n * @param {unknown} value The value to check\n * @returns {boolean} Returns `true` if `value` is empty, `false` otherwise\n */\nexport function isEmptyObject<T>(value: T): boolean {\n return isObject(value) && isEmpty(value)\n}\n\n/**\n * Checks if all members of the `values` param are empty objects\n *\n * @param {unknown} values The values to check\n * @returns {boolean} Returns `true` if all members of the `values` param are empty, `false` otherwise\n */\nexport function areEmptyObjects<T>(...values: T[]): boolean {\n return values.every(isEmptyObject)\n}\n\n/**\n * Checks if `key` is a direct property of `value`\n *\n * @param {unknown} value `object` potentially containing property\n * @param {string} key property key\n * @returns whether `key` param is a property of the `obj` param\n */\nexport function has(value: unknown, key: string): boolean {\n return value != null && Object.hasOwn(value, key)\n}\n\n/**\n * Does an ordering and shallow comparison of each array value,\n * plus a value equality check for empty objects and arrays.\n */\nexport function areSelectorDepsEqual<T>(currentDeps: T[], nextDeps: T[]): boolean {\n if (currentDeps.length !== nextDeps.length) {\n return false\n }\n return currentDeps.every((currentDep, index) => {\n const nextDep = nextDeps[index]\n\n if (areEmptyArrays(currentDep, nextDep) || areEmptyObjects(currentDep, nextDep)) {\n return true\n }\n\n return currentDep === nextDep\n })\n}\n\nexport const getComparator = (selector: UseAuthenticatorSelector): Comparator => {\n return (currentFacade, nextFacade) => {\n const currentSelectorDeps = selector(currentFacade)\n const nextSelectorDeps = selector(nextFacade)\n\n // Shallow compare the array values\n return areSelectorDepsEqual(currentSelectorDeps, nextSelectorDeps)\n }\n}\n"],"mappings":";AAqBA,SAAgB,SAAS,OAAiC;AACxD,QAAO,SAAS,QAAQ,CAAC,MAAM,QAAQ,MAAM,IAAI,OAAO,UAAU;;AASpE,SAAgB,SAAS,OAAiC;AACxD,QAAO,OAAO,UAAU,YAAa,OAAO,UAAU,YAAY,OAAO,UAAU,SAAS,KAAK,MAAM,KAAK;;AAS9G,SAAgB,MAAM,OAAgD;AACpE,QAAO,SAAS,MAAM,IAAI,OAAO,UAAU,SAAS,KAAK,MAAM,KAAK;;AAStE,SAAgB,MAAS,OAAiC;AACxD,QAAO,SAAS,MAAM,IAAI,OAAO,UAAU,SAAS,KAAK,MAAM,KAAK;;AA6BtE,SAAgB,QAAW,OAAmB;AAC5C,KAAI,UAAU,QAAQ,UAAU,KAAA,EAAW,QAAO;AAElD,KAAI,SAAS,MAAM,KAAK,MAAM,MAAM,IAAI,MAAM,MAAM,EAClD,QAAO,CAAC,MAAM;AAEhB,KAAI,SAAS,MAAM,KAAK,SAAS,MAAM,IAAI,MAAM,QAAQ,MAAM,EAC7D,QAAO,CAAC,MAAM;AAEhB,MAAK,MAAM,OAAO,MAChB,KAAI,IAAI,OAAO,IAAI,CACjB,QAAO;AAGX,QAAO;;AAST,SAAS,aAAgB,OAAmB;AAC1C,QAAO,MAAM,QAAQ,MAAM,IAAI,QAAQ,MAAM;;AAS/C,SAAgB,eAAkB,GAAG,QAAsB;AACzD,QAAO,OAAO,MAAM,aAAa;;AASnC,SAAgB,cAAiB,OAAmB;AAClD,QAAO,SAAS,MAAM,IAAI,QAAQ,MAAM;;AAS1C,SAAgB,gBAAmB,GAAG,QAAsB;AAC1D,QAAO,OAAO,MAAM,cAAc;;AAUpC,SAAgB,IAAI,OAAgB,KAAsB;AACxD,QAAO,SAAS,QAAQ,OAAO,OAAO,OAAO,IAAI;;AAOnD,SAAgB,qBAAwB,aAAkB,UAAwB;AAChF,KAAI,YAAY,WAAW,SAAS,OAClC,QAAO;AAET,QAAO,YAAY,OAAO,YAAY,UAAU;EAC9C,MAAM,UAAU,SAAS;AAEzB,MAAI,eAAe,YAAY,QAAQ,IAAI,gBAAgB,YAAY,QAAQ,CAC7E,QAAO;AAGT,SAAO,eAAe;GACtB;;AAGJ,IAAa,iBAAiB,aAAmD;AAC/E,SAAQ,eAAe,eAAe;AAKpC,SAAO,qBAJqB,SAAS,cAIT,EAHH,SAAS,WAGe,CAAiB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,66 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
};
|
|
9
|
-
declare const AuthenticatorContext: Context<AuthenticatorContextType | null>;
|
|
10
|
-
//#endregion
|
|
11
|
-
//#region src/context/AuthenticatorProvider.d.ts
|
|
12
|
-
type Props$1 = PropsWithChildren & {
|
|
13
|
-
options?: AuthenticatorMachineOptions;
|
|
14
|
-
};
|
|
15
|
-
declare const _default: ({
|
|
16
|
-
options,
|
|
17
|
-
children
|
|
18
|
-
}: Props$1) => JSX.Element;
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region src/hooks/types.d.ts
|
|
21
|
-
type AuthenticatorRoute = "authenticated" | "confirmResetPassword" | "confirmSignIn" | "confirmSignUp" | "confirmVerifyUser" | "forceNewPassword" | "idle" | "forgotPassword" | "setup" | "signOut" | "selectMfaType" | "setupEmail" | "setupTotp" | "signIn" | "signUp" | "transition" | "verifyUser";
|
|
22
|
-
type AuthenticatorMachineContext = AuthenticatorServiceFacade;
|
|
23
|
-
type AuthenticatorMachineContextKey = keyof AuthenticatorMachineContext;
|
|
24
|
-
type UseAuthenticatorSelector = (context: AuthenticatorServiceFacade) => AuthenticatorMachineContext[AuthenticatorMachineContextKey][];
|
|
25
|
-
type UseAuthenticator = AuthenticatorServiceFacade;
|
|
26
|
-
type AuthenticatorServiceContextFacade = {
|
|
27
|
-
allowedMfaTypes: AuthAllowedMFATypes | undefined;
|
|
28
|
-
codeDeliveryDetails: CodeDeliveryDetails | undefined;
|
|
29
|
-
errorMessage: string | undefined;
|
|
30
|
-
isPending: boolean;
|
|
31
|
-
route: AuthenticatorRoute | null;
|
|
32
|
-
totpSecretCode: string | undefined;
|
|
33
|
-
username: string | undefined;
|
|
34
|
-
user: AuthUser | undefined;
|
|
35
|
-
unverifiedUserAttributes: UnverifiedUserAttributes | undefined;
|
|
36
|
-
};
|
|
37
|
-
type AuthenticatorSendEventAliases = {
|
|
38
|
-
handleSubmit: (data?: AuthEventData) => void;
|
|
39
|
-
resendConfirmationCode: () => void;
|
|
40
|
-
refreshUser: () => void;
|
|
41
|
-
setRoute: (route: NavigableRoute) => void;
|
|
42
|
-
skipAttributeVerification: () => void;
|
|
43
|
-
toFederatedSignIn: (data?: AuthEventData) => void;
|
|
44
|
-
};
|
|
45
|
-
type AuthenticatorServiceFacade = AuthenticatorSendEventAliases & AuthenticatorServiceContextFacade;
|
|
46
|
-
type Comparator = (currentMachineContext: AuthenticatorMachineContext, nextMachineContext: AuthenticatorMachineContext) => boolean;
|
|
47
|
-
//#endregion
|
|
48
|
-
//#region src/hooks/useAuth.d.ts
|
|
49
|
-
type UseAuthResult = {
|
|
50
|
-
user?: AuthUser;
|
|
51
|
-
isLoading: boolean;
|
|
52
|
-
error?: Error;
|
|
53
|
-
};
|
|
54
|
-
type Props = {
|
|
55
|
-
getCurrentUser?: Handlers["getCurrentUser"];
|
|
56
|
-
};
|
|
57
|
-
declare const useAuth: (handlers?: Props) => UseAuthResult;
|
|
58
|
-
//#endregion
|
|
59
|
-
//#region src/hooks/useAuthenticator.d.ts
|
|
60
|
-
declare const useAuthenticator: (selector?: UseAuthenticatorSelector) => UseAuthenticator;
|
|
61
|
-
//#endregion
|
|
62
|
-
//#region src/utils.d.ts
|
|
63
|
-
declare const getTotpCodeURL: (issuer: string, username: string, secret: string) => string;
|
|
64
|
-
//#endregion
|
|
65
|
-
export { ActorDoneData, AuthActorContext, AuthAllowedMFATypes, AuthContext, AuthError, AuthEvent, AuthEventData, AuthEventTypes, AuthMFAType, AuthTOTPSetupDetails, AuthenticatorContext, _default as AuthenticatorProvider, AuthenticatorRoute, AuthenticatorSendEventAliases, AuthenticatorServiceContextFacade, AuthenticatorServiceFacade, Comparator, FederatedIdentityProviders, Handlers, InitialRoute, InitialStep, NavigableRoute, ResetPasswordContext, ResetPasswordStep, SignInContext, SignInStep, SignOutContext, SignUpContext, SignUpStep, Step, UnverifiedContactMethodType, UnverifiedUserAttributes, type UseAuthResult, UseAuthenticator, UseAuthenticatorSelector, UserAttributeStep, VerifyUserContext, getTotpCodeURL, unverifiedContactMethodTypes, useAuth, useAuthenticator };
|
|
66
|
-
//# sourceMappingURL=index.d.ts.map
|
|
1
|
+
export { AuthenticatorContext, AuthenticatorProvider } from './context/index.js';
|
|
2
|
+
export { type UseAuthResult, useAuth } from './hooks/useAuth.js';
|
|
3
|
+
export { useAuthenticator } from './hooks/useAuthenticator.js';
|
|
4
|
+
export type * from './types/hooks.js';
|
|
5
|
+
export type * from './types/machines.js';
|
|
6
|
+
export * from './types/user.js';
|
|
7
|
+
export { getTotpCodeURL } from './utils.js';
|