next-auth-example signs you in with GitHub and gives you one form, for writing a post with a title and a body. Submitting it sends the post to an API route, which saves it for the signed-in user. There’s no list of posts: the form clears when the save succeeds.
It was started in June 2021, five days after nextjs-auth-example, from a fresh Create Next App rather than a copy of it. It was worked on in three short bursts, in June, August and September 2021, and it hasn’t changed since. The README is the Create Next App default.
How it works
Without a session, the app sends you to NextAuth’s built-in sign-in page, which has a single GitHub button. Signed in, you get a blue bar with a Sign out button and the form. Formik validates the fields against a Yup schema, and once you leave a field or submit the form, Chakra UI marks the empty ones.
The form posts JSON to /api/posts. The route reads the session with getSession and answers 401 without one. Otherwise it finds the session’s row in the database by its access token, loads that session’s user, and checks the body against the same Yup schema the form uses, which it imports from the form’s component. A valid post is created with Prisma and returned. An invalid one gets a 500 with a short error.
GitHub ids as strings
The first version stored the GitHub account id as an integer, as nextjs-auth-example does. A day later, a commit titled “fixes issue with GitHub Int” changed the column to a string and gave the GitHub provider a profile callback that converts the id with toString(), falling back to the GitHub login when there’s no display name.
![Lines 6 to 31 of pages/api/auth/[...nextauth].js: NextAuth configured with the GitHub provider, whose profile callback, under the comment fixes issue where schema was changed to int, returns the id as profile.id.toString(), the name or else the login, the email and the avatar URL, followed by a JWT signing key from an environment variable, the database URL and the Prisma adapter.](/images/projects/next-auth-example-nextauth.webp)
How it changed
- June: GitHub sign-in, and a page that showed who you were signed in as. A second pass checked the session on the server in
getServerSidePropsand redirected to the sign-in page. - August: the
Postmodel, the/api/postsroute and a plain HTML form. - September 7: the route switched from finding the user by email to finding the session’s row first, the form moved to Formik and Yup and then to Chakra UI, and a cleanup moved the sign-in check into an
Authcomponent that wraps every page in the browser, replacing the server-side redirect.
How it’s built
It uses Next.js 11.0.0 with React 17, NextAuth 3.27 with a canary of its Prisma adapter, and Prisma 2.25 on PostgreSQL, with two migrations: NextAuth’s tables, then posts. The interface is Chakra UI 1.6 on Emotion 11 and Framer Motion 4, with Formik 2.2 and Yup 0.32 for the form.
Running it today
It still installs from its lockfile and runs with Node 14 and PostgreSQL: yarn install, npx prisma migrate deploy, then yarn dev. The settings go in .env, following .env.example, and signing in needs your own GitHub OAuth app. On Linux, Prisma 2.25’s engines also need the OpenSSL 1.1 libraries.
The creation date shown here is the GitHub repository’s UTC creation date. The screenshots and the terminal capture come from running the project locally from its source with Node 14 and PostgreSQL, signed in with a throwaway user and session inserted into the database, with sample post text. The code card shows a real source file from the repository.