()
| 9 | * This is used to track ownership of chats created by anonymous (non-authenticated) users. |
| 10 | */ |
| 11 | export const getOrCreateAnonymousId = async (): Promise<string> => { |
| 12 | const cookieStore = await cookies(); |
| 13 | const existingId = cookieStore.get(ANONYMOUS_ID_COOKIE_NAME)?.value; |
| 14 | |
| 15 | if (existingId) { |
| 16 | return existingId; |
| 17 | } |
| 18 | |
| 19 | const newId = uuidv4(); |
| 20 | cookieStore.set(ANONYMOUS_ID_COOKIE_NAME, newId, { |
| 21 | httpOnly: true, |
| 22 | secure: process.env.NODE_ENV === 'production', |
| 23 | sameSite: 'lax', |
| 24 | maxAge: COOKIE_MAX_AGE, |
| 25 | path: '/', |
| 26 | }); |
| 27 | |
| 28 | return newId; |
| 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * Gets the anonymous session ID from the cookie if it exists. |
no outgoing calls
no test coverage detected