Skip to content

adjust to new cookie scheme #7946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const KCL_DEFAULT_COLOR = `#3c73ff`
export const SETTINGS_FILE_NAME = 'settings.toml'
export const PROJECT_SETTINGS_FILE_NAME = 'project.toml'
export const COOKIE_NAME = '__Secure-next-auth.session-token'
export const COOKIE_NAME_DEV = '__Secure-session-token-dev'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is just a draft, but I don't want to see a literal "dev" propagate any further in this codebase. The term is far too overloaded. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would preview work? this is mostly targeting api.dev.zoo.dev so not sure what to call it 😓

Copy link
Contributor

@jacebrowning jacebrowning Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to include the domain of the environment in the cookie name per our discussion on Slack, so something like __secure-session-token-dev.zoo.dev.

export const TELEMETRY_FILE_NAME = 'boot.txt'
export const TELEMETRY_RAW_FILE_NAME = 'raw-metrics.txt'
export const ENVIRONMENT_FILE_NAME = 'environment.txt'
Expand Down
16 changes: 5 additions & 11 deletions src/lib/promptToEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import { diffLines } from 'diff'
import toast from 'react-hot-toast'
import type { TextToCadMultiFileIteration_type } from '@kittycad/lib/dist/types/src/models'
import { getCookie, TOKEN_PERSIST_KEY } from '@src/machines/authMachine'
import { APP_DOWNLOAD_PATH, COOKIE_NAME } from '@src/lib/constants'
import { APP_DOWNLOAD_PATH } from '@src/lib/constants'
import { isDesktop } from '@src/lib/isDesktop'
import { openExternalBrowserIfDesktop } from '@src/lib/openWindow'
import { ActionButton } from '@src/components/ActionButton'
Expand Down Expand Up @@ -112,14 +111,9 @@
selections: Selections | null
projectFiles: FileMeta[]
projectName: string
token?: string
token: string
artifactGraph: ArtifactGraph
}) {
const _token =
token && token !== ''
? token
: getCookie(COOKIE_NAME) || localStorage?.getItem(TOKEN_PERSIST_KEY) || ''

const kclFilesMap: KclFileMetaMap = {}
const endPointFiles: KittyCadLibFile[] = []
projectFiles.forEach((file) => {
Expand Down Expand Up @@ -150,7 +144,7 @@
kcl_version: kclManager.kclVersion,
},
endPointFiles,
_token
token
)
}

Expand Down Expand Up @@ -296,7 +290,7 @@
kcl_version: kclManager.kclVersion,
},
endPointFiles,
_token

Check failure on line 293 in src/lib/promptToEdit.tsx

View workflow job for this annotation

GitHub Actions / npm-tsc

Cannot find name '_token'. Did you mean 'token'?
)
}

Expand Down Expand Up @@ -328,7 +322,7 @@
prompt: string
selections: Selections
projectFiles: FileMeta[]
token?: string
token: string
projectName: string
artifactGraph: ArtifactGraph
}): Promise<Models['TextToCadMultiFileIteration_type'] | Error> {
Expand Down Expand Up @@ -413,7 +407,7 @@
prompt: string
selections: Selections
projectFiles: FileMeta[]
token?: string
token: string
artifactGraph: ArtifactGraph
projectName: string
filePath: string | undefined
Expand Down
17 changes: 15 additions & 2 deletions src/machines/authMachine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { Models } from '@kittycad/lib'
import env, { updateEnvironment, updateEnvironmentPool } from '@src/env'
import { assign, fromPromise, setup } from 'xstate'
import { COOKIE_NAME, OAUTH2_DEVICE_CLIENT_ID } from '@src/lib/constants'
import {
COOKIE_NAME,
COOKIE_NAME_DEV,
COOKIE_NAME_LEGACY,

Check failure on line 7 in src/machines/authMachine.ts

View workflow job for this annotation

GitHub Actions / npm-tsc

'"@src/lib/constants"' has no exported member named 'COOKIE_NAME_LEGACY'. Did you mean 'COOKIE_NAME_DEV'?

Check failure on line 7 in src/machines/authMachine.ts

View workflow job for this annotation

GitHub Actions / npm-lint

'COOKIE_NAME_LEGACY' is defined but never used. Allowed unused vars must match /^_/u
OAUTH2_DEVICE_CLIENT_ID,
} from '@src/lib/constants'
import {
getUser as getUserDesktop,
listAllEnvironments,
Expand Down Expand Up @@ -38,7 +43,7 @@
/**
* Determine which token do we have persisted to initialize the auth machine
*/
const persistedCookie = getCookie(COOKIE_NAME)

Check failure on line 46 in src/machines/authMachine.ts

View workflow job for this annotation

GitHub Actions / npm-tsc

Expected 0 arguments, but got 1.
const persistedDevToken = env().VITE_KITTYCAD_API_TOKEN
export const persistedToken = persistedDevToken || persistedCookie || ''
console.log('Initial persisted token')
Expand Down Expand Up @@ -223,11 +228,19 @@
}
}

export function getCookie(cname: string): string | null {
export function getCookie(): string | null {
if (isDesktop()) {
return null
}

if (window.electron.process.env.NODE_ENV === 'production') {
return getCookieByName(COOKIE_NAME)
} else {
return getCookieByName(COOKIE_NAME_DEV)
}
}

function getCookieByName(cname: string): string | null {
let name = cname + '='
let decodedCookie = decodeURIComponent(document.cookie)
let ca = decodedCookie.split(';')
Expand Down Expand Up @@ -258,7 +271,7 @@

// Find possible tokens
const inputToken = input.token && input.token !== '' ? input.token : ''
const cookieToken = getCookie(COOKIE_NAME)

Check failure on line 274 in src/machines/authMachine.ts

View workflow job for this annotation

GitHub Actions / npm-tsc

Expected 0 arguments, but got 1.
const fileToken =
isDesktop() && environmentName
? await readEnvironmentConfigurationToken(environmentName)
Expand Down
Loading