polar
polar copied to clipboard
Docs: unusable example on the better-auth usage meters docs
trafficstars
Description
To be brief, on this docs the example on the image is shown. I don't think this is usable because usage is metered server-side, and the authClient is imported client-side.
Reasoning
- Metered usage isn't like checkout products, you can call
authClient.checkout({productId})on the client and that will call the respective function in the server. - In metered usage you're calling a service of some sort, like
apiClient.get('new-ai-video')and the server must then call the service to create the video and ingest the usage based on that, likeconst { newAiVideo, tokensConsumed } = makeNewVideo(ctx.request); polar.usage.ingest('new-ai-video', { userId: <event.locals.auth.wheresThePolarUserId?>}) - Even if the authClient were to be used server-side, it has no context of the current user
- (I could be wrong on this one but I don't think I am... the auth object server-side needs to get the user from the request token every time, and the authClient object client-side only keeps track of the respective cookie.)
// server-side
if (routeRequiresAuth(event)) {
const session = await auth.api.getSession({
headers: event.request.headers
});
event.locals.user = session.user;
}
// client-side
export const authClient = createAuthClient({
plugins: [polarClient()]
});
Solution
- Add file names to the examples to make clear where they are used
- Update example to show server-side usage ingestion
- Add to example how to get the polar user ID from the server-side auth object (not authClient) to pass it to the
polar.usage.ingest
Notes
- The better auth example I provided applies to all frameworks (it's in the better-auth section of the docs)
- Idk if there are frameworks for which the server-side auth and client-side authClient sync, if this is the case please let me know.
- On sveltekit idk how to get the polar userId from the auth object.