1 year ago

#208230

test-img

Florian Walther

How to send auth cookie from my Chrome extension to my server, when this cookie was set on a website?

When the user logs through my website (running on localhost:3000), my server (running on localhost:8080) sends back a session cookie and this cookie keeps the user logged in.

app.use(cookieSession({
    name: "my-session",
    secret: process.env.SESSION_COOKIE_SECRET,
    maxAge: 24 * 60 * 60 * 1000,
}))

The website has an accompanying Chrome extension and I want to use the same session cookie to authenticate the user, so they don't have to login in 2 different places.

I can get a handle to that cookie inside my Chrome extension:

chrome.cookies.get({"url": "http://localhost:3000", "name": "my-session"}, cookie => {
    console.log("found cookie: " + cookie.value);
    const response = await fetch(
    'http://localhost:8080/users',
    {
      method: "GET",
      credentials: 'include',
    }
  )
})

The cookie was found but it is not sent to my server when the extension makes the request.

This is inside the background script, so I have no document to attach the cookie to. However, I tried setting document.cookie = cookie (and cookie.value) in the popup script and do the same request from there, but the cookie was still missing from the request.

How can I send my website's cookie through my Chrome extension, preferably from the background script?

javascript

cookies

google-chrome-extension

session-cookies

google-chrome-app

0 Answers

Your Answer

Accepted video resources