* Migrate authentication from Clerk to BetterAuth (#322) Replaces @clerk/react-router with self-hosted better-auth to eliminate the external Clerk dependency and keep all user/session data in our own PostgreSQL database. **What changed** - New: auth.server.ts (BetterAuth config w/ Drizzle adapter, bcrypt, Resend), auth-client.ts, api.auth.$.ts handler - New: /login and /register pages with email+password and Google/Discord OAuth; open-redirect guard on redirectTo param - New: UserMenu component replacing Clerk's UserButton - Schema: sessions, accounts, verifications tables; emailVerified column; clerkId made nullable - Migrations 0081 (BetterAuth tables) and 0082 (accounts extra columns for v1.6.9) - All ~30 route files: getAuth → auth.api.getSession, isUserAdminByClerkId → isUserAdmin - root.tsx: isAdmin read directly from session.user.isAdmin (no extra DB query) - useDraftAuthRecovery: removed Clerk JWT refresh logic; replaced with cookie-session check - models/user.ts: removed findUserByClerkId, findOrCreateUser, updateUserByClerkId (webhook pattern) - Deleted: app/routes/api/webhooks/clerk.ts; uninstalled @clerk/react-router, @clerk/themes, svix - scripts/migrate.mjs: extended with idempotent Clerk → BetterAuth data migration (FK conversion, email_verified, OAuth accounts) - scripts/migrate-clerk-passwords.mjs: one-time script to import bcrypt hashes from Clerk CSV export - BETTERAUTH_MIGRATION.md: dev and production runbooks - All test mocks updated: vi.mock('~/lib/auth.server') instead of @clerk/react-router/server - Test fixtures: added emailVerified field **Follow-up (post-stable)** - Rename actor_clerk_id column → actor_user_id in commissioner_audit_log - Drop clerk_id column from users once migration confirmed Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Add .npmrc with legacy-peer-deps for better-auth/drizzle peer dep conflict better-auth@1.6.9 declares peerOptional deps on drizzle-orm ^0.45.2 and drizzle-kit >=0.31.4, but we run drizzle-orm ~0.36.3 / drizzle-kit ~0.28.1. The adapter works correctly at runtime with our versions — the peer dep is only for stricter type checking. This unblocks npm ci in CI without a risky drizzle major-version upgrade. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
244 lines
9.3 KiB
TypeScript
244 lines
9.3 KiB
TypeScript
import { useEffect } from "react";
|
|
import { useSearchParams } from "react-router";
|
|
import { toast } from "sonner";
|
|
import { auth } from "~/lib/auth.server";
|
|
import { addDays, subDays } from "date-fns";
|
|
|
|
import type { Route } from "./+types/home";
|
|
import { LandingPage } from "~/components/marketing/LandingPage";
|
|
import { findLeaguesWithActiveSeasonsByUserId } from "~/models/league";
|
|
import { findSeasonById, findCurrentSeasonWithSports } from "~/models/season";
|
|
import { findTeamByOwnerAndSeason } from "~/models/team";
|
|
import { getDraftedParticipantsBySportsSeason } from "~/models/draft-pick";
|
|
import { getUpcomingEventsForDraftedParticipants } from "~/models/scoring-event";
|
|
import { getTeamStanding, getSeasonStandings } from "~/models/standings";
|
|
import { getSeasonCompletionPercentage } from "~/lib/season-helpers.server";
|
|
import { buildTiedRankChecker, getDisplayRank } from "~/lib/standings-display";
|
|
import { findDraftSlotsBySeasonId } from "~/models/draft-slot";
|
|
import { getTeamForPick } from "~/lib/draft-order";
|
|
import type { CalendarPanelEvent } from "~/components/sport-season/UpcomingCalendarPanel";
|
|
import { UpcomingEventsCard } from "~/components/sport-season/UpcomingEventsCard";
|
|
import { MyLeaguesCard } from "~/components/league/MyLeaguesCard";
|
|
import { CreateLeagueCard } from "~/components/league/CreateLeagueCard";
|
|
import type { LeagueRowProps } from "~/components/league/LeagueRow";
|
|
import { toEventSortKey } from "~/lib/date-utils";
|
|
|
|
export function meta() {
|
|
return [
|
|
{ title: "Brackt - Fantasy All of the Sports!" },
|
|
{
|
|
name: "description",
|
|
content: "Play multi-sport fantasy leagues with your friends!",
|
|
},
|
|
];
|
|
}
|
|
|
|
export async function loader(args: Route.LoaderArgs) {
|
|
const session = await auth.api.getSession({ headers: args.request.headers });
|
|
const userId = session?.user.id ?? null;
|
|
|
|
if (!userId) {
|
|
return { leagues: [], isLoggedIn: false, upcomingCalendarEvents: [] };
|
|
}
|
|
|
|
const leagues = await findLeaguesWithActiveSeasonsByUserId(userId);
|
|
|
|
const today = new Date();
|
|
const dateFromStr = subDays(today, 1).toISOString().split("T")[0];
|
|
const dateToStr = addDays(today, 30).toISOString().split("T")[0];
|
|
|
|
const leaguesWithData = await Promise.all(
|
|
leagues.map(async (league) => {
|
|
const [season, seasonWithSports] = await Promise.all([
|
|
league.currentSeasonId ? findSeasonById(league.currentSeasonId) : null,
|
|
findCurrentSeasonWithSports(league.id),
|
|
]);
|
|
|
|
const numSports = seasonWithSports?.seasonSports?.length ?? 0;
|
|
const calendarEvents: CalendarPanelEvent[] = [];
|
|
let currentRank: number | undefined;
|
|
let totalPoints: number | undefined;
|
|
let previousRank: number | undefined;
|
|
let displayRank: string | number | undefined;
|
|
let completionPercentage = 0;
|
|
let picksUntilMyTurn: number | undefined;
|
|
let draftPosition: number | undefined;
|
|
|
|
if (league.currentSeasonId && seasonWithSports?.seasonSports) {
|
|
const myTeam = await findTeamByOwnerAndSeason(userId, league.currentSeasonId);
|
|
|
|
if (myTeam) {
|
|
// Fetch standing, completion %, and calendar events in parallel
|
|
const [standing, pct, participantsBySportsSeason] = await Promise.all([
|
|
getTeamStanding(myTeam.id, league.currentSeasonId),
|
|
getSeasonCompletionPercentage(league.currentSeasonId),
|
|
getDraftedParticipantsBySportsSeason(myTeam.id, league.currentSeasonId),
|
|
]);
|
|
|
|
completionPercentage = pct;
|
|
|
|
// Draft slot info — needed for pre-draft position and in-progress picks-until-turn
|
|
if (season?.status === "draft" || season?.status === "pre_draft") {
|
|
const draftSlots = await findDraftSlotsBySeasonId(league.currentSeasonId);
|
|
const mySlot = draftSlots.find((s) => s.teamId === myTeam.id);
|
|
if (mySlot) {
|
|
draftPosition = mySlot.draftOrder;
|
|
}
|
|
if (season.status === "draft" && season.currentPickNumber) {
|
|
const currentPick = season.currentPickNumber;
|
|
let offset = 0;
|
|
// Snake draft: one full cycle is draftSlots.length * 2 picks
|
|
while (offset < draftSlots.length * 2) {
|
|
const slot = getTeamForPick(currentPick + offset, draftSlots);
|
|
if (slot?.teamId === myTeam.id) {
|
|
picksUntilMyTurn = offset;
|
|
break;
|
|
}
|
|
offset++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (standing) {
|
|
currentRank = standing.currentRank ?? undefined;
|
|
totalPoints = standing.totalPoints ?? undefined;
|
|
previousRank = standing.previousRank ?? undefined;
|
|
const allStandings = await getSeasonStandings(league.currentSeasonId);
|
|
const isTied = buildTiedRankChecker(allStandings.map((s) => s.currentRank));
|
|
displayRank = getDisplayRank(standing, allStandings.length, isTied(standing.currentRank));
|
|
} else if (myTeam && season?.status === "active") {
|
|
// No standing row yet (no scoring events) — default to rank 1, 0 points
|
|
currentRank = 1;
|
|
totalPoints = 0;
|
|
}
|
|
|
|
const perSeasonEvents = await Promise.all(
|
|
seasonWithSports.seasonSports.map(async ({ sportsSeason: ss }) => {
|
|
const draftedParticipants = participantsBySportsSeason.get(ss.id) ?? [];
|
|
if (draftedParticipants.length === 0) return [];
|
|
|
|
const events = await getUpcomingEventsForDraftedParticipants(
|
|
ss.id,
|
|
ss.scoringPattern ?? "",
|
|
draftedParticipants,
|
|
dateFromStr,
|
|
dateToStr
|
|
);
|
|
|
|
return events.map((event) => ({
|
|
...event,
|
|
sportName: ss.sport.name,
|
|
sportSeasonName: ss.name,
|
|
leagueName: league.name,
|
|
leagueId: league.id,
|
|
sportsSeasonPageUrl: `/leagues/${league.id}/sports-seasons/${ss.id}`,
|
|
}));
|
|
})
|
|
);
|
|
|
|
calendarEvents.push(...perSeasonEvents.flat());
|
|
}
|
|
}
|
|
|
|
return {
|
|
...league,
|
|
currentSeason: season,
|
|
numSports,
|
|
displayRank,
|
|
currentRank,
|
|
totalPoints,
|
|
previousRank,
|
|
completionPercentage,
|
|
picksUntilMyTurn,
|
|
draftPosition,
|
|
draftDateTime: season?.draftDateTime?.toISOString() ?? null,
|
|
calendarEvents,
|
|
};
|
|
})
|
|
);
|
|
|
|
const upcomingCalendarEvents = leaguesWithData
|
|
.flatMap((l) => l.calendarEvents)
|
|
.toSorted((a, b) => toEventSortKey(a).localeCompare(toEventSortKey(b)));
|
|
|
|
return {
|
|
leagues: leaguesWithData,
|
|
isLoggedIn: true,
|
|
upcomingCalendarEvents,
|
|
};
|
|
}
|
|
|
|
const VALID_STATUSES = ["draft", "active", "pre_draft", "completed"] as const;
|
|
type ValidStatus = (typeof VALID_STATUSES)[number];
|
|
function isValidStatus(s: string): s is ValidStatus {
|
|
return VALID_STATUSES.includes(s as ValidStatus);
|
|
}
|
|
|
|
export default function Home({ loaderData }: Route.ComponentProps) {
|
|
const { leagues, isLoggedIn, upcomingCalendarEvents } = loaderData;
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
|
|
useEffect(() => {
|
|
if (searchParams.get("deleted") === "true") {
|
|
toast.success("League deleted successfully");
|
|
setSearchParams({});
|
|
}
|
|
if (searchParams.get("left") === "true") {
|
|
toast.success("You have left the league. Your team is now available.");
|
|
setSearchParams({});
|
|
}
|
|
}, [searchParams, setSearchParams]);
|
|
|
|
if (!isLoggedIn) {
|
|
return <LandingPage />;
|
|
}
|
|
|
|
const leagueRows: LeagueRowProps[] = leagues.map((league) => {
|
|
const rawStatus = league.currentSeason?.status ?? "pre_draft";
|
|
return {
|
|
leagueId: league.id,
|
|
leagueName: league.name,
|
|
numSports: league.numSports,
|
|
status: isValidStatus(rawStatus) ? rawStatus : "pre_draft",
|
|
seasonId: league.currentSeasonId ?? undefined,
|
|
currentRank: league.currentRank,
|
|
displayRank: league.displayRank,
|
|
totalPoints: league.totalPoints,
|
|
previousRank: league.previousRank,
|
|
completionPercentage: league.completionPercentage,
|
|
picksUntilMyTurn: league.picksUntilMyTurn,
|
|
draftPosition: league.draftPosition,
|
|
draftDateTime: league.draftDateTime,
|
|
};
|
|
});
|
|
|
|
return (
|
|
<div className="container mx-auto py-8 px-4">
|
|
{/*
|
|
lg+: 3-col grid, left column (My Leagues + Create) spans 2, right (Events) spans 1.
|
|
mobile/tablet: single column stacked in order: My Leagues, Upcoming Events, Create League.
|
|
*/}
|
|
<div className="grid grid-cols-1 gap-6 lg:grid-cols-3">
|
|
{/* My Leagues — order 1 on mobile, col-span-2 on desktop */}
|
|
<div className="order-1 lg:col-span-2">
|
|
<MyLeaguesCard leagues={leagueRows} />
|
|
</div>
|
|
|
|
{/* Upcoming Events — order 2 on mobile, right column on desktop spanning both rows */}
|
|
<div className="order-2 lg:col-span-1 lg:row-span-2">
|
|
<UpcomingEventsCard
|
|
events={upcomingCalendarEvents}
|
|
limit={8}
|
|
viewAllUrl="/upcoming-events"
|
|
showLeagueAvatar={leagueRows.length > 1}
|
|
/>
|
|
</div>
|
|
|
|
{/* Create a League — order 3 on mobile, below My Leagues on desktop */}
|
|
<div className="order-3 lg:col-span-2">
|
|
<CreateLeagueCard />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|