Skip to content

Commit

Permalink
Merge pull request #184 from ystv/mia/cookie-fix
Browse files Browse the repository at this point in the history
Move COOKIE_NAME to auth core and rename
  • Loading branch information
archessmn authored Oct 20, 2024
2 parents 26a23e4 + 062aadd commit 9b536b8
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 22 deletions.
5 changes: 3 additions & 2 deletions app/login/google/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { cookieName, loginOrCreateUserGoogle } from "@/lib/auth/server";
import { loginOrCreateUserGoogle } from "@/lib/auth/server";
import { COOKIE_NAME } from "@/lib/auth/core";
import { env } from "@/lib/env";
import { NextRequest, NextResponse } from "next/server";

export const dynamic = "force-dynamic";

export async function POST(req: NextRequest): Promise<NextResponse> {
const cookies = req.cookies;
const redirect = cookies.get(`${cookieName}.redirect`);
const redirect = cookies.get(`${COOKIE_NAME}.redirect`);

const dataRaw = await req.formData();
const idToken = dataRaw.get("credential");
Expand Down
6 changes: 3 additions & 3 deletions app/login/google/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cookieName } from "@/lib/auth/server";
import { COOKIE_NAME } from "@/lib/auth/core";
import { env } from "@/lib/env";
import { randomUUID } from "crypto";
import { NextRequest, NextResponse } from "next/server";
Expand All @@ -19,11 +19,11 @@ export async function GET(req: NextRequest) {
const res = NextResponse.redirect(googleLoginURI);

if (redirect !== null) {
res.cookies.set(`${cookieName}.redirect`, redirect, {
res.cookies.set(`${COOKIE_NAME}.redirect`, redirect, {
domain: env.COOKIE_DOMAIN,
});
} else {
res.cookies.set(`${cookieName}.redirect`, "", {
res.cookies.set(`${COOKIE_NAME}.redirect`, "", {
domain: env.COOKIE_DOMAIN,
maxAge: 0,
});
Expand Down
4 changes: 2 additions & 2 deletions app/login/slack/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import { getSlackUserInfo } from "@/lib/auth/slack";
import {
cookieName,
COOKIE_NAME,
getCurrentUserOrNull,
loginOrCreateUserSlack,
} from "@/lib/auth/server";
Expand All @@ -11,7 +11,7 @@ export const dynamic = "force-dynamic";

export async function GET(req: NextRequest): Promise<NextResponse> {
const cookies = req.cookies;
const redirect = cookies.get(`${cookieName}.redirect`);
const redirect = cookies.get(`${COOKIE_NAME}.redirect`);

const searchParams = req.nextUrl.searchParams;
const code = searchParams.get("code");
Expand Down
6 changes: 3 additions & 3 deletions app/login/slack/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cookieName } from "@/lib/auth/server";
import { COOKIE_NAME } from "@/lib/auth/core";
import { env } from "@/lib/env";
import { NextRequest, NextResponse } from "next/server";

Expand All @@ -14,11 +14,11 @@ export async function GET(req: NextRequest) {
const res = NextResponse.redirect(slackLoginURI);

if (redirect !== null) {
res.cookies.set(`${cookieName}.redirect`, redirect, {
res.cookies.set(`${COOKIE_NAME}.redirect`, redirect, {
domain: env.COOKIE_DOMAIN,
});
} else {
res.cookies.set(`${cookieName}.redirect`, "", {
res.cookies.set(`${COOKIE_NAME}.redirect`, "", {
domain: env.COOKIE_DOMAIN,
maxAge: 0,
});
Expand Down
4 changes: 2 additions & 2 deletions components/SignoutButton/actions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use server";

import { wrapServerAction } from "@/lib/actions";
import { cookieName } from "@/lib/auth/server";
import { COOKIE_NAME } from "@/lib/auth/core";
import { env } from "@/lib/env";
import { cookies } from "next/headers";

export const signOut = wrapServerAction("signOut", async function signOut() {
cookies().set(cookieName, "", {
cookies().set(COOKIE_NAME, "", {
maxAge: 0,
domain: env.COOKIE_DOMAIN,
});
Expand Down
4 changes: 2 additions & 2 deletions components/google/actions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use server";

import { wrapServerAction } from "@/lib/actions";
import { cookieName } from "@/lib/auth/server";
import { COOKIE_NAME } from "@/lib/auth/core";
import { env } from "@/lib/env";
import { cookies } from "next/headers";

export const setRedirectCookie = wrapServerAction(
"setRedirectCookie",
async function setRedirectCookie(redirect: string) {
cookies().set(`${cookieName}.redirect`, redirect, {
cookies().set(`${COOKIE_NAME}.redirect`, redirect, {
domain: env.COOKIE_DOMAIN,
});
},
Expand Down
2 changes: 2 additions & 0 deletions lib/auth/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Permission } from "./permissions";
import { prisma } from "../db";
import { cache } from "react";

export const COOKIE_NAME = "ystv-calendar-session";

export interface UserWithIdentities extends User {
identities: Identity[];
}
Expand Down
15 changes: 9 additions & 6 deletions lib/auth/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import { SlackTokenJson, findOrCreateUserFromSlackToken } from "./slack";
import { env } from "../env";
import { RequestCookie } from "next/dist/compiled/@edge-runtime/cookies";
import { cache } from "react";
import { UserType, resolvePermissionsForUser, userHasPermission } from "./core";
import {
COOKIE_NAME,
UserType,
resolvePermissionsForUser,
userHasPermission,
} from "./core";

export * from "./core";

Expand All @@ -26,19 +31,17 @@ export async function requirePermission(...perms: Permission[]) {
if (!(await hasPermission(...perms))) throw new Forbidden(perms);
}

export const cookieName = "ystv-calendar-session";

const sessionSchema = z.object({
userID: z.number(),
});

async function getSession(req?: NextRequest) {
var sessionID: RequestCookie | undefined;
if (req) {
sessionID = req.cookies.get(cookieName);
sessionID = req.cookies.get(COOKIE_NAME);
} else {
const { cookies } = await import("next/headers");
sessionID = cookies().get(cookieName);
sessionID = cookies().get(COOKIE_NAME);
}
if (!sessionID) return null;
if (sessionID.value == "") return null;
Expand All @@ -57,7 +60,7 @@ async function getSession(req?: NextRequest) {
async function setSession(user: z.infer<typeof sessionSchema>) {
const payload = await encode(user);
const { cookies } = await import("next/headers");
cookies().set(cookieName, payload, {
cookies().set(COOKIE_NAME, payload, {
httpOnly: true,
sameSite: "lax",
secure: env.NODE_ENV === "production",
Expand Down
3 changes: 1 addition & 2 deletions server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { z } from "zod";
import { ExtendedError } from "socket.io/dist/namespace";
import { env } from "../lib/env";
import { Socket } from "socket.io";
import { cookieName } from "@/lib/auth/server";

export async function authenticateSocket(
socket: Socket,
Expand Down Expand Up @@ -32,7 +31,7 @@ export async function authenticateSocket(

const cookie = parseCookie(socket.client.request.headers.cookie);

const sessionCookie: string | undefined = cookie[cookieName];
const sessionCookie: string | undefined = cookie["ystv-calendar-session"];

if (sessionCookie) {
var decodedSession: unknown;
Expand Down

0 comments on commit 9b536b8

Please sign in to comment.