From 873a76ce69e39496b3ec5657c1b864fa641879c7 Mon Sep 17 00:00:00 2001 From: Victor Ejike Nwosu Date: Wed, 23 Oct 2024 16:29:08 +0200 Subject: [PATCH] Refactor cookie options --- app/api/graphql/route.ts | 13 +++---------- util/cookies.ts | 7 +++++++ 2 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 util/cookies.ts diff --git a/app/api/graphql/route.ts b/app/api/graphql/route.ts index 1abd7c1..6697b6e 100644 --- a/app/api/graphql/route.ts +++ b/app/api/graphql/route.ts @@ -22,6 +22,7 @@ import { } from '../../../database/users'; import type { Resolvers } from '../../../graphql/graphqlGeneratedTypes'; import type { Animal } from '../../../migrations/00000-createTableAnimals'; +import { secureCookieOptions } from '../../../util/cookies'; export type GraphqlResponseBody = | { @@ -158,11 +159,7 @@ const resolvers: Resolvers = { (await cookies()).set({ name: 'sessionToken', value: session.token, - httpOnly: true, - path: '/', - secure: process.env.NODE_ENV === 'production', - maxAge: 60 * 60 * 24, // This is 24 hours - sameSite: 'lax', // For cross site scripting + ...secureCookieOptions, }); return newUser; @@ -213,11 +210,7 @@ const resolvers: Resolvers = { (await cookies()).set({ name: 'sessionToken', value: session.token, - httpOnly: true, - path: '/', - secure: process.env.NODE_ENV === 'production', - maxAge: 60 * 60 * 24, - sameSite: 'lax', + ...secureCookieOptions, }); return null; diff --git a/util/cookies.ts b/util/cookies.ts new file mode 100644 index 0000000..a9f854c --- /dev/null +++ b/util/cookies.ts @@ -0,0 +1,7 @@ +export const secureCookieOptions = { + httpOnly: true, + path: '/', + secure: process.env.NODE_ENV === 'production', + maxAge: 60 * 60 * 24, // This is 24 hours + sameSite: 'lax', // For cross site scripting +} as const;