Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { redirect } from "next/navigation";
import DashboardSSEProvider from "@/components/DashboardSSEProvider";
import { DashboardWidgetA11yProvider } from "@/components/dashboard/DashboardWidgetA11yContext";
import RoastHypeWidget from "./RoastHypeWidget";
import ScrollToTopButton from "@/components/ScrollToTopButton";


export default async function DashboardPage() {
Expand Down Expand Up @@ -90,7 +91,7 @@ export default async function DashboardPage() {

{/* Today Focus */}
<section>
<TodayFocusHero userName={session.user?.name ?? null} />
<TodayFocusHero userName={session?.user?.name ?? null} />
</section>

{/* Featured Section */}
Expand Down Expand Up @@ -146,6 +147,8 @@ export default async function DashboardPage() {
<MilestonePlanner />
</section>
</div>

<ScrollToTopButton />
</main>
</DashboardWidgetA11yProvider>
</DashboardSSEProvider>
Expand Down
34 changes: 34 additions & 0 deletions src/components/ScrollToTopButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use client";

import { useState, useEffect, useCallback } from "react";
import { ArrowUp } from "lucide-react";

export default function ScrollToTopButton({ threshold = 400 }: { threshold?: number }) {
const [visible, setVisible] = useState(false);

useEffect(() => {
const handleScroll = () => {
setVisible(window.scrollY > threshold);
};

window.addEventListener("scroll", handleScroll, { passive: true });
handleScroll();
return () => window.removeEventListener("scroll", handleScroll);
}, [threshold]);

const scrollToTop = useCallback(() => {
window.scrollTo({ top: 0, behavior: "smooth" });
}, []);

if (!visible) return null;

return (
<button
onClick={scrollToTop}
aria-label="Scroll to top"
className="fixed bottom-6 right-6 z-50 flex h-11 w-11 items-center justify-center rounded-full bg-[var(--accent)] text-white shadow-lg transition-transform hover:scale-110 hover:shadow-xl active:scale-95"
>
<ArrowUp className="h-5 w-5" />
</button>
);
}
1 change: 1 addition & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export async function middleware(req: NextRequest) {
(route) => pathname === route || pathname.startsWith(`${route}/`)
);


if ((isProtectedRoute || isAdminRoute) && !token) {
const url = req.nextUrl.clone();
url.pathname = "/";
Expand Down
114 changes: 114 additions & 0 deletions test/get-session-token.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { describe, it, expect, beforeEach, vi } from "vitest";

// "server-only" throws outside a server context, so it must be stubbed for tests
vi.mock("server-only", () => ({}));

vi.mock("next-auth", () => ({
getServerSession: vi.fn(),
}));

vi.mock("@/lib/auth", () => ({
authOptions: {},
}));

vi.mock("@/lib/resolve-user", () => ({
resolveAppUser: vi.fn(),
}));

import { getServerSession } from "next-auth";
import { resolveAppUser } from "@/lib/resolve-user";
import { getSessionWithToken } from "../src/lib/get-session-token";

describe("getSessionWithToken", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("returns null when there is no session", async () => {
(getServerSession as any).mockResolvedValue(null);

const result = await getSessionWithToken();

expect(result).toBeNull();
});

it("returns null when session is missing githubId", async () => {
(getServerSession as any).mockResolvedValue({
githubLogin: "octocat",
accessToken: "token-123",
});

const result = await getSessionWithToken();

expect(result).toBeNull();
});

it("returns null when session is missing githubLogin", async () => {
(getServerSession as any).mockResolvedValue({
githubId: "12345",
accessToken: "token-123",
});

const result = await getSessionWithToken();

expect(result).toBeNull();
});

it("returns null when session is missing accessToken", async () => {
(getServerSession as any).mockResolvedValue({
githubId: "12345",
githubLogin: "octocat",
});

const result = await getSessionWithToken();

expect(result).toBeNull();
});

it("returns null when resolveAppUser returns null", async () => {
(getServerSession as any).mockResolvedValue({
githubId: "12345",
githubLogin: "octocat",
accessToken: "token-123",
});
(resolveAppUser as any).mockResolvedValue(null);

const result = await getSessionWithToken();

expect(resolveAppUser).toHaveBeenCalledWith("12345", "octocat");
expect(result).toBeNull();
});

it("returns SessionWithToken with correct shape when all conditions are met", async () => {
const session = {
githubId: "12345",
githubLogin: "octocat",
accessToken: "token-123",
};

(getServerSession as any).mockResolvedValue(session);
(resolveAppUser as any).mockResolvedValue({ id: "user-1" });

const result = await getSessionWithToken();

expect(result).toEqual({
session,
accessToken: "token-123",
});
});

it("populates accessToken field from the session", async () => {
const session = {
githubId: "67890",
githubLogin: "hubot",
accessToken: "gho_abcdef123456",
};

(getServerSession as any).mockResolvedValue(session);
(resolveAppUser as any).mockResolvedValue({ id: "user-2" });

const result = await getSessionWithToken();

expect(result?.accessToken).toBe("gho_abcdef123456");
});
});