Add support for DISABLE_AUTH on the FE

This commit is contained in:
Weves 2023-05-16 21:20:35 -07:00 committed by Chris Weaver
parent 16ca6f760c
commit d447b66039
6 changed files with 34 additions and 20 deletions

View File

@ -7,6 +7,7 @@ import {
GoogleDriveIcon,
SlackIcon,
} from "@/components/icons/icons";
import { DISABLE_AUTH } from "@/lib/constants";
import { getCurrentUserSS } from "@/lib/userSS";
import { redirect } from "next/navigation";
@ -15,12 +16,15 @@ export default async function AdminLayout({
}: {
children: React.ReactNode;
}) {
const user = await getCurrentUserSS();
if (!user) {
return redirect("/auth/login");
}
if (user.role !== "admin") {
return redirect("/");
let user = null;
if (!DISABLE_AUTH) {
user = await getCurrentUserSS();
if (!user) {
return redirect("/auth/login");
}
if (user.role !== "admin") {
return redirect("/");
}
}
return (

View File

@ -1,7 +1,13 @@
import { DISABLE_AUTH } from "@/lib/constants";
import { getGoogleOAuthUrlSS, getCurrentUserSS } from "@/lib/userSS";
import { redirect } from "next/navigation";
const Page = async () => {
// no need for any of the below if auth is disabled
if (DISABLE_AUTH) {
return redirect("/");
}
const [currentUser, authorizationUrl] = await Promise.all([
getCurrentUserSS(),
getGoogleOAuthUrlSS(),

View File

@ -1,9 +1,6 @@
import { Header } from "@/components/Header";
import "./globals.css";
import { Inter } from "next/font/google";
import { getCurrentUserSS } from "@/lib/userSS";
import { redirect } from "next/navigation";
const inter = Inter({
subsets: ["latin"],

View File

@ -2,11 +2,15 @@ import { SearchSection } from "@/components/search/SearchSection";
import { Header } from "@/components/Header";
import { getCurrentUserSS } from "@/lib/userSS";
import { redirect } from "next/navigation";
import { DISABLE_AUTH } from "@/lib/constants";
export default async function Home() {
const user = await getCurrentUserSS();
if (!user) {
return redirect("/auth/login");
let user = null;
if (!DISABLE_AUTH) {
user = await getCurrentUserSS();
if (!user && !DISABLE_AUTH) {
return redirect("/auth/login");
}
}
return (
<>

View File

@ -9,7 +9,7 @@ import { useRouter } from "next/navigation";
import React, { useEffect, useRef, useState } from "react";
interface HeaderProps {
user: User;
user: User | null;
}
export const Header: React.FC<HeaderProps> = ({ user }) => {
@ -74,19 +74,21 @@ export const Header: React.FC<HeaderProps> = ({ user }) => {
"w-36 overflow-hidden shadow-xl z-10 text-sm text-gray-300"
}
>
{user.role === "admin" && (
{user?.role === "admin" && (
<Link href="/admin/indexing/status">
<div className="flex py-2 px-3 cursor-pointer hover:bg-gray-500 border-b border-gray-500">
Connectors
</div>
</Link>
)}
<div
className="flex py-2 px-3 cursor-pointer hover:bg-gray-500"
onClick={handleLogout}
>
Logout
</div>
{user && (
<div
className="flex py-2 px-3 cursor-pointer hover:bg-gray-500"
onClick={handleLogout}
>
Logout
</div>
)}
</div>
)}
</div>

1
web/src/lib/constants.ts Normal file
View File

@ -0,0 +1 @@
export const DISABLE_AUTH = process.env.DISABLE_AUTH?.toLowerCase() === "true";