#Next-auth with app directory
1 messages · Page 1 of 1 (latest)
Based on the information provided in the extracts, here's how you can configure NextAuth.js for authentication with GitHub OAuth and JWT:
-
First, log into your GitHub account and navigate to Settings > Developer Settings > OAuth Apps. Click on "Register a new application" or "New OAuth App" to create a new OAuth application.
-
Fill out the registration form for your app. The Authorization callback URL should be the Next.js
/api/authroute. Note that the Authorization callback URL field only supports a single URL. If you want to deploy your app later with a production URL, you will need to set up a new GitHub OAuth app. -
After registering the application, you will find your newly generated Client ID and Client Secret. Copy and paste this info into the
.envfile in the root directory as theGITHUB_IDandGITHUB_SECRETenvironment variables. Also set theNEXTAUTH_URLto the same value of the Authorization callback URL that you configured on GitHub.
Your .env file might look like this:
# .env
# GitHub OAuth
GITHUB_ID=your_client_id
GITHUB_SECRET=your_client_secret
NEXTAUTH_URL=<http://localhost:3000/api/auth>
- If you're using JWT for session handling, you'll need to set up a
JWT_SECRET_KEYin your.env.localfile. This secret will be used to sign the JWTs.
# .env.local
JWT_SECRET_KEY=your_random_secret
- If you're using the
srcdirectory for your application code, move theappRouter folder orpagesRouter folder tosrc/apporsrc/pagesrespectively. Note that the/publicdirectory, config files likepackage.json,next.config.jsandtsconfig.json, and.env.*files should remain in the root of your project.
Please note that the specific configuration of callbacks for JWT and session handling is not provided in the extracts. For more detailed information, you may want to refer to the NextAuth.js documentation (https://next-auth.js.org/).
Sources:
- Vercel Guide (https://vercel.com/guides/nextjs-prisma-postgres)
- Next.js Documentation (https://nextjs.org/docs/app/building-your-application/configuring/src-directory)
- Vercel Template (https://vercel.com/templates/next.js/jwt-authentication)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@slender helm kapa.ai is still learning and improving, please let us how it did by reacting below