Skip to content

Commit

Permalink
refactor: Make a COOKIE_DOMAIN constant
Browse files Browse the repository at this point in the history
  • Loading branch information
AlpacaFur committed Nov 27, 2023
1 parent 16aae79 commit cc54f31
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
16 changes: 7 additions & 9 deletions packages/api-v2/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from "../../src/student/student.errors";
import { BadToken, InvalidPayload, TokenExpiredError } from "./auth.errors";
import { Throttle } from "@nestjs/throttler";
import { COOKIE_DOMAIN } from "../../src/constants";

@Controller("auth")
export class AuthController {
Expand Down Expand Up @@ -61,14 +62,13 @@ export class AuthController {
const { accessToken } = student;

const isSecure = process.env.NODE_ENV !== "development";
const domain =
process.env.NODE_ENV === "production" ? "graduatenu.com" : "localhost";

// Store JWT token in a cookie
response.cookie("auth_cookie", accessToken, {
httpOnly: true,
sameSite: "strict",
secure: isSecure,
domain,
domain: COOKIE_DOMAIN,
});
if (process.env.NODE_ENV !== "testing") {
await this.emailConfirmationService.sendVerificationLink(
Expand All @@ -93,14 +93,13 @@ export class AuthController {
const { accessToken } = student;

const isSecure = process.env.NODE_ENV !== "development";
const domain =
process.env.NODE_ENV === "production" ? "graduatenu.com" : "localhost";

// Store JWT token in a cookie
response.cookie("auth_cookie", accessToken, {
httpOnly: true,
sameSite: "strict",
secure: isSecure,
domain,
domain: COOKIE_DOMAIN,
});

return student;
Expand Down Expand Up @@ -158,13 +157,12 @@ export class AuthController {
@Res({ passthrough: true }) response: Response
): Promise<void> {
const isSecure = process.env.NODE_ENV !== "development";
const domain =
process.env.NODE_ENV === "production" ? "graduatenu.com" : "localhost";

response.clearCookie("auth_cookie", {
httpOnly: true,
sameSite: "strict",
secure: isSecure,
domain,
domain: COOKIE_DOMAIN,
});
}
}
9 changes: 9 additions & 0 deletions packages/api-v2/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* The root Domain on which all cookies should be set. (See:
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#define_where_cookies_are_sent)
*
* In production, this should be set to "graduatenu.com" which allows
* api.graduatenu.com to set cookies on every other *.graduatenu.com domain.
*/
export const COOKIE_DOMAIN =
process.env.NODE_ENV === "production" ? "graduatenu.com" : "localhost";

0 comments on commit cc54f31

Please sign in to comment.