Skip to content

Commit

Permalink
Cherry pick cookie changes from v2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Nov 26, 2024
1 parent 857ac85 commit ce3dd9b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
7 changes: 5 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 19 additions & 5 deletions packages/api/src/express/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const {
UNCHAINED_COOKIE_NAME = 'unchained_token',
UNCHAINED_COOKIE_PATH = '/',
UNCHAINED_COOKIE_DOMAIN,
UNCHAINED_COOKIE_SAMESITE,
UNCHAINED_COOKIE_INSECURE,
} = process.env;

const addContext = async function middlewareWithContext(
Expand Down Expand Up @@ -119,6 +121,18 @@ export const connect = (
) => {
const passport = setupPassport(unchainedAPI);

const name = UNCHAINED_COOKIE_NAME;
const domain = UNCHAINED_COOKIE_DOMAIN;
const path = UNCHAINED_COOKIE_PATH;
const secure = UNCHAINED_COOKIE_INSECURE ? false : true;
const sameSite = ({
none: 'none',
lax: 'lax',
strict: 'strict',
'1': true,
'0': false,
}[UNCHAINED_COOKIE_SAMESITE?.trim()?.toLowerCase()] || false) as boolean | 'none' | 'lax' | 'strict';

expressApp.use(
session({
secret: process.env.UNCHAINED_TOKEN_SECRET,
Expand All @@ -127,14 +141,14 @@ export const connect = (
dbName: db.databaseName,
collectionName: 'sessions',
}),
name: UNCHAINED_COOKIE_NAME,
name,
saveUninitialized: false,
resave: false,
cookie: {
domain: UNCHAINED_COOKIE_DOMAIN,
path: UNCHAINED_COOKIE_PATH,
sameSite: 'none',
secure: true,
domain,
path,
sameSite,
secure,
httpOnly: true,
maxAge: 1000 * 60 * 60 * 24 * 7,
},
Expand Down
26 changes: 20 additions & 6 deletions packages/api/src/fastify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const {
UNCHAINED_COOKIE_NAME = 'unchained_token',
UNCHAINED_COOKIE_PATH = '/',
UNCHAINED_COOKIE_DOMAIN,
NODE_ENV,
UNCHAINED_COOKIE_SAMESITE,
UNCHAINED_COOKIE_INSECURE,
} = process.env;

const middlewareHook = async function middlewareHook(req: any, reply: any) {
Expand Down Expand Up @@ -84,20 +85,33 @@ export const connect = (
db,
}: { graphqlHandler: YogaServerInstance<any, any>; db: mongodb.Db; unchainedAPI: UnchainedCore },
) => {
const cookieName = UNCHAINED_COOKIE_NAME;
const domain = UNCHAINED_COOKIE_DOMAIN;
const path = UNCHAINED_COOKIE_PATH;
const secure = UNCHAINED_COOKIE_INSECURE ? false : true;
const sameSite = ({
none: 'none',
lax: 'lax',
strict: 'strict',
'1': true,
'0': false,
}[UNCHAINED_COOKIE_SAMESITE?.trim()?.toLowerCase()] || false) as boolean | 'none' | 'lax' | 'strict';

fastify.register(fastifyCookie);
fastify.register(fastifySession, {
secret: process.env.UNCHAINED_TOKEN_SECRET,
cookieName: UNCHAINED_COOKIE_NAME,
cookieName,
store: MongoStore.create({
client: (db as any).client,
dbName: db.databaseName,
collectionName: 'sessions',
}),
cookie: {
domain: UNCHAINED_COOKIE_DOMAIN,
httpOnly: Boolean(NODE_ENV === 'production'),
path: UNCHAINED_COOKIE_PATH,
secure: NODE_ENV === 'production',
domain,
httpOnly: true,
path,
secure,
sameSite,
maxAge: 1000 * 60 * 60 * 24 * 7,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ export const configureAssortmentProductsModule = ({
{ upsert: true, returnDocument: 'after' },
);

if (!assortmentProduct) return null;

await emit('ASSORTMENT_ADD_PRODUCT', { assortmentProduct });

if (!options?.skipInvalidation) {
await invalidateCache({ assortmentIds: [assortmentProduct.assortmentId] });
}

return assortmentProduct;
},

delete: async (assortmentProductId, options) => {
Expand Down
9 changes: 6 additions & 3 deletions packages/core-users/src/module/configureUsersModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,12 @@ export const configureUsersModule = async ({
returnDocument: 'after',
});

await emit('USER_UPDATE_BILLING_ADDRESS', {
user: removeConfidentialServiceHashes(user),
});
if (updatedUser) {
await emit('USER_UPDATE_BILLING_ADDRESS', {
user: removeConfidentialServiceHashes(updatedUser),
});
}

return updatedUser;
},

Expand Down

0 comments on commit ce3dd9b

Please sign in to comment.