Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom sign in page. #10

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -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',
},
},
);
5 changes: 4 additions & 1 deletion pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
41 changes: 41 additions & 0 deletions pages/auth/signin.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof getServerSideProps>) {
return (
<>
{providers &&
Object.values(providers).map((provider) => (
<div key={provider.name}>
<button onClick={() => signIn(provider.id)}>
Sign in with {provider.name}
</button>
</div>
))}
</>
);
}

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 ?? [] },
};
}