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
20 changes: 20 additions & 0 deletions src/evently.client/src/lib/services/auth-service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { redirect } from "@tanstack/react-router";
import axios from "axios";
import { Account } from "~/lib/domains/entities";

Expand All @@ -23,3 +24,22 @@ export async function getAccount(): Promise<Account | null> {
}
return null;
}

/**
* Validate against the user's `Account` in the route context.
* Redirect to login page if authentication fails.
* Tightly coupled with TanStack framework
* @param account
* @param currentHref
*/
export async function guardRoute(account: Account | null, currentHref: string): Promise<void> {
if (account == null) {
throw redirect({
to: "/login",
replace: true,
search: {
redirect: currentHref
}
});
}
}
258 changes: 124 additions & 134 deletions src/evently.client/src/routeTree.gen.ts

Large diffs are not rendered by default.

24 changes: 0 additions & 24 deletions src/evently.client/src/routes/(auth)/gatherings/route.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createFileRoute, Outlet, redirect } from "@tanstack/react-router";

export const Route = createFileRoute("/(auth)/bookings")({
export const Route = createFileRoute("/_auth")({
beforeLoad: ({ context }) => {
if (context.account == null) {
throw redirect({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Card, Tabs, TabState } from "~/lib/components";
import cloneDeep from "lodash.clonedeep";
import { useQuery } from "@tanstack/react-query";

export const Route = createFileRoute("/(auth)/bookings/attending/")({
export const Route = createFileRoute("/bookings/(auth)/attending/")({
loader: ({ context }) => ({ account: context.account }),
component: GetBookingsPage,
pendingComponent: () => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { BookingsTable, Jumbotron, StatsCard } from "./-components";
import { useInterval } from "usehooks-ts";
import type { PageResult } from "~/lib/domains/interfaces";

export const Route = createFileRoute("/(auth)/bookings/hosting/$gatheringId/dashboard/")({
export const Route = createFileRoute("/bookings/(auth)/hosting/$gatheringId/dashboard/")({
loader: async ({ params }) => {
const gatheringId: number = parseInt(params.gatheringId);
const gathering: Gathering = (await getGathering(gatheringId)) ?? new Gathering();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ToastContent, ToastStatus, toastStyles } from "~/lib/domains/models";
import { useForm } from "@tanstack/react-form";
import { FieldErrMsg as FieldInfo } from "~/lib/components";

export const Route = createFileRoute("/(auth)/bookings/hosting/$gatheringId/dashboard/scan")({
export const Route = createFileRoute("/bookings/(auth)/hosting/$gatheringId/dashboard/scan")({
component: RouteComponent
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Card, Tabs, TabState } from "~/lib/components";
import { useQuery } from "@tanstack/react-query";
import cloneDeep from "lodash.clonedeep";

export const Route = createFileRoute("/(auth)/bookings/hosting/")({
export const Route = createFileRoute("/bookings/(auth)/hosting/")({
component: GetHostedGatheringsPage,
pendingComponent: () => (
<div className="h-full">
Expand Down
11 changes: 11 additions & 0 deletions src/evently.client/src/routes/bookings/(auth)/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createFileRoute, Outlet } from "@tanstack/react-router";
import { guardRoute } from "~/lib/services";

export const Route = createFileRoute("/bookings/(auth)")({
beforeLoad: ({ context }) => guardRoute(context.account, window.location.href),
component: AuthLayout
});

function AuthLayout() {
return <Outlet />;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { createFileRoute } from "@tanstack/react-router";
import { Category, Gathering } from "~/lib/domains/entities";
import { useEffect, useState, type JSX } from "react";
import { fetchFile, getCategories, getGathering, sleep, updateGathering } from "~/lib/services";
import {
fetchFile,
getCategories,
getGathering,
guardRoute,
sleep,
updateGathering
} from "~/lib/services";
import {
useGatheringForm,
type GatheringForm as IGatheringForm
} from "~/routes/(auth)/gatherings/-services";
} from "~/routes/gatherings/-services";
import { GatheringReqDto, ToastContent } from "~/lib/domains/models";
import { GatheringForm } from "~/routes/(auth)/gatherings/-components";
import { GatheringForm } from "~/routes/gatherings/-components";

export const Route = createFileRoute("/(auth)/gatherings/$gatheringId/update")({
export const Route = createFileRoute("/gatherings/$gatheringId/(auth)/update")({
beforeLoad: ({ context }) => guardRoute(context.account, window.location.href),
loader: async ({ params }) => {
const gatheringId: number = parseInt(params.gatheringId);
let gathering: Gathering | null = await getGathering(gatheringId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createFileRoute } from "@tanstack/react-router";
import { Category, Gathering } from "~/lib/domains/entities";
import { useState, type JSX } from "react";
import { createGathering, getCategories, sleep } from "~/lib/services";
import { createGathering, getCategories, guardRoute, sleep } from "~/lib/services";
import { useGatheringForm, type GatheringForm as IGatheringForm } from "./-services";
import { GatheringReqDto, ToastContent } from "~/lib/domains/models";
import { GatheringForm } from "~/routes/(auth)/gatherings/-components";
import { GatheringForm } from "~/routes/gatherings/-components";

export const Route = createFileRoute("/(auth)/gatherings/create")({
export const Route = createFileRoute("/gatherings/(auth)/create")({
beforeLoad: ({ context }) => guardRoute(context.account, window.location.href),
loader: async () => {
const categories: Category[] = await getCategories();
return { categories };
Expand Down