mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-11-23 09:16:49 +08:00
Add authentication
This commit is contained in:
42
app/api/auth/[...nextauth]/route.ts
Normal file
42
app/api/auth/[...nextauth]/route.ts
Normal 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 };
|
||||
5
app/components/session-provider.tsx
Normal file
5
app/components/session-provider.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
"use client";
|
||||
|
||||
import { SessionProvider } from "next-auth/react";
|
||||
|
||||
export default SessionProvider;
|
||||
@@ -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
17
app/login/page.tsx
Normal 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>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user