Skip to content
Merged
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
12 changes: 9 additions & 3 deletions app/admin/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import type { ReactNode } from "react";
import { DashboardNav } from "@/components/dashboard/dashboard-nav";
import { getMember, getSession } from "@/lib/session";
import { can, canAccessAdminArea } from "@/lib/permissions";

/**
* /admin sits outside the /dashboard segment, so it needs the member-app nav
* of its own. Authorization stays in the page itself, which reads the live
* member record rather than trusting the token's role.
*/
export default function AdminLayout({ children }: { children: ReactNode }) {
export default async function AdminLayout({ children }: { children: ReactNode }) {
const session = await getSession();
const member = session ? await getMember(session.usn) : null;

return (
<>
{/* /admin is only reachable by elevated users (proxy + page guard), all of whom can sign off journey milestones. */}
<DashboardNav elevated reviewer tracker />
{member && (
<DashboardNav elevated={canAccessAdminArea(member)} reviewer={can(member, "journey:review")} tracker={can(member, "tracker:view")} />
)}
{children}
</>
);
Expand Down
5 changes: 2 additions & 3 deletions app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ export default async function AdminPage() {
{canManageMembers && (
<>

{pending.length > 0 && (
<section className="mb-10">
<h2 className="text-xs uppercase tracking-wider text-neutral-600 mb-3">
Waiting for review
</h2>
{pending.length === 0 && <p className="text-sm text-neutral-400">No requests waiting.</p>}
<div className="space-y-2">
{pending.map((member) => (
<MemberRow key={member.usn} member={member} isSelf={member.usn === me.usn} canManageRoles={canManageRoles} />
Expand All @@ -136,15 +136,14 @@ export default async function AdminPage() {
the student portal, so there is nothing to check by hand.
</p>
</section>
)}

<section>
<h2 className="text-xs uppercase tracking-wider text-neutral-600 mb-3">
Members ({approved.length})
</h2>
<div className="space-y-2">
{approved.map((member) => (
<MemberRow key={member.usn} member={member} isSelf={member.usn === me.usn} />
<MemberRow key={member.usn} member={member} isSelf={member.usn === me.usn} canManageRoles={canManageRoles} />
))}
</div>
</section>
Expand Down
17 changes: 16 additions & 1 deletion app/api/admin/members/[usn]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const runtime = "nodejs";
type Params = { params: Promise<{ usn: string }> };

const schema = z.object({
action: z.enum(["approve", "reject", "set-role"]),
action: z.enum(["approve", "reject", "set-role", "reissue"]),
role: z.enum(["member", "mentor", "admin"]).optional(),
});

Expand Down Expand Up @@ -62,6 +62,21 @@ export async function PATCH(request: NextRequest, { params }: Params) {
return NextResponse.json({ ok: true, message: `${target.name}'s request was declined.` });
}

if (action === "reissue") {
if (target.status !== "approved" || !target.email) {
return NextResponse.json({ ok: false, message: "Only approved members with an email can be reissued." }, { status: 409 });
}
const password = generatePassword();
// Send first: if the email fails, their current password keeps working.
try {
await sendCredentialsEmail({ to: target.email, name: target.name, username: target.usn, password });
} catch {
return NextResponse.json({ ok: false, message: "The email failed to send, so nothing was changed." }, { status: 502 });
}
await ref.update({ passwordHash: await hashPassword(password), mustChangePassword: true });
return NextResponse.json({ ok: true, message: `New credentials emailed to ${target.name}.` });
}

// approve
if (target.status === "approved") {
return NextResponse.json({ ok: true, message: `${target.name} is already a member.` });
Expand Down
3 changes: 2 additions & 1 deletion app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Star,
} from "lucide-react";
import { getMember, getSession } from "@/lib/session";
import { canAccessAdminArea } from "@/lib/permissions";
import { Avatar } from "@/components/ui/avatar";
import { PushToggle } from "@/components/pwa/push-toggle";
import { InstallButton } from "@/components/pwa/install-button";
Expand All @@ -28,7 +29,7 @@ export default async function DashboardPage() {
if (!member) redirect("/login");

const firstName = member.name.split(" ")[0];
const elevated = member.role === "admin" || member.role === "mentor";
const elevated = canAccessAdminArea(member);
const title = member.councilPosition ?? ROLE_LABEL[member.role] ?? "Member";
const joinedYear = member.joinedAt ? new Date(member.joinedAt).getFullYear() : null;

Expand Down
13 changes: 12 additions & 1 deletion components/admin/member-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function MemberRow({
const [message, setMessage] = useState<string | null>(null);
const [failed, setFailed] = useState(false);

async function act(action: "approve" | "reject" | "set-role", role?: string) {
async function act(action: "approve" | "reject" | "set-role" | "reissue", role?: string) {
if (action === "reissue" && !confirm(`Email ${member.name} a new password? Their current one will stop working.`)) return;
if (action === "reject" && !confirm(`Decline ${member.name}'s request?`)) return;

setPending(true);
Expand Down Expand Up @@ -111,6 +112,16 @@ export function MemberRow({
</>
)}

{member.status === "approved" && !isSelf && (
<button
onClick={() => act("reissue")}
disabled={pending}
className="glass-subtle hover:border-cyan-400/40 disabled:opacity-50 text-neutral-400 hover:text-cyan-300 text-xs rounded-full px-3.5 py-2 transition-colors"
>
Reissue password
</button>
)}

{member.status === "approved" && !isSelf && canManageRoles && (
<select
value={member.role}
Expand Down
21 changes: 10 additions & 11 deletions components/dashboard/dashboard-nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const LINKS: NavLink[] = [
* signed in. `elevated` comes from the server layout (which already reads the
* member) rather than a client fetch, one fewer round-trip per navigation.
*
* Desktop shows a pill row; below `lg` it collapses to a hamburger, since seven
* Desktop shows a pill row; below `xl` it collapses to a hamburger, since seven
* links scrolling sideways on a phone read as clutter.
*/
export function DashboardNav({ elevated = false, reviewer = false, tracker = false }: { elevated?: boolean; reviewer?: boolean; tracker?: boolean }) {
Expand All @@ -62,7 +62,7 @@ export function DashboardNav({ elevated = false, reviewer = false, tracker = fal

return (
<header className="fixed top-0 left-0 right-0 z-50 py-3 sm:py-4">
<div className="max-w-6xl mx-auto px-4">
<div className="max-w-7xl mx-auto px-4">
<div className="glass !rounded-2xl border border-white/10 px-3 py-2 flex items-center gap-2">
<Link href="/dashboard" className="flex items-center gap-2 group shrink-0 pl-1 pr-2">
{/* eslint-disable-next-line @next/next/no-img-element */}
Expand All @@ -77,35 +77,34 @@ export function DashboardNav({ elevated = false, reviewer = false, tracker = fal
</Link>

{/* Desktop pill row */}
<nav aria-label="Member app" className="hidden lg:flex items-center gap-1 flex-1 min-w-0">
<nav aria-label="Member app" className="hidden xl:flex items-center gap-1 flex-1 min-w-0">
{links.map((link) => {
const active = isActive(link.href);
return (
<Link
key={link.href}
href={link.href}
aria-current={active ? "page" : undefined}
className={`inline-flex items-center gap-1.5 px-3 py-2 rounded-full text-sm font-medium whitespace-nowrap transition-colors ${
className={`inline-flex items-center px-2.5 py-2 rounded-full text-sm font-medium whitespace-nowrap transition-colors ${
active
? "bg-cyan-400/15 text-cyan-300"
: "text-neutral-400 hover:text-white hover:bg-white/5"
}`}
>
{link.icon}
<span>{link.name}</span>
</Link>
);
})}
</nav>

<div className="flex items-center gap-2 shrink-0 ml-auto lg:ml-0">
<div className="flex items-center gap-2 shrink-0 ml-auto xl:ml-0">
<ThemeSwitcher />
<div className="hidden lg:block">
<div className="hidden xl:block">
<SignOutButton />
</div>
<button
onClick={() => setOpen((v) => !v)}
className="lg:hidden glass-subtle hover:border-cyan-400/40 text-neutral-200 rounded-full p-2 transition-colors"
className="xl:hidden glass-subtle hover:border-cyan-400/40 text-neutral-200 rounded-full p-2 transition-colors"
aria-label={open ? "Close menu" : "Open menu"}
aria-expanded={open}
>
Expand All @@ -126,7 +125,7 @@ export function DashboardNav({ elevated = false, reviewer = false, tracker = fal
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
onClick={() => setOpen(false)}
className="lg:hidden fixed inset-0 z-40 bg-black/70 cursor-default"
className="xl:hidden fixed inset-0 z-40 bg-black/70 cursor-default"
/>
{/* Fixed + high z so it clears the page's stacking contexts.
Only the slide is animated, never opacity, a throttled
Expand All @@ -138,11 +137,11 @@ export function DashboardNav({ elevated = false, reviewer = false, tracker = fal
animate={{ y: 0 }}
exit={{ y: -8 }}
transition={{ duration: 0.18 }}
className="lg:hidden fixed left-4 right-4 top-[68px] z-50"
className="xl:hidden fixed left-4 right-4 top-[68px] z-50"
>
<div
style={{ background: "var(--bg-secondary)" }}
className="max-w-6xl mx-auto rounded-2xl border border-white/10 shadow-2xl p-2"
className="max-w-7xl mx-auto rounded-2xl border border-white/10 shadow-2xl p-2"
>
{links.map((link) => {
const active = isActive(link.href);
Expand Down
5 changes: 2 additions & 3 deletions proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ export function proxy(request: NextRequest) {
return NextResponse.redirect(login);
}

if (pathname.startsWith("/admin") && session.role !== "admin" && session.role !== "mentor") {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
// No role check for /admin: council powers (e.g. Membership Lead) aren't in the
// token, so each admin page checks the live member record itself.

return NextResponse.next();
}
Expand Down
Loading