diff --git a/middleware.ts b/middleware.ts index 48c15edcc0..bc49d1933b 100644 --- a/middleware.ts +++ b/middleware.ts @@ -1,21 +1,29 @@ import { withAuth } from 'next-auth/middleware'; import { NextResponse } from 'next/server'; -import { MAINTENANCE_MODE } from './utils/app/const'; +import { MAINTENANCE_MODE } from '@/utils/app/const'; -export default withAuth(function middleware(req) { - if (MAINTENANCE_MODE) { - req.nextUrl.pathname = `/maintenance`; - return NextResponse.rewrite(req.nextUrl); - } - const username = req.nextauth.token?.email; - if (username) { - const requestHeaders = new Headers(req.headers); - requestHeaders.set('x-user', username); - return NextResponse.next({ - request: { - headers: requestHeaders, - }, - }); - } -}); + +export default withAuth( + function middleware(req) { + if (MAINTENANCE_MODE) { + req.nextUrl.pathname = `/maintenance`; + return NextResponse.rewrite(req.nextUrl); + } + const username = req.nextauth.token?.email; + if (username) { + const requestHeaders = new Headers(req.headers); + requestHeaders.set('x-user', username); + return NextResponse.next({ + request: { + headers: requestHeaders, + }, + }); + } + }, + { + pages: { + signIn: '/auth/signin', + }, + }, +); \ No newline at end of file diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts index 60e567c029..2c604dd2f8 100644 --- a/pages/api/auth/[...nextauth].ts +++ b/pages/api/auth/[...nextauth].ts @@ -22,7 +22,10 @@ export const authOptions: NextAuthOptions = { session: { strategy: 'jwt', }, + pages: { + signIn: '/auth/signin', + }, // debug: true, }; -export default NextAuth(authOptions); +export default NextAuth(authOptions); \ No newline at end of file diff --git a/pages/auth/signin.tsx b/pages/auth/signin.tsx new file mode 100644 index 0000000000..907edf9bbf --- /dev/null +++ b/pages/auth/signin.tsx @@ -0,0 +1,41 @@ +import { getProviders, signIn } from 'next-auth/react'; + +import { GetServerSidePropsContext, InferGetServerSidePropsType } from 'next'; +import { getServerSession } from 'next-auth'; + +import { authOptions } from '@/pages/api/auth/[...nextauth]'; + + +export default function SignIn({ + providers, +}: InferGetServerSidePropsType) { + return ( + <> + {providers && + Object.values(providers).map((provider) => ( +
+ +
+ ))} + + ); +} + +export async function getServerSideProps({ + req, + res, +}: GetServerSidePropsContext) { + const session = await getServerSession(req, res, authOptions); + + // If the user is already logged in, redirect. + if (session) { + return { redirect: { destination: '/' } }; + } + + const providers = await getProviders(); + return { + props: { providers: providers ?? [] }, + }; +} \ No newline at end of file