Skip to content

Commit

Permalink
fix: set cookie after signing in in electron
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoban committed Dec 26, 2024
1 parent 4b6a0fc commit 55882ca
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions apps/main/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ function bootstrap() {
callback({ cancel: false, requestHeaders: details.requestHeaders })
})

// handle session cookie when sign in with email in electron
session.defaultSession.webRequest.onHeadersReceived(
{
urls: [`${apiURL}/better-auth/sign-in/email?*`],
},
(detail, callback) => {
const { responseHeaders } = detail
if (responseHeaders?.["set-cookie"]) {
const cookies = responseHeaders["set-cookie"] as string[]
cookies.forEach((cookie) => {
const cookieObj = parse(cookie)
Object.keys(cookieObj).forEach((name) => {
const value = cookieObj[name]
mainWindow.webContents.session.cookies.set({
url: apiURL,
name,
value,
secure: true,
httpOnly: true,
domain: new URL(apiURL).hostname,
sameSite: "no_restriction",
})
})
})
}

callback({ cancel: false, responseHeaders })
},
)

app.on("activate", () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
Expand Down

0 comments on commit 55882ca

Please sign in to comment.