How to set Bearer token in Next.js
Hello Everyone,
But I'm getting this error:
I believe it's because auth-client.ts is being run on the server side, where localStorage is unavailable.
Any help is appreciated!
8 Replies
You can use bearerr plugin for handling bearer authentication
Bearer Token Authentication | Better Auth
Authenticate API requests using Bearer tokens instead of browser cookies
@KiNFiSH
im using Next js But I'm getting this error:
ReferenceError: localStorage is not defined
can you please try with for safe ignoring -
plus it is react package which means it is bundled with client files and it is not sth that should run on the server instead run on browser/client
actually i was getting the same issue on sveltekit, and in the docs it says to put auth-client under lib/ so maybe it should be updated on the docs:
https://www.better-auth.com/docs/integrations/svelte-kit#create-a-client
SvelteKit Integration | Better Auth
Integrate Better Auth with SvelteKit.
@KiNFiSH
Hi,
I try the safe ignoring not working but it not throwing error ReferenceError: localStorage is not defined
I also try the "use client" in auth-client.ts file in next js
Note: the auth-client.ts file is under the /lib folder
So does that mean you are seeing the log on server ?
It probably just means you're using the authClient in a server enviroment