From 3942f511cf33e3e93317d0aa1cec1b981d0ab198 Mon Sep 17 00:00:00 2001 From: JuliusMoehring <44407680+JuliusMoehring@users.noreply.github.com> Date: Tue, 7 Nov 2023 17:25:40 +0100 Subject: [PATCH 1/6] Add authentication --- app/api/auth/[...nextauth]/route.ts | 42 +++ app/components/session-provider.tsx | 5 + app/layout.tsx | 18 +- app/login/page.tsx | 17 + middleware.ts | 5 + package.json | 1 + src-tauri/Cargo.lock | 488 +++++++++++++++++++++++++++- yarn.lock | 89 +++++ 8 files changed, 657 insertions(+), 8 deletions(-) create mode 100644 app/api/auth/[...nextauth]/route.ts create mode 100644 app/components/session-provider.tsx create mode 100644 app/login/page.tsx create mode 100644 middleware.ts diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts new file mode 100644 index 000000000..c94e94f91 --- /dev/null +++ b/app/api/auth/[...nextauth]/route.ts @@ -0,0 +1,42 @@ +import NextAuth, { getServerSession, type NextAuthOptions } from "next-auth"; +import AzureADProvider from "next-auth/providers/azure-ad"; + +/** + * Options for NextAuth.js used to configure adapters, providers, callbacks, etc. + * + * @see https://next-auth.js.org/configuration/options + */ +export const authOptions: NextAuthOptions = { + callbacks: { + session: ({ session, token }) => ({ + ...session, + user: { + ...session.user, + id: token.sub, + }, + }), + }, + providers: [ + AzureADProvider({ + clientId: process.env.AZURE_AD_CLIENT_ID ?? "", + clientSecret: process.env.AZURE_AD_CLIENT_SECRET ?? "", + tenantId: process.env.AZURE_AD_TENANT_ID ?? "", + }), + ], + pages: { + signIn: "/login", + signOut: "/login", + error: "/login", + }, +}; + +/** + * Wrapper for `getServerSession` so that you don't need to import the `authOptions` in every file. + * + * @see https://next-auth.js.org/configuration/nextjs + */ +export const getServerAuthSession = () => getServerSession(authOptions); + +const handler = NextAuth(authOptions); + +export { handler as GET, handler as POST }; diff --git a/app/components/session-provider.tsx b/app/components/session-provider.tsx new file mode 100644 index 000000000..82bd8ae6d --- /dev/null +++ b/app/components/session-provider.tsx @@ -0,0 +1,5 @@ +"use client"; + +import { SessionProvider } from "next-auth/react"; + +export default SessionProvider; diff --git a/app/layout.tsx b/app/layout.tsx index 9ea973825..7820263bf 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -1,9 +1,11 @@ /* eslint-disable @next/next/no-page-custom-font */ -import "./styles/globals.scss"; -import "./styles/markdown.scss"; -import "./styles/highlight.scss"; -import { getClientConfig } from "./config/client"; import { type Metadata } from "next"; +import { getServerSession } from "next-auth"; +import SessionProvider from "./components/session-provider"; +import { getClientConfig } from "./config/client"; +import "./styles/globals.scss"; +import "./styles/highlight.scss"; +import "./styles/markdown.scss"; export const metadata: Metadata = { title: "AdExGPT Web", @@ -23,11 +25,13 @@ export const metadata: Metadata = { }, }; -export default function RootLayout({ +export default async function RootLayout({ children, }: { children: React.ReactNode; }) { + const session = await getServerSession(); + return (
@@ -36,7 +40,9 @@ export default function RootLayout({ - {children} + ++ Before you can start using AdEx GPT you have to sign in using your AdEx + account. After a successful sign in you will be redirected to the chat. +
+ + {session.status === "authenticated" ? ( + +- Before you can start using AdEx GPT you have to sign in using your AdEx - account. After a successful sign in you will be redirected to the chat. -
- - {session.status === "authenticated" ? ( - -