-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paths: Forward graphql cookies from the backend to the client
- Loading branch information
Showing
5 changed files
with
81 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
parseSetCookie, | ||
RequestCookie, | ||
} from 'next/dist/compiled/@edge-runtime/cookies'; | ||
import { cookies } from 'next/headers'; | ||
import { NextRequest } from 'next/server'; | ||
|
||
export type BackendCookieOptions = { | ||
prefix: string; | ||
}; | ||
|
||
export function getClientCookies( | ||
req: NextRequest, | ||
opts: BackendCookieOptions | ||
): RequestCookie[] { | ||
const cookieHeader = req.headers.get('cookie'); | ||
if (!cookieHeader) return []; | ||
const prefixedCookies = req.cookies | ||
.getAll() | ||
.filter((cookie) => cookie.name.startsWith(opts.prefix)); | ||
return prefixedCookies.map((cookie) => { | ||
return { | ||
name: cookie.name.slice(opts.prefix.length), | ||
value: cookie.value, | ||
}; | ||
}); | ||
} | ||
|
||
export function getClientCookieAsHeader( | ||
req: NextRequest, | ||
opts: BackendCookieOptions | ||
): string | null { | ||
const cookies = getClientCookies(req, opts); | ||
if (!cookies.length) return null; | ||
return cookies | ||
.map((cookie) => `${cookie.name}=${encodeURIComponent(cookie.value)}`) | ||
.join('; '); | ||
} | ||
|
||
export function forwardSetCookie( | ||
clientReq: NextRequest, | ||
backendResponse: Response, | ||
opts: BackendCookieOptions | ||
) { | ||
const setCookieHeaders = backendResponse.headers.getSetCookie(); | ||
if (!setCookieHeaders.length) return; | ||
|
||
// Pass cookies to the client, modify some of the attributes along the way | ||
setCookieHeaders.forEach((header) => { | ||
const cookie = parseSetCookie(header); | ||
if (!cookie) return; | ||
|
||
cookie.sameSite = 'strict'; | ||
cookie.secure = process.env.NODE_ENV === 'production'; | ||
cookie.name = `${opts.prefix}${cookie.name}`; | ||
cookies().set(cookie); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters