{"version":3,"file":"StytchContext.mjs","sources":["../../../../../../../../../web/src/ui/react/b2c/StytchContext.tsx"],"sourcesContent":["'use client';\n\nimport { PermissionsMap, Session, StytchProjectConfigurationInput, User } from '@stytch/core/public';\nimport { mergeWithStableProps } from '@stytch/js-utils';\nimport React, {\n  ComponentType,\n  createContext,\n  ReactNode,\n  useCallback,\n  useContext,\n  useEffect,\n  useMemo,\n  useRef,\n} from 'react';\n\nimport { StytchClient } from '../../../StytchClient';\nimport { isStytchSSRProxy } from '../bindings/StytchSSRProxy';\nimport { useAsyncState } from '../utils/async';\nimport { noProviderError, providerMustBeUniqueError } from '../utils/errors';\nimport { invariant } from '../utils/invariant';\n\n// There's no user facing strings in this file\n/* eslint-disable lingui/no-unlocalized-strings */\n\ntype StytchContext<TProjectConfiguration extends StytchProjectConfigurationInput> =\n  | {\n      isMounted: false;\n    }\n  | {\n      isMounted: true;\n      client: StytchClient<TProjectConfiguration>;\n    };\n\ntype SWRUserUninitialized = {\n  /**\n   * Either the active {@link User} object, or null if the user is not logged in.\n   */\n  user: null;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: false;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: false;\n};\n\ntype SWRUserInitialized = {\n  /**\n   * Either the active {@link User} object, or null if the user is not logged in.\n   */\n  user: User | null;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: boolean;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: true;\n};\n\ntype SWRUser<TAlwaysInitialized extends boolean = boolean> = TAlwaysInitialized extends true\n  ? SWRUserInitialized\n  : SWRUserInitialized | SWRUserUninitialized;\n\nconst initialUser: SWRUserUninitialized = {\n  user: null,\n  fromCache: false,\n  isInitialized: false,\n};\n\ntype SWRSessionUninitialized = {\n  /**\n   * Either the active {@link Session} object, or null if the user is not logged in.\n   */\n  session: null;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: false;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: false;\n};\n\ntype SWRSessionInitialized = {\n  /**\n   * Either the active {@link Session} object, or null if the user is not logged in.\n   */\n  session: Session | null;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: boolean;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: true;\n};\n\ntype SWRSession<TAlwaysInitialized extends boolean = boolean> = TAlwaysInitialized extends true\n  ? SWRSessionInitialized\n  : SWRSessionInitialized | SWRSessionUninitialized;\n\nconst initialSession: SWRSessionUninitialized = {\n  session: null,\n  fromCache: false,\n  isInitialized: false,\n};\n\nconst StytchContext = createContext<StytchContext<StytchProjectConfigurationInput>>({ isMounted: false });\nconst StytchUserContext = createContext<SWRUser>(initialUser);\nconst StytchSessionContext = createContext<SWRSession>(initialSession);\n\nexport const useIsMounted__INTERNAL = (): boolean => useContext(StytchContext).isMounted;\n\n/**\n * Returns the active User.\n * The Stytch SDKs are used for client-side authentication and session management.\n * Check the isInitialized property to determine if the SDK has completed initialization.\n * Check the fromCache property to determine if the user data is from persistent storage.\n * @example\n * const {user, isInitialized, fromCache} = useStytchUser();\n * if (!isInitialized) {\n *     return <p>Loading...</p>;\n * }\n * return (<h1>Welcome, {user.name.first_name}</h1>);\n */\nexport const useStytchUser = <TAssumeHydrated extends boolean = false>(): SWRUser<TAssumeHydrated> => {\n  invariant(useIsMounted__INTERNAL(), noProviderError('useStytchUser'));\n  return useContext(StytchUserContext) as SWRUser<TAssumeHydrated>;\n};\n\n/**\n * Returns the active user's Stytch session.\n * The Stytch SDKs are used for client-side authentication and session management.\n * Check the isInitialized property to determine if the SDK has completed initialization.\n * Check the fromCache property to determine if the session data is from persistent storage.\n * @example\n * const {session, isInitialized, fromCache} = useStytchSession();\n * useEffect(() => {\n *   if (!isInitialized) {\n *     return;\n *   }\n *   if (!session) {\n *     router.replace('/login')\n *   }\n * }, [session, isInitialized]);\n */\nexport const useStytchSession = <TAssumeHydrated extends boolean = false>(): SWRSession<TAssumeHydrated> => {\n  invariant(useIsMounted__INTERNAL(), noProviderError('useStytchSession'));\n  return useContext(StytchSessionContext) as SWRSession<TAssumeHydrated>;\n};\n\n/**\n * Returns the Stytch client stored in the Stytch context.\n *\n * @example\n * const stytch = useStytch();\n * useEffect(() => {\n *   stytch.magicLinks.authenticate('...')\n * }, [stytch]);\n */\nexport const useStytch = <\n  TProjectConfiguration extends StytchProjectConfigurationInput = Stytch.DefaultProjectConfiguration,\n>(): StytchClient<TProjectConfiguration> => {\n  const ctx = useContext(StytchContext);\n  invariant(ctx.isMounted, noProviderError('useStytch'));\n  return ctx.client as StytchClient<TProjectConfiguration>;\n};\n\nexport const withStytch = <\n  T extends object,\n  TProjectConfiguration extends StytchProjectConfigurationInput = Stytch.DefaultProjectConfiguration,\n>(\n  Component: ComponentType<T & { stytch: StytchClient<TProjectConfiguration> }>,\n): ComponentType<T> => {\n  const WithStytch: ComponentType<T> = (props) => {\n    invariant(useIsMounted__INTERNAL(), noProviderError('withStytch'));\n    return <Component {...props} stytch={useStytch()} />;\n  };\n  WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;\n  return WithStytch;\n};\n\nexport const withStytchUser = <T extends object, TAssumeHydrated extends boolean = false>(\n  Component: ComponentType<\n    T & { stytchUser: User | null; stytchUserIsInitialized: boolean; stytchUserIsFromCache: boolean }\n  >,\n): ComponentType<T> => {\n  const WithStytchUser: ComponentType<T> = (props) => {\n    invariant(useIsMounted__INTERNAL(), noProviderError('withStytchUser'));\n    const { user, isInitialized, fromCache } = useStytchUser<TAssumeHydrated>();\n    return (\n      <Component\n        {...props}\n        stytchUser={user}\n        stytchUserIsInitialized={isInitialized}\n        stytchUserIsFromCache={fromCache}\n      />\n    );\n  };\n  WithStytchUser.displayName = `withStytchUser(${Component.displayName || Component.name || 'Component'})`;\n  return WithStytchUser;\n};\n\nexport const withStytchSession = <T extends object, TAssumeHydrated extends boolean = false>(\n  Component: ComponentType<\n    T & { stytchSession: Session | null; stytchSessionIsInitialized: boolean; stytchSessionIsFromCache: boolean }\n  >,\n): ComponentType<T> => {\n  const WithStytchSession: ComponentType<T> = (props) => {\n    invariant(useIsMounted__INTERNAL(), noProviderError('withStytchSession'));\n    const { session, isInitialized, fromCache } = useStytchSession<TAssumeHydrated>();\n    return (\n      <Component\n        {...props}\n        stytchSession={session}\n        stytchSessionIsInitialized={isInitialized}\n        stytchSessionIsFromCache={fromCache}\n      />\n    );\n  };\n  WithStytchSession.displayName = `withStytchSession(${Component.displayName || Component.name || 'Component'})`;\n  return WithStytchSession;\n};\n\ntype SWRIsAuthorizedUninitialized = {\n  /**\n   * Whether the logged-in user is allowed to perform the specified action on the specified resource.\n   */\n  isAuthorized: false;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: false;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: false;\n};\n\ntype SWRIsAuthorizedInitialized = {\n  /**\n   * Whether the logged-in user is allowed to perform the specified action on the specified resource.\n   */\n  isAuthorized: boolean;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: boolean;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: boolean;\n};\n\ntype SWRIsAuthorized<TAlwaysInitialized extends boolean> = TAlwaysInitialized extends true\n  ? SWRIsAuthorizedInitialized\n  : SWRIsAuthorizedInitialized | SWRIsAuthorizedUninitialized;\n\n/**\n * Determines whether the logged-in user is allowed to perform the specified action on the specified resource.\n * Returns `true` if the user can perform the action, `false` otherwise.\n *\n * If the user is not logged in, this method will always return false.\n * If the resource or action provided are not valid for the configured RBAC policy, this method will return false.\n *\n * Remember - authorization checks for sensitive actions should always occur on the backend as well.\n * @example\n * const { isAuthorized } = useStytchIsAuthorized('documents', 'edit');\n * return <button disabled={!isAuthorized}>Edit</button>\n */\nexport const useStytchIsAuthorized = <TAssumeHydrated extends boolean = false>(\n  resourceId: string,\n  action: string,\n): SWRIsAuthorized<TAssumeHydrated> => {\n  invariant(useIsMounted__INTERNAL(), noProviderError('useStytchIsAuthorized', 'StytchProvider'));\n  const client = useStytch();\n  const { user } = useStytchUser();\n  const [isAuthorized, setIsAuthorized] = useAsyncState<SWRIsAuthorized<boolean>>({\n    isInitialized: false,\n    fromCache: false,\n    isAuthorized: false,\n  });\n\n  useEffect(() => {\n    if (isStytchSSRProxy(client)) {\n      return;\n    }\n\n    setIsAuthorized({\n      isInitialized: true,\n      fromCache: true,\n      isAuthorized: client.rbac.isAuthorizedSync(resourceId, action),\n    });\n  }, [action, client, resourceId, setIsAuthorized]);\n\n  useEffect(() => {\n    if (isStytchSSRProxy(client)) {\n      return;\n    }\n    client.rbac.isAuthorized(resourceId, action).then((isAuthorized) => {\n      setIsAuthorized({ isAuthorized, fromCache: false, isInitialized: true });\n    });\n  }, [client, user?.roles, resourceId, action, setIsAuthorized]);\n\n  return isAuthorized;\n};\n\ntype permissionsLoaded<Permissions extends Record<string, string>> =\n  | {\n      loaded: false;\n      value: null;\n    }\n  | {\n      loaded: true;\n      value: PermissionsMap<Permissions>;\n    };\n\n/**\n * Wrap your component with this HOC in order to receive the permissions for the logged-in user.\n * Evaluates all permissions granted to the logged-in user.\n * Returns a Record<RoleId, Record<Action, boolean>> response indicating the user's permissions.\n * Each boolean will be `true` if the user can perform the action, `false` otherwise.\n *\n * If the user is not logged in, all values will be false.\n *\n * Remember - authorization checks for sensitive actions should always occur on the backend as well.\n * @example\n * type Permissions = {\n *   document: 'create' | 'read' | 'write\n *   image: 'create' | 'read'\n * }\n *\n * const MyComponent = (props) => {\n *   const canEditDocuments = props.stytchPermissions.document.edit;\n *   const canReadImages = props.stytchPermissions.image.read;\n * }\n * return withStytchPermissions<Permissions>(MyComponent)\n */\nexport const withStytchPermissions = <Permissions extends Record<string, string>, T extends object>(\n  Component: ComponentType<T & { stytchPermissions: PermissionsMap<Permissions> }>,\n): ComponentType<T> => {\n  const WithStytchPermissions: ComponentType<T> = (props) => {\n    invariant(useIsMounted__INTERNAL(), noProviderError('useRBACPermissions', 'StytchProvider'));\n    const client = useStytch();\n    const { user } = useStytchUser();\n    const [permissions, setPermissions] = useAsyncState<permissionsLoaded<Permissions>>({ loaded: false, value: null });\n\n    useEffect(() => {\n      client.rbac.allPermissions<Permissions>().then((permissions) => {\n        setPermissions({ loaded: true, value: permissions });\n      });\n    }, [client, user?.roles, setPermissions]);\n\n    if (!permissions.loaded) {\n      return null;\n    }\n    return <Component {...props} stytchPermissions={permissions.value} />;\n  };\n  WithStytchPermissions.displayName = `withStytchPermissions(${\n    Component.displayName || Component.name || 'Component'\n  })`;\n  return WithStytchPermissions;\n};\n\nexport interface StytchProviderProps<\n  TProjectConfiguration extends StytchProjectConfigurationInput = Stytch.DefaultProjectConfiguration,\n> {\n  /**\n   * A Stytch client instance, created using either {@link createStytchHeadlessClient} or {@link createStytchClient}\n   */\n  stytch: StytchClient<TProjectConfiguration>;\n  /**\n   * When true, the provider will assume that the component will only be\n   * rendered in a browser environment, either in a single-page application or\n   * after completing hydration of a server-rendered application. This allows\n   * cached values to be retrieved from the browser on the first render, meaning\n   * that the `isInitialized` value returned from Stytch hooks will be `true`\n   * starting from the first render.\n   *\n   * When `false`, the provider will defer initialization until after the first\n   * render, and `isInitialized` will initially be `false`.\n   *\n   * This value defaults to `false` in `@stytch/nextjs`.\n   */\n  assumeHydrated?: boolean;\n  children?: ReactNode;\n}\n\n/**\n * The Stytch Context Provider.\n * Wrap your application with this component in order to use Stytch everywhere in your app.\n * @example\n * const stytch = createStytchClient('public-token-<find yours in the stytch dashboard>')\n *\n * return (\n *   <StytchProvider stytch={stytch}>\n *     <App />\n *   </StytchProvider>\n * )\n */\nexport const StytchProvider = <\n  TProjectConfiguration extends StytchProjectConfigurationInput = Stytch.DefaultProjectConfiguration,\n>({\n  stytch,\n  children,\n  assumeHydrated = false,\n}: StytchProviderProps<TProjectConfiguration>) => {\n  invariant(!useIsMounted__INTERNAL(), providerMustBeUniqueError);\n  invariant(\n    !assumeHydrated || typeof window !== 'undefined',\n    'The `assumeHydrated` prop must be set to `false` when using StytchProvider in a server environment.',\n  );\n\n  const ctx = useMemo(() => ({ client: stytch, isMounted: true }), [stytch]);\n\n  type ClientState<TAlwaysInitialized extends boolean> = {\n    session: SWRSession<TAlwaysInitialized>;\n    user: SWRUser<TAlwaysInitialized>;\n  };\n\n  const getHydratedState = useCallback(() => {\n    return {\n      session: { ...stytch.session.getInfo(), isInitialized: true },\n      user: { ...stytch.user.getInfo(), isInitialized: true },\n    } satisfies ClientState<true>;\n  }, [stytch]);\n\n  const getInitialState = () => {\n    return {\n      session: initialSession,\n      user: initialUser,\n    } satisfies ClientState<false>;\n  };\n\n  const [{ user, session }, setClientState] = useAsyncState<ClientState<boolean>>(() =>\n    assumeHydrated ? getHydratedState() : (getInitialState() as ClientState<boolean>),\n  );\n\n  // Store the initial value of `assumeHydrated` in a ref, because it is\n  // logically only relevant for the first render\n  const assumeHydratedRef = useRef(assumeHydrated);\n  useEffect(() => {\n    if (isStytchSSRProxy(stytch)) {\n      return;\n    }\n\n    const updateState = () => {\n      setClientState((oldState) => mergeWithStableProps(oldState, getHydratedState()));\n    };\n\n    if (!assumeHydratedRef.current) {\n      updateState();\n    }\n\n    return stytch.onStateChange(updateState);\n  }, [getHydratedState, setClientState, stytch]);\n\n  return (\n    <StytchContext.Provider value={ctx}>\n      <StytchUserContext.Provider value={user}>\n        <StytchSessionContext.Provider value={session}>{children}</StytchSessionContext.Provider>\n      </StytchUserContext.Provider>\n    </StytchContext.Provider>\n  );\n};\n"],"names":["initialUser","user","fromCache","isInitialized","initialSession","session","StytchContext","createContext","isMounted","StytchUserContext","StytchSessionContext","useIsMounted__INTERNAL","useContext","useStytchUser","invariant","noProviderError","useStytchSession","useStytch","ctx","client","withStytch","Component","WithStytch","props","React","stytch","displayName","name","withStytchUser","WithStytchUser","stytchUser","stytchUserIsInitialized","stytchUserIsFromCache","withStytchSession","WithStytchSession","stytchSession","stytchSessionIsInitialized","stytchSessionIsFromCache","useStytchIsAuthorized","resourceId","action","isAuthorized","setIsAuthorized","useAsyncState","useEffect","isStytchSSRProxy","rbac","isAuthorizedSync","then","roles","withStytchPermissions","WithStytchPermissions","permissions","setPermissions","loaded","value","allPermissions","stytchPermissions","StytchProvider","children","assumeHydrated","providerMustBeUniqueError","window","useMemo","getHydratedState","useCallback","getInfo","getInitialState","setClientState","assumeHydratedRef","useRef","updateState","oldState","mergeWithStableProps","current","onStateChange","Provider"],"mappings":";;;;;;;AAmEA,MAAMA,WAAAA,GAAoC;IACxCC,IAAAA,EAAM,IAAA;IACNC,SAAAA,EAAW,KAAA;IACXC,aAAAA,EAAe;AACjB,CAAA;AAoCA,MAAMC,cAAAA,GAA0C;IAC9CC,OAAAA,EAAS,IAAA;IACTH,SAAAA,EAAW,KAAA;IACXC,aAAAA,EAAe;AACjB,CAAA;AAEA,MAAMG,8BAAgBC,aAAAA,CAA8D;IAAEC,SAAAA,EAAW;AAAM,CAAA,CAAA;AACvG,MAAMC,kCAAoBF,aAAAA,CAAuBP,WAAAA,CAAAA;AACjD,MAAMU,qCAAuBH,aAAAA,CAA0BH,cAAAA,CAAAA;MAE1CO,sBAAAA,GAAyB,IAAeC,UAAAA,CAAWN,aAAAA,CAAAA,CAAeE;AAE/E;;;;;;;;;;;UAYaK,aAAAA,GAAgB,IAAA;AAC3BC,IAAAA,SAAAA,CAAUH,0BAA0BI,eAAAA,CAAgB,eAAA,CAAA,CAAA;AACpD,IAAA,OAAOH,UAAAA,CAAWH,iBAAAA,CAAAA;AACpB;AAEA;;;;;;;;;;;;;;;UAgBaO,gBAAAA,GAAmB,IAAA;AAC9BF,IAAAA,SAAAA,CAAUH,0BAA0BI,eAAAA,CAAgB,kBAAA,CAAA,CAAA;AACpD,IAAA,OAAOH,UAAAA,CAAWF,oBAAAA,CAAAA;AACpB;AAEA;;;;;;;;UASaO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