Add authentication

This commit is contained in:
JuliusMoehring
2023-11-07 17:25:40 +01:00
parent f1fdeb422b
commit 3942f511cf
8 changed files with 657 additions and 8 deletions

View File

@@ -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 };

View File

@@ -0,0 +1,5 @@
"use client";
import { SessionProvider } from "next-auth/react";
export default SessionProvider;

View File

@@ -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 (
<html lang="en">
<head>
@@ -36,7 +40,9 @@ export default function RootLayout({
<script src="/serviceWorkerRegister.js" defer></script>
<script src="/redirect.js" defer></script>
</head>
<body>{children}</body>
<body>
<SessionProvider session={session}>{children}</SessionProvider>
</body>
</html>
);
}

17
app/login/page.tsx Normal file
View File

@@ -0,0 +1,17 @@
"use client";
import { signIn } from "next-auth/react";
export default function Login() {
const handleLogin = async () => {
signIn("azure-ad", {
callbackUrl: "/",
});
};
return (
<div>
<button onClick={handleLogin}>Login</button>
</div>
);
}