Skip to content

Commit b0c34c0

Browse files
committed
Fix navbar showing logged in when not
1 parent adbd48f commit b0c34c0

File tree

5 files changed

+53
-53
lines changed

5 files changed

+53
-53
lines changed

peerprep/app/actions/server_actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export async function hydrateUid(): Promise<null | UserData> {
9595
if (isError(json)) {
9696
console.log("Failed to fetch user ID.");
9797
console.log(`Error ${json.status}: ${json.error}`);
98-
redirect("/auth/logout");
98+
// redirect("/auth/logout");
9999
}
100100
// TODO: handle error handling
101101
const response = json as UserServiceResponse;

peerprep/app/questions/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import { hydrateUid } from "../actions/server_actions";
66
import { isError, Question, StatusBody, UserData } from "@/api/structs";
77
import { UserInfoProvider } from "@/contexts/UserInfoContext";
88
import { fetchAllQuestions } from "@/app/questions/helper";
9+
import { redirect } from "next/navigation";
910

1011
async function QuestionsPage() {
1112
const userData = (await hydrateUid()) as UserData;
1213

14+
if (!userData) {
15+
redirect("/auth/login");
16+
}
17+
1318
const questions: Question[] | StatusBody = await fetchAllQuestions();
1419

1520
if (isError(questions)) {

peerprep/components/navbar/ProfileDropdown.tsx

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
"use client";
2-
31
import { Menu, MenuButton, MenuItem, MenuItems } from "@headlessui/react";
42
import Image from "next/image";
53
import React from "react";
64
import Link from "next/link";
5+
import { UserData } from "@/api/structs";
6+
import { hydrateUid } from "@/app/actions/server_actions";
7+
8+
export const ProfileDropdown = async () => {
9+
let userData;
10+
try {
11+
userData = (await hydrateUid()) as UserData;
12+
} catch {
13+
userData = null;
14+
console.error("Error hydrating user data.");
15+
}
716

8-
export const ProfileDropdown = () => {
917
return (
1018
<div className="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0">
1119
{/* Profile dropdown */}
@@ -27,30 +35,34 @@ export const ProfileDropdown = () => {
2735
transition
2836
className="absolute right-0 z-10 mt-2 w-48 origin-top-right rounded-md bg-gray-1 py-1 shadow-lg ring-1 ring-black ring-opacity-5 transition focus:outline-none data-[closed]:scale-95 data-[closed]:transform data-[closed]:opacity-0 data-[enter]:duration-100 data-[leave]:duration-75 data-[enter]:ease-out data-[leave]:ease-in"
2937
>
30-
<MenuItem>
31-
<Link
32-
href="#"
33-
className="block px-4 py-2 text-sm data-[focus]:bg-gray-2"
34-
>
35-
Your Profile (Coming Soon)
36-
</Link>
37-
</MenuItem>
38-
<MenuItem>
39-
<Link
40-
href="#"
41-
className="block px-4 py-2 text-sm data-[focus]:bg-gray-2"
42-
>
43-
Settings (Coming Soon)
44-
</Link>
45-
</MenuItem>
46-
<MenuItem>
47-
<Link
48-
href="/auth/logout"
49-
className="block px-4 py-2 text-sm data-[focus]:bg-gray-2"
50-
>
51-
Sign out
52-
</Link>
53-
</MenuItem>
38+
{userData ? (
39+
<>
40+
<MenuItem>
41+
<div className="block px-4 py-2 text-sm text-white data-[focus]:bg-gray-2">
42+
Hello, {userData.username}!
43+
</div>
44+
</MenuItem>
45+
<MenuItem>
46+
<Link
47+
href="/auth/logout"
48+
className="block px-4 py-2 text-sm data-[focus]:bg-gray-2"
49+
>
50+
Sign out
51+
</Link>
52+
</MenuItem>
53+
</>
54+
) : (
55+
<>
56+
<MenuItem>
57+
<Link
58+
href="/auth/login"
59+
className="block px-4 py-2 text-sm data-[focus]:bg-gray-2"
60+
>
61+
Login
62+
</Link>
63+
</MenuItem>
64+
</>
65+
)}
5466
</MenuItems>
5567
</Menu>
5668
</div>

peerprep/components/shared/TitleBar.tsx

Lines changed: 0 additions & 23 deletions
This file was deleted.

peerprep/middleware.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { cookies } from "next/headers";
22
import { NextRequest, NextResponse } from "next/server";
3+
import { CookieNames } from "@/app/actions/session";
34

45
const protectedRoutes = ["/questions/*", "/user/*"];
56
const publicRoutes = ["/", "/auth/login/", "/auth/register"];
@@ -27,8 +28,13 @@ export async function middleware(request: NextRequest) {
2728
}
2829

2930
if (path === "/auth/logout" || path === "/auth/logout/") {
30-
let response = NextResponse.redirect(new URL("/auth/login", request.url));
31-
response.cookies.delete("session");
31+
const response = NextResponse.redirect(new URL("/auth/login", request.url));
32+
33+
for (const cookieName of Object.values(CookieNames)) {
34+
response.cookies.delete(cookieName.valueOf());
35+
}
36+
// response.cookies.delete("session");
37+
3238
return response;
3339
}
3440

0 commit comments

Comments
 (0)