Usage with Ionic Capacitor #4446
Replies: 4 comments 22 replies
-
Great finding @creativiii are you still doing it this way? By any chance did you try with Google Sign in? Will this work for iOS as well? Thanks! |
Beta Was this translation helpful? Give feedback.
-
Example repository with comments on how to make Next Auth and Capacitor work together here: https://github.com/choutkamartin/next-auth-capacitor |
Beta Was this translation helpful? Give feedback.
-
So this method won't work with iOS? Only Android? |
Beta Was this translation helpful? Give feedback.
-
I found some of this useful - thanks - but my own solution ended up somewhat different. It feels a bit like a lash up, but it seems to work and be reliable. I'll try to summarize it here anyway , in case it helps anyone else. I started with Sveltekit and was loathe to adopt an Ionic approach. I was a bit surprised there wasn't an "off the shelf" solution for using OpenAuth across different platforms, but it seems there isn't. I played a lot with auth.js and also Lucia, which trades complexity for flexibility. I wanted to make auth.js work as I like simple. I started with the standard "in app browser" but I couldn't make that work, and eventually found an unofficial version which worked straight away. My problem was broadly to authenticate a sveltekit API using auth.js, such that I can authenticate using OpenAuth on web or mobile.
Then, when the user wants to sign in on Capacitor...
At this point I have a session token, and the internet server has a set of auth.js cookies, and my back end database server has a record of the session. From here on I can use the |
Beta Was this translation helpful? Give feedback.
-
Question 💬
This isn't as much of a question as more of a list of findings/tutorial on how to get this (kind-of) working, it's already been done for React Native #569 so hopefully it's okay for me to post this.
The big issue I found while implementing this is how Android's Browser (which uses Chrome Custom Tabs) handles cookies.
Using the built in browser didn't work
Initially I was hoping I'd be able to simply open the Browser, log-in from there and then be sent back to my app with my cookies set. Any cookie saved within Chrome Custom Tabs will be saved on the systems' browser and since Capacitor does not share cookies with Chrome, that session will not be available in your Capacitor app.
Using the Rest API almost worked
The second thing I tried was to implement the oAuth flow entirely using the Rest API. My theory was to:
callback
endpoint to get session dataWith the API I was able to get an oAuth url to authorise my app, however the issue appeared when redirecting after authorising.
As far as I can tell the
/api/auth/signin/:provider
endpoint will generate an oAuth URL that will always redirect to/api/auth/callback/:provider
. ThecallbackURL
option is just a page that Next Auth will redirect you after. So there is technically no way to redirect to the app with the code.Workaround using the In-App Browser plugin
I was almost about to give up when I found the Cordova In App Browser plugin. In App Browser essentially opens another webview on top of your app, its cookies are shared with the Capacitor webview which means any authentication done here will also work in Capacitor.
After some trial and error I ended up getting the flow to work with the following code, added comments for clarity.
This doesn't look super legit, and since the IAP doesn't actually have access to any Cookies from Chrome, the user will be asked to login again before authorising the app.
That said, it does work. It's pretty much the only solution I could find short of reimplementing the entire flow.
Note:
SessionProvider
won't immediately have access to the session. The docs say to callgetSession
to force an update, but it didn't work for me. At the moment I'm forcing a page reload after login to make sure everything is synced properly.Would love to know if anyone has better solutions, let me know if this has helped you.
How to reproduce ☕️
N/A
Contributing 🙌🏽
Yes, I am willing to help answer this question in a PR
Beta Was this translation helpful? Give feedback.
All reactions