mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-09 14:00:15 -04:00
Smooth redirection moharana (#48)
* Fix indefinite loading spinner when navigating to same page * Add middleware to handle navigation depending on authentication
This commit is contained in:
parent
fdbf4ffa40
commit
00ba6d7df1
|
@ -15,7 +15,6 @@ export default function RootLayout({
|
||||||
const [isSidebarOpen, setIsSidebarOpen] = useState(true);
|
const [isSidebarOpen, setIsSidebarOpen] = useState(true);
|
||||||
const [user, setUser] = useState<User>();
|
const [user, setUser] = useState<User>();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function getUser() {
|
async function getUser() {
|
||||||
|
@ -36,7 +35,6 @@ export default function RootLayout({
|
||||||
);
|
);
|
||||||
|
|
||||||
setUser(await userData.json());
|
setUser(await userData.json());
|
||||||
setLoading(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getUser();
|
getUser();
|
||||||
|
@ -52,7 +50,6 @@ export default function RootLayout({
|
||||||
setIsSidebarOpen={setIsSidebarOpen}
|
setIsSidebarOpen={setIsSidebarOpen}
|
||||||
isSidebarOpen={isSidebarOpen}
|
isSidebarOpen={isSidebarOpen}
|
||||||
isAdmin={user.role === Role.ADMIN}
|
isAdmin={user.role === Role.ADMIN}
|
||||||
loading={loading}
|
|
||||||
/>
|
/>
|
||||||
<div
|
<div
|
||||||
className={`flex-1 transition duration-300 ease-in-out ${
|
className={`flex-1 transition duration-300 ease-in-out ${
|
||||||
|
|
|
@ -9,7 +9,6 @@ import {
|
||||||
LockClosedIcon,
|
LockClosedIcon,
|
||||||
} from "@heroicons/react/24/solid";
|
} from "@heroicons/react/24/solid";
|
||||||
import { SidebarItem } from "./SidebarItem";
|
import { SidebarItem } from "./SidebarItem";
|
||||||
import styles from "./LoadingIcon.module.css";
|
|
||||||
import { UserProfile } from "../resource/UserProfile";
|
import { UserProfile } from "../resource/UserProfile";
|
||||||
import LoadingIcon from "./LoadingIcon";
|
import LoadingIcon from "./LoadingIcon";
|
||||||
|
|
||||||
|
@ -19,7 +18,6 @@ interface SidebarProps {
|
||||||
name: string;
|
name: string;
|
||||||
email: string;
|
email: string;
|
||||||
isAdmin: boolean;
|
isAdmin: boolean;
|
||||||
loading: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const Sidebar: React.FC<SidebarProps> = ({
|
const Sidebar: React.FC<SidebarProps> = ({
|
||||||
|
@ -28,7 +26,6 @@ const Sidebar: React.FC<SidebarProps> = ({
|
||||||
name,
|
name,
|
||||||
email,
|
email,
|
||||||
isAdmin: admin,
|
isAdmin: admin,
|
||||||
loading,
|
|
||||||
}) => {
|
}) => {
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
|
import { usePathname } from "next/navigation";
|
||||||
|
|
||||||
interface SidebarItemProps {
|
interface SidebarItemProps {
|
||||||
icon: React.ReactElement;
|
icon: React.ReactElement;
|
||||||
|
@ -15,9 +16,13 @@ export const SidebarItem: React.FC<SidebarItemProps> = ({
|
||||||
redirect,
|
redirect,
|
||||||
onClick,
|
onClick,
|
||||||
}) => {
|
}) => {
|
||||||
|
const pathname = usePathname();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Link
|
<Link
|
||||||
onClick={() => onClick(true)}
|
onClick={() =>
|
||||||
|
pathname.startsWith(redirect) ? onClick(false) : onClick(true)
|
||||||
|
}
|
||||||
href={redirect}
|
href={redirect}
|
||||||
className={
|
className={
|
||||||
active
|
active
|
||||||
|
|
|
@ -23,8 +23,8 @@ export const SearchResult: React.FC<SearchResultProps> = ({
|
||||||
type === "resource"
|
type === "resource"
|
||||||
? BookmarkIcon
|
? BookmarkIcon
|
||||||
: type === "service"
|
: type === "service"
|
||||||
? ClipboardIcon
|
? ClipboardIcon
|
||||||
: QuestionMarkCircleIcon; // Unknown type
|
: QuestionMarkCircleIcon; // Unknown type
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex justify-between items-center w-full p-2 rounded-md hover:bg-purple-100 cursor-pointer group">
|
<div className="flex justify-between items-center w-full p-2 rounded-md hover:bg-purple-100 cursor-pointer group">
|
||||||
|
|
|
@ -1,32 +1,32 @@
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"type": "resource",
|
"type": "resource",
|
||||||
"name": "example name",
|
"name": "example name",
|
||||||
"description": "example description"
|
"description": "example description"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "service",
|
"type": "service",
|
||||||
"name": "example name",
|
"name": "example name",
|
||||||
"description": "example description"
|
"description": "example description"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "resource",
|
"type": "resource",
|
||||||
"name": "National Domestic Violence Hotline",
|
"name": "National Domestic Violence Hotline",
|
||||||
"description": "24/7 confidential support for victims of domestic violence"
|
"description": "24/7 confidential support for victims of domestic violence"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "resource",
|
"type": "resource",
|
||||||
"name": "Legal Aid Society",
|
"name": "Legal Aid Society",
|
||||||
"description": "Free legal assistance for low-income individuals"
|
"description": "Free legal assistance for low-income individuals"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "service",
|
"type": "service",
|
||||||
"name": "Crisis Hotline",
|
"name": "Crisis Hotline",
|
||||||
"description": "24/7 support for individuals in crisis"
|
"description": "24/7 support for individuals in crisis"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "unknown",
|
"type": "unknown",
|
||||||
"name": "unknown thing with a really long name",
|
"name": "unknown thing with a really long name",
|
||||||
"description": "and let's also type out a really long description to see how it handles overflow and all that anyways"
|
"description": "and let's also type out a really long description to see how it handles overflow and all that anyways"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import { createServerClient, type CookieOptions } from "@supabase/ssr";
|
import { createServerClient, type CookieOptions } from "@supabase/ssr";
|
||||||
import { NextResponse, type NextRequest } from "next/server";
|
import { User } from "@supabase/supabase-js";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { Role } from "../models/User";
|
||||||
|
|
||||||
export async function updateSession(request: NextRequest) {
|
export async function updateSession(request: NextRequest) {
|
||||||
let response = NextResponse.next({
|
let response = NextResponse.next({
|
||||||
|
@ -54,7 +56,50 @@ export async function updateSession(request: NextRequest) {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
await supabase.auth.getUser();
|
const { data, error } = await supabase.auth.getUser();
|
||||||
|
|
||||||
|
const authenticatedRoutes = ["/admin", "/resource", "/home", "/service"];
|
||||||
|
const pathname = request.nextUrl.pathname;
|
||||||
|
|
||||||
|
for (const route of authenticatedRoutes) {
|
||||||
|
if (error && pathname.startsWith(route)) {
|
||||||
|
console.log("redirected");
|
||||||
|
return NextResponse.redirect(
|
||||||
|
new URL(
|
||||||
|
"/auth/login",
|
||||||
|
request.nextUrl.protocol + "//" + request.nextUrl.host
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pathname.startsWith("/admin") && data.user) {
|
||||||
|
// After the previous checks we can assume the user is not empty
|
||||||
|
const userData = await fetch(
|
||||||
|
`${process.env.NEXT_PUBLIC_HOST}/api/user?uuid=${data.user.id}`
|
||||||
|
);
|
||||||
|
|
||||||
|
const user: User = await userData.json();
|
||||||
|
|
||||||
|
if (user.role !== Role.ADMIN) {
|
||||||
|
console.log("redirected as not admin");
|
||||||
|
return NextResponse.redirect(
|
||||||
|
new URL(
|
||||||
|
"/home",
|
||||||
|
request.nextUrl.protocol + "//" + request.nextUrl.host
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.user && pathname.startsWith("/auth/login")) {
|
||||||
|
return NextResponse.redirect(
|
||||||
|
new URL(
|
||||||
|
"/home",
|
||||||
|
request.nextUrl.protocol + "//" + request.nextUrl.host
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user