Expo web doesn't retrieve the token
Hello 👋
I'm trying to use better-auth with expo on the web part.
It is working great on the native part, but when trying to sign-in on the web part, it seems that the token is not retrieved.
First, I had to make a custom storage and not using SecureStore directly, because it is not available on the web (I have used localStorage at the moment for simplicity, but could use mmkv or something else)
Then I use those functions directly in my authClient
After sign-in and doing a first call, I tried to retrieve the cookie with
but the storage is empty.
I see that the seems not to be called on the web, and so is called but return a null value.
Do I need to store the token from sign-in method myself on the web, and retrieve it myself ?
Thank you for the help !
Solution:Jump to solution
I have finally succeed to make it works.
To complete what I have found, I think on the web the set-cookie from the response header should be enough to store the cookie, this is why the "setItem" is skipped on the web.
But this should be documented IMO.
...
7 Replies
Anyone ? Do I open a issue on github ?
Hello @bekacru , I have digged in the code source of the expo plugin and found out that the storage of the cookie is disabled on web part (https://github.com/better-auth/better-auth/blob/main/packages/expo/src/client.ts#L153)
Do you remember why ? Do we have to handle the token ourself or via another plugin ?
Thank you !
GitHub
better-auth/packages/expo/src/client.ts at main · better-auth/bett...
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
Maybe @Ping you know why ?
Sorry, I don't know why
Hey guys, any updates on this?
I'm also trying to implement it in a cross-platform Expo project (web and native), but it's simply not working on the web.
I tried the same approach of creating a custom function to define the storage, but so far, no success…
Solution
I have finally succeed to make it works.
To complete what I have found, I think on the web the set-cookie from the response header should be enough to store the cookie, this is why the "setItem" is skipped on the web.
But this should be documented IMO.
So to make it work :
- Verify that the sign-in response has correct "set-cookie" header, and that cookies are correctly stored (can not be the case for wrong domain, etc...)
- Cookies are httponly, so you can't access it from javascript
- On fetch, don't retrieve the cookie from authClient, but just set "credentials=include", that will retrieve all cookies and send it to your api
@Ping do you think could be a good idea to add this to expo plugin documentation ?