-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Expose (and Use) BOTH
headers
and cookies
from `SuperTo…
…kensOutput` After chatting with Rich Harris (creator of Svelte) on sveltejs/kit#8409, it seems that it's better to expose any necessary auth cookies _explicitly_ rather than wrap them in `Headers`. The short reasoning for this is that it provides more options to developers using SSR frameworks. (The previous implementation locked some developers out of using `SuperTokens` in the framework of their choice.) Note that the `SuperTokensOutput` utility class now exposes the `responseHeaders` as a `Map` instead of a `Headers` object in order to support frameworks (or servers) that do not have a proper `Headers` class that they can use. (Node.js doesn't quite support `Headers` out of the box yet. See the MDN compatibility table.)
- Loading branch information
1 parent
f50eabc
commit bffa41d
Showing
6 changed files
with
68 additions
and
36 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 |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import type { LoaderFunction } from "@remix-run/node"; | ||
import { SuperTokensHelpers } from "~/utils/supertokens/index.server"; | ||
import { SuperTokensHelpers, setCookiesFromMap, setHeadersFromMap } from "~/utils/supertokens/index.server"; | ||
import { commonRoutes } from "~/utils/constants"; | ||
|
||
// TODO: Do we need to handle error cases? | ||
export const loader: LoaderFunction = async ({ request }) => { | ||
const responseHeaders = await SuperTokensHelpers.logout(request.headers, request.method.toLowerCase() as "get"); | ||
const { cookies, responseHeaders } = await SuperTokensHelpers.logout( | ||
request.headers, | ||
request.method.toLowerCase() as "get" | ||
); | ||
|
||
responseHeaders.set("Location", commonRoutes.login); | ||
return new Response(null, { status: 302, statusText: "OK", headers: responseHeaders }); | ||
const headers = new Headers({ Location: commonRoutes.login }); | ||
cookies.forEach(setCookiesFromMap(headers)); | ||
responseHeaders.forEach(setHeadersFromMap(headers)); | ||
return new Response(null, { status: 302, statusText: "OK", headers }); | ||
}; |
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,14 @@ | ||
/** A `Map.forEach` helper that sets cookies to the provided `Headers` */ | ||
export function setCookiesFromMap(headers: Headers) { | ||
return function setCookies(value: string): void { | ||
headers.append("Set-Cookie", value); | ||
}; | ||
} | ||
|
||
/** A `Map.forEach` helper that sets headers for the provided `Headers` */ | ||
export function setHeadersFromMap(headers: Headers) { | ||
return function setHeaders(value: string | string[], name: string): void { | ||
if (typeof value === "string") headers.set(name, value); | ||
else value.forEach((v) => headers.append(name, v)); | ||
}; | ||
} |
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