Logo

dev-resources.site

for different kinds of informations.

Integrating GitHub Authentication with NextAuth.js: A Step-by-Step Guide

Published at
9/19/2024
Categories
authjs
nextjs
supabase
prisma
Author
heinhtoo
Categories
4 categories in total
authjs
open
nextjs
open
supabase
open
prisma
open
Author
8 person written this
heinhtoo
open
Integrating GitHub Authentication with NextAuth.js: A Step-by-Step Guide

I am going to share my experience about integrating GitHub authentication with NextAuth.js.

Previous Post: Implementing auth.js v5 with Prisma and Supabase in Next.js

My GitHub Repo for next-auth Repo

Step 1: Create an OAuth App on GitHub

Go to this link and create new OAuth App.

OAuth app configure

You can add home page URL as http://localhost:3000

Note. GitHub only provides single Authorization callback URL so we need to create two OAuth app, one for localhost and one for production.

Authorization callback URL should point to Next.js API routes where NextAuth.js handles authentication

http://localhost:3000/api/auth/callback/github
Enter fullscreen mode Exit fullscreen mode

Copy the Client ID and Client Secret for environment variables.


![Secrets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cpu1lim6jscv4748c9oo.png)
Enter fullscreen mode Exit fullscreen mode

Step 2: Adding Environment variables

Create environment variables in .env.local by adding the following environment variables

AUTH_GITHUB_ID=your-github-client-id
AUTH_GITHUB_SECRET=your-github-client-secret
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure NextAuth.js for GitHub

In previous post, we added auth.js like this

// auth.ts
import NextAuth from "next-auth"
import { PrismaAdapter } from "@auth/prisma-adapter"
import { prisma } from "@/prisma"
export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: PrismaAdapter(prisma),
  providers: [],
})
Enter fullscreen mode Exit fullscreen mode

Now we are going to add GitHub provider

// auth.ts
import NextAuth from "next-auth"
import { PrismaAdapter } from "@auth/prisma-adapter"
import { prisma } from "@/prisma"
import GitHubProvider from "next-auth/providers/github";

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: PrismaAdapter(prisma),
  providers: [
    GitHubProvider({
      clientId: process.env.AUTH_GITHUB_ID ?? "",
      clientSecret: process.env.AUTH_GITHUB_SECRET ?? "",
    })
  ],
})
Enter fullscreen mode Exit fullscreen mode

That's all the part of setting up.

Step 4: Login with GitHub

You can either login from http://localhost:3000/api/auth/signin or you can create your custom sign-in page.

If you want to customize the sign-in experience, you can create a custom sign-in page.

First, create a file at app/sign-in/page.tsx

import { signIn } from 'next-auth/react'

export default function SignIn() {
  return (
    <div>
      <h1>Sign In</h1>
      <button onClick={() => signIn('github')}>Sign in with GitHub</button>
    </div>
  )
}
Enter fullscreen mode Exit fullscreen mode

And that's all for setting up with GitHub integration in Auth.js

Happy Coding ^_^

authjs Article's
30 articles in total
Favicon
Authentication System Using NodeJS
Favicon
Add Authjs to Next.js 15 app router with GitHub Authentication
Favicon
Master Authentication with Auth.js, Next.js, and PostgreSQL: A Comprehensive Guide
Favicon
Nuxt Authorization: How to Implement Team Role-Based Access Control in Nuxt 3
Favicon
Mastering Authentication in Next.js: A Step-by-Step Guide to GitHub Login with Auth.js
Favicon
User Authentication with Auth.js in Next.js App Router
Favicon
Lucia Auth is getting deprected
Favicon
Integrating GitHub Authentication with NextAuth.js: A Step-by-Step Guide
Favicon
Simple Next.js Magic Link JWT Authentication with Prisma, PostgreSQL, and Resend
Favicon
Password Authentication with Auth.js in Astro and Customizing Session Information (auth-astro)
Favicon
Basic Authentication for Nuxt.js (JSON Web Token + Local Storage)
Favicon
Implementing Federated Sign-Out with Auth.js in Next.js 14 App Router
Favicon
Integrating LinkedIn Authentication with NextAuth.js: A Step-by-Step Guide
Favicon
Implementing auth.js v5 with Prisma and Supabase in Next.js
Favicon
Auth, OAuth, and Auth0: What is what?
Favicon
JWT Authentication and Cookie Management in Web Applications
Favicon
๐Ÿš€ Exciting News!
Favicon
Data Persistence (Cookies, Sessions, Tokens, LocalStorage and SessionStorage)
Favicon
Fashion website
Favicon
The Firebase Shortcut: Simplifying Next.js Authentication
Favicon
Authentication system in Next.Js using Auth.js
Favicon
Roles based authentication using Nextauth and next.js
Favicon
Authentication & Authorization
Favicon
Top User Authentication Tools for Developers
Favicon
Comprehensive Guide to SvelteKitAuth: Secure Authentication for SvelteKit Apps
Favicon
Building a Secure OTP-based Login System in Next.js
Favicon
Implementing Secure Authentication in Next.js with JWT and MongoDB. Protect Routes using middleware
Favicon
Next.js 14 and NextAuth v4 : Credentials Authentication A Detailed Step-by-Step Guide
Favicon
Building a Secure OTP-based Login System in Next.js
Favicon
Web3Auth(ๆฌกใฎjs)ใ‚’ไฝฟ็”จใ—ใŸXRP Ledgerใ‚ขใ‚ซใ‚ฆใƒณใƒˆใฎไฝœๆˆ:ใ‚นใƒ†ใƒƒใƒ—ใƒใ‚คใ‚นใƒ†ใƒƒใƒ—ใ‚ฌใ‚คใƒ‰

Featured ones: