🚀 Authenticate Your Next.js App with GitHub in 5 Easy Steps! 🤖 #141055
Replies: 1 comment
-
New post |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi everyone,
Need to let users sign in with GitHub on your Next.js app? Look no further! We're gonna get your app authenticated using NextAuth.js in a way so simple, even your pet could do it! 🐶 (Okay, maybe not that simple, but almost.)
Let’s goooo! 🏃♂️💨
1. Install NextAuth.js 📦
First, let’s install the magic. Open up your terminal and run:
Your project is now officially NextAuth-ified! 🪄✨
2. Create a GitHub OAuth App 🐙
Head to GitHub Developer Settings and hit “New OAuth App”. Fill in the deets:
http://localhost:3000
(for local dev)http://localhost:3000/api/auth/callback/github
(this is where GitHub will send users after they log in)Boom! 💥 GitHub will give you a Client ID and a Client Secret (Top-secret stuff! 🤫).
3. Configure NextAuth.js Like a Pro 🖥️
Now, let’s connect your GitHub app to Next.js. Create a file called
[...nextauth].js
inpages/api/auth
(yeah, those brackets are totally normal):mkdir -p pages/api/auth && touch pages/api/auth/[...nextauth].js
Then, in
[...nextauth].js
, set up your GitHub provider:Add the following secrets to your
.env.local
:🤫 PRO TIP: Keep those secrets safe. Don’t go pushing them to GitHub!
4. Create the Login Button 🖱️ (Because Clicking Is Fun!)
What’s an app without a shiny "Sign In with GitHub" button? ✨
Here’s how you can add it:
Now you’ve got a simple, user-friendly "Login" button ready to go. Click it. Click it now. 🖱️
5. Protect Your Pages Like a Bouncer at the Club 🕶️🎟️
Want to block unauthorized users from accessing certain pages? You can add some gatekeeping with
getSession
:Now only logged-in peeps can access the dashboard. 🚪⛔
🔥 You're Done! 🚀
Run your app with:
Open
http://localhost:3000
, and watch the magic happen! 🪄 You’ll see a slick "Sign in with GitHub" button. Click it, authenticate, and access protected pages like a true VIP. 🎟️✨TL;DR 📝
[...nextauth].js
🛠️That’s it! You’re officially GitHub-authenticated, like a real tech wizard. 🧙♂️ Feel free to flex this on your dev friends (but not too hard, we don’t want to make them jealous). 😎
If you run into issues, let me know in the comments or open an issue on GitHub! or email me @beingshahidali@gmail.com
Beta Was this translation helpful? Give feedback.
All reactions