Capacitor don't store cookies


I’ve been pulling my hairs out for a couple hours to try to make capacitor.js works with my Node.js back-end but it seems like there is an issue with cookies:

I have a basic index.html in capacitor, and this as a back-end:

(Express + MongoDB + Passport)

const session = require("cookie-session")
const app = express()

// other code to setup express, passport etc"/validate-auth-code", async (req, res) => {
    const user = await User.findOne({ email: '' })
    if (!user) return res.status(404).send('User not found')
    req.logIn(user, (err) => { })
})'/test-user', async (req, res) => {

When I try this in a browser, it works fine, the session is saved and /test-user show the user.

However, when I send it from capacitor localhost:3000 to /test-user, it shows “undefined”.
/validate-auth-code show the user email and the user being logged in, so the issue seems to be that capacitor is not saving cookies

Am I missing something? How can I save the cookies in capacitor.js ?

(the HTML is just a blank page with a h1 and a fetch request in a script)

Sorry if this is a stupid question, I just started using capacitor a couple of hours ago, I’m really struggling with this.

I personally wouldn’t rely on Cookies in a Capacitor app for authentication. See my post over here - Server set cookies not working on Capacitor iOS - #2 by twestrick

Since that post though, the Capacitor team improved (or released?) the native Cookie plugin which should greatly help those that need to use cookies. Though not mentioned in the documentation, Set-Cookie was added in v4.6.0.

  • cookies: Use Set-Cookie headers to persist cookies (57f8b39)

You might need to use the native HTTP plugin too? I am not sure. Hopefully someone else can chime who has a working app using the Cookie plugin for auth. I am just giving my two cents because I’ve seen so many posts here about Cookie issues where they don’t seem to be reliable for a mobile Capacitor app :slight_smile:

1 Like

Perfect! Ended up using storage + bearer token and it works perfectly! Way easier than using cookies haha

1 Like