{"version":3,"sources":["../../src/client/session-provider.tsx"],"sourcesContent":["// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\n\"use client\"\nimport { Session } from \"@ory/client-fetch\"\nimport { createContext, useCallback, useEffect, useRef, useState } from \"react\"\nimport { frontendClient } from \"./frontendClient\"\n\ntype SessionState =\n  | {\n      session: Session\n      state: \"authenticated\"\n    }\n  | {\n      state: \"unauthenticated\"\n    }\n  | {\n      state: \"error\"\n      error: Error\n    }\n\n/**\n * Holds the session context data.\n * This context is used to provide the session data to the children of the provider.\n * It is used by the {@link useSession} hook to access the session data.\n */\nexport type SessionContextData = {\n  /**\n   * Whether the session is currently being loaded\n   */\n  isLoading: boolean\n  /**\n   * Whether the session is being loaded for the first time\n   * Never true, if a session was passed to the provider\n   */\n  initialized: boolean\n  /**\n   * The current session or null if the user is not authenticated or an error occurred,\n   * when fetching the session\n   */\n  session: Session | null\n  /**\n   * The error that occurred when fetching the session if any\n   */\n  error: Error | undefined\n  /**\n   * Refetches the session\n   */\n  refetch: () => Promise<void>\n}\n\nexport const SessionContext = createContext<SessionContextData>({\n  session: null,\n  isLoading: false,\n  initialized: false,\n  error: undefined,\n  refetch: async () => {},\n})\n\nexport type SessionProviderProps = {\n  session?: Session | null\n  baseUrl?: string\n} & React.PropsWithChildren\n\n/**\n * A provider that fetches the session from the Ory Network and provides it to the children.\n *\n * To use this provider, wrap your application in it:\n *\n * ```tsx\n * import { SessionProvider } from \"@ory/elements-react\"\n *\n * export default function App() {\n *   return (\n *     <SessionProvider>\n *       <MyApp />\n *     </SessionProvider>\n *   )\n * }\n * ```\n *\n * If you have a session from the server, you can pass it to the provider:\n *\n * ```tsx\n * <SessionProvider session={serverSession}>\n * ```\n *\n * @see {@link useSession}\n * @param props - The provider props\n */\nexport function SessionProvider({\n  session: initialSession,\n  children,\n  baseUrl,\n}: SessionProviderProps) {\n  const initialized = useRef(!!initialSession)\n  const [isLoading, setLoading] = useState(false)\n  const [sessionState, setSessionState] = useState<SessionState | undefined>(\n    () => {\n      if (initialSession) {\n        return {\n          session: initialSession,\n          state: initialSession.active ? \"authenticated\" : \"unauthenticated\",\n        }\n      }\n\n      return undefined\n    },\n  )\n\n  const fetchSession = useCallback(async () => {\n    try {\n      setLoading(true)\n      const session = await frontendClient({\n        forceBaseUrl: baseUrl,\n      }).toSession()\n\n      setSessionState({\n        session,\n        state: session.active ? \"authenticated\" : \"unauthenticated\",\n      })\n    } catch (error) {\n      setSessionState({ state: \"error\", error: error as Error })\n    } finally {\n      setLoading(false)\n    }\n  }, [baseUrl])\n\n  useEffect(() => {\n    if (!initialized.current) {\n      initialized.current = true\n      void fetchSession()\n    }\n  }, [fetchSession])\n\n  return (\n    <SessionContext.Provider\n      value={{\n        error: sessionState?.state === \"error\" ? sessionState.error : undefined,\n        session:\n          sessionState?.state === \"authenticated\" ? sessionState.session : null,\n        isLoading,\n        initialized: initialized.current,\n        refetch: fetchSession,\n      }}\n    >\n      {children}\n    </SessionContext.Provider>\n  )\n}\n"],"mappings":";AAwII;AAnIJ,SAAS,eAAe,aAAa,WAAW,QAAQ,gBAAgB;AACxE,SAAS,sBAAsB;AA6CxB,MAAM,iBAAiB,cAAkC;AAAA,EAC9D,SAAS;AAAA,EACT,WAAW;AAAA,EACX,aAAa;AAAA,EACb,OAAO;AAAA,EACP,SAAS,YAAY;AAAA,EAAC;AACxB,CAAC;AAiCM,SAAS,gBAAgB;AAAA,EAC9B,SAAS;AAAA,EACT;AAAA,EACA;AACF,GAAyB;AACvB,QAAM,cAAc,OAAO,CAAC,CAAC,cAAc;AAC3C,QAAM,CAAC,WAAW,UAAU,IAAI,SAAS,KAAK;AAC9C,QAAM,CAAC,cAAc,eAAe,IAAI;AAAA,IACtC,MAAM;AACJ,UAAI,gBAAgB;AAClB,eAAO;AAAA,UACL,SAAS;AAAA,UACT,OAAO,eAAe,SAAS,kBAAkB;AAAA,QACnD;AAAA,MACF;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AAEA,QAAM,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