Replies: 2 comments
-
I've found a rather tricky approach, customize the // Path: src/main.tsx
import { LogtoProvider, LogtoConfig } from "@logto/react";
import LogtoClient from "@logto/browser";
// Store the global LogtoClient instance
let globalLogtoClientInstance: LogtoClient | null = null;
// Custom LogtoClient class to store the instance
class CustomLogtoClient extends LogtoClient {
constructor(config: LogtoConfig) {
super(config);
globalLogtoClientInstance = this;
}
}
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<LogtoProvider config={config} LogtoClientClass={CustomLogtoClient}>
<RouterProvider router={router} />
</LogtoProvider>
</React.StrictMode>
); // Path: src/services/network.ts
class NetworkService {
async fetchSomething() {
// Get the access token from the global LogtoClient instance
const accessToken = globalLogtoClientInstance?.getAccessToken();
// ...
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Your approach is a bit tricky, but it works. Can you tell me why do you want to do this in the first place? The |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to add an auth header to my fetch function but I'm finding it difficult.
I could use
useLogto()
but this is awkward I want to do something likeOr is there any way I can get the
LogtoClient
instance in React to call itsgetAccessToken()
method in my network library?See also:
Beta Was this translation helpful? Give feedback.
All reactions