This is a Next.js project bootstrapped with create-next-app
.
Please check the MongoDB installation documentation to install MongoDB on your own local machine for testing
Make sure MongoDB CLI tools are installed. Run npm run dump
to export datastore, and run npm run restore
to import. The git repo already contains mock data from Samuel's computer, so for most people, just run the restore script.
Make sure your MongoDB server is running
Run the development server:
npm run dev
# or
yarn dev
# or
pnpm dev
Open http://localhost:3000 with your browser to see the result.
Authentication is handled with the next-auth
library. You will need to set the GOOGLE_ID
and GOOGLE_SECRET
environment variables, and create your own secret with openssl rand -base64 32
and put into NEXTAUTH_SECRET
.
Page routes are protected server side, which means we utilize the getServerSideProps
to check if the user is logged in and redirect accordingly.
Insert this code to protect a page (example: see page.tsx
):
import protectedGetServerSideProps from '@/src/utils/protectRoute';
export const getServerSideProps = protectedGetServerSideProps;
// ... your page
API routes are protected with a function that will not only fetch the session, but will return the correct 401 error response with message.
Insert this code at the beginning of your handler function (example: see api/users.tsx
):
const session = await protectApiRoute(req, res);
if (!session) return;
// ... your handler