Skip to content

Commit

Permalink
🐛 Fix session empty session when logging out (#1345)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella authored Sep 13, 2024
1 parent b75dfed commit 8f34f75
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
10 changes: 0 additions & 10 deletions apps/web/src/app/[locale]/auth/logout/route.ts

This file was deleted.

9 changes: 9 additions & 0 deletions apps/web/src/app/api/logout/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NextRequest, NextResponse } from "next/server";

import { resetUser } from "@/app/guest";

export async function POST(req: NextRequest) {
const res = NextResponse.json({ ok: 1 });
await resetUser(req, res);
return res;
}
7 changes: 2 additions & 5 deletions apps/web/src/app/components/logout-button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";
import { Button, ButtonProps } from "@rallly/ui/button";
import { signOut } from "next-auth/react";

import { usePostHog } from "@/utils/posthog";

Expand All @@ -15,12 +14,10 @@ export function LogoutButton({
{...rest}
onClick={async (e) => {
onClick?.(e);
await fetch("/api/logout", { method: "POST" });
posthog?.capture("logout");
posthog?.reset();
signOut({
redirect: true,
callbackUrl: "/login",
});
window.location.href = "/login";
}}
>
{children}
Expand Down
31 changes: 21 additions & 10 deletions apps/web/src/app/guest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import languages from "@rallly/languages";
import languageParser from "accept-language-parser";
import { NextRequest, NextResponse } from "next/server";
import { encode, JWT } from "next-auth/jwt";

import { absoluteUrl } from "@/utils/absolute-url";
import { randomid } from "@/utils/nanoid";

const supportedLocales = Object.keys(languages);

function getCookieSettings() {
const secure = absoluteUrl().startsWith("https://");
const prefix = secure ? "__Secure-" : "";
Expand All @@ -14,6 +18,16 @@ function getCookieSettings() {
};
}

export async function getLocaleFromHeader(req: NextRequest) {
// Check if locale is specified in header
const headers = req.headers;
const acceptLanguageHeader = headers.get("accept-language");
const localeFromHeader = acceptLanguageHeader
? languageParser.pick(supportedLocales, acceptLanguageHeader)
: null;
return localeFromHeader ?? "en";
}

async function setCookie(res: NextResponse, jwt: JWT) {
const { name, secure } = getCookieSettings();

Expand All @@ -32,32 +46,29 @@ async function setCookie(res: NextResponse, jwt: JWT) {
});
}

export async function resetUser(res: NextResponse) {
export async function resetUser(req: NextRequest, res: NextResponse) {
// resets to a new guest user
const locale = await getLocaleFromHeader(req);

const jwt: JWT = {
sub: `user-${randomid()}`,
email: null,
locale,
};

await setCookie(res, jwt);
}

export async function initGuest(
req: NextRequest,
res: NextResponse,
{
locale,
}: {
locale: string;
},
) {
export async function initGuest(req: NextRequest, res: NextResponse) {
const { name } = getCookieSettings();

if (req.cookies.has(name)) {
// already has a session token
return;
}

const locale = await getLocaleFromHeader(req);

const jwt: JWT = {
sub: `user-${randomid()}`,
email: null,
Expand Down
15 changes: 4 additions & 11 deletions apps/web/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import languages from "@rallly/languages";
import languageParser from "accept-language-parser";
import { NextResponse } from "next/server";
import withAuth from "next-auth/middleware";

import { initGuest } from "@/app/guest";
import { getLocaleFromHeader, initGuest } from "@/app/guest";
import { isSelfHosted } from "@/utils/constants";

const supportedLocales = Object.keys(languages);

export const middleware = withAuth(
async function middleware(req) {
const { headers, nextUrl } = req;
const { nextUrl } = req;
const newUrl = nextUrl.clone();

// if the user is already logged in, don't let them access the login page
Expand All @@ -28,20 +27,14 @@ export const middleware = withAuth(
newUrl.pathname = `/${locale}${newUrl.pathname}`;
} else {
// Check if locale is specified in header
const acceptLanguageHeader = headers.get("accept-language");
const localeFromHeader = acceptLanguageHeader
? languageParser.pick(supportedLocales, acceptLanguageHeader)
: null;
locale = localeFromHeader ?? "en";
locale = await getLocaleFromHeader(req);

newUrl.pathname = `/${locale}${newUrl.pathname}`;
}

const res = NextResponse.rewrite(newUrl);

await initGuest(req, res, {
locale,
});
await initGuest(req, res);

return res;
},
Expand Down

0 comments on commit 8f34f75

Please sign in to comment.