You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using NextAuth.js in my Next.js app to authenticate users via Google Provider and magic link. This works perfectly in a browser but not so well in a native wrapper using Capacitor JS - https://capacitorjs.com/.
To provide some context, here is my setup for authentication. I'm using MongoDB to store user accounts:
The issue stems from the fact that in Capacitor, the Google Provider opens in a new browser window so the session-token cookie required for authentication wasn't passed back to the app.
I decided to setup deep linking and tried to make it work through the Capacitor Browser plugin (https://capacitorjs.com/docs/apis/browser) using this custom signIn function:
This generated the correct auth URL and opened in the in-app browser, however once authenticated it just loaded the callbackUrl inside the in-app browser rather than deep-linking back to the app.
After much trial and error, I gave up and ended up focusing solely on the email provider route.
The email generated a URL that successfully deep linked into the app so I caught the link containing the email and token using the Capacitor listener appUrlOpen and hit the api/auth/callback/email route manually:
This worked great – On success it reloaded the router and the user is authenticated. It also remained authenticated when I exited and reloaded the app so all seemed to be working perfect.
If I inspect the cookies, I can see that the three cookies are all present as they should be:
However, the issue comes when I logout. The session-token cookie disappears as it should but then if I try to log back in it just reloads the router but remains unauthenticated.
If I reload the app then the session-token cookie sometimes appears again but I still can't login. If I manually delete the cookies and try again then it works so I'm thinking that the session-token cookie isn't getting properly removed on signOut maybe?
For reference, I am obtaining the session info on the client-side using the following way:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am using NextAuth.js in my Next.js app to authenticate users via Google Provider and magic link. This works perfectly in a browser but not so well in a native wrapper using Capacitor JS - https://capacitorjs.com/.
To provide some context, here is my setup for authentication. I'm using MongoDB to store user accounts:
I am triggering the sign in functions via:
The issue stems from the fact that in Capacitor, the Google Provider opens in a new browser window so the
session-token
cookie required for authentication wasn't passed back to the app.I decided to setup deep linking and tried to make it work through the Capacitor Browser plugin (https://capacitorjs.com/docs/apis/browser) using this custom
signIn
function:This generated the correct auth URL and opened in the in-app browser, however once authenticated it just loaded the
callbackUrl
inside the in-app browser rather than deep-linking back to the app.After much trial and error, I gave up and ended up focusing solely on the email provider route.
The email generated a URL that successfully deep linked into the app so I caught the link containing the email and token using the Capacitor listener
appUrlOpen
and hit theapi/auth/callback/email
route manually:This worked great – On success it reloaded the router and the user is authenticated. It also remained authenticated when I exited and reloaded the app so all seemed to be working perfect.
If I inspect the cookies, I can see that the three cookies are all present as they should be:
However, the issue comes when I logout. The
session-token
cookie disappears as it should but then if I try to log back in it just reloads the router but remains unauthenticated.If I reload the app then the
session-token
cookie sometimes appears again but I still can't login. If I manually delete the cookies and try again then it works so I'm thinking that thesession-token
cookie isn't getting properly removed onsignOut
maybe?For reference, I am obtaining the session info on the client-side using the following way:
This issue doesn't happen in Xcode simulator by the way, only when testing on a connected iOS device or when distributed via TestFlight.
Any help or guidance would be greatly appreciated – I've been banging my head against the wall for days on this!
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions