<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hein Htoo</title>
    <description>The latest articles on DEV Community by Hein Htoo (@heinhtoo).</description>
    <link>https://dev.to/heinhtoo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1298740%2F601d7a1c-affd-4b7f-a8a3-d8fe9ab760e9.png</url>
      <title>DEV Community: Hein Htoo</title>
      <link>https://dev.to/heinhtoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/heinhtoo"/>
    <language>en</language>
    <item>
      <title>Integrating GitHub Authentication with NextAuth.js: A Step-by-Step Guide</title>
      <dc:creator>Hein Htoo</dc:creator>
      <pubDate>Thu, 19 Sep 2024 01:59:38 +0000</pubDate>
      <link>https://dev.to/heinhtoo/integrating-github-authentication-with-nextauthjs-a-step-by-step-guide-1fo4</link>
      <guid>https://dev.to/heinhtoo/integrating-github-authentication-with-nextauthjs-a-step-by-step-guide-1fo4</guid>
      <description>&lt;p&gt;I am going to share my experience about integrating GitHub authentication with NextAuth.js. &lt;/p&gt;

&lt;p&gt;Previous Post: &lt;a href="https://dev.to/heinhtoo/implementing-authjs-v5-with-prisma-and-supabase-in-nextjs-lie"&gt;Implementing auth.js v5 with Prisma and Supabase in Next.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My GitHub Repo for next-auth &lt;a href="https://github.com/heinhtoo/next-auth-template" rel="noopener noreferrer"&gt;Repo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create an OAuth App on GitHub
&lt;/h2&gt;

&lt;p&gt;Go to this &lt;a href="https://github.com/settings/developers" rel="noopener noreferrer"&gt;link&lt;/a&gt; and create new OAuth App.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29kqgzldypwmx995tl8q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29kqgzldypwmx995tl8q.png" alt="OAuth app configure" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can add home page URL as &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note. GitHub only provides single Authorization callback URL so we need to create two OAuth app, one for localhost and one for production.&lt;/p&gt;

&lt;p&gt;Authorization callback URL should point to Next.js API routes where NextAuth.js handles authentication&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:3000/api/auth/callback/github
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the Client ID and Client Secret for environment variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
![Secrets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cpu1lim6jscv4748c9oo.png)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Adding Environment variables
&lt;/h2&gt;

&lt;p&gt;Create environment variables in &lt;code&gt;.env.local&lt;/code&gt; by adding the following environment variables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AUTH_GITHUB_ID=your-github-client-id
AUTH_GITHUB_SECRET=your-github-client-secret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Configure NextAuth.js for GitHub
&lt;/h2&gt;

&lt;p&gt;In previous post, we added auth.js like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 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: [],
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are going to add GitHub provider&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 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 ?? "",
    })
  ],
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all the part of setting up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Login with GitHub
&lt;/h2&gt;

&lt;p&gt;You can either login from &lt;code&gt;http://localhost:3000/api/auth/signin&lt;/code&gt; or you can create your custom sign-in page.&lt;/p&gt;

&lt;p&gt;If you want to customize the sign-in experience, you can create a custom sign-in page. &lt;/p&gt;

&lt;p&gt;First, create a file at &lt;code&gt;app/sign-in/page.tsx&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { signIn } from 'next-auth/react'

export default function SignIn() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Sign In&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; signIn('github')}&amp;gt;Sign in with GitHub&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's all for setting up with GitHub integration in Auth.js&lt;/p&gt;

&lt;p&gt;Happy Coding ^_^&lt;/p&gt;

</description>
      <category>authjs</category>
      <category>nextjs</category>
      <category>supabase</category>
      <category>prisma</category>
    </item>
    <item>
      <title>Integrating LinkedIn Authentication with NextAuth.js: A Step-by-Step Guide</title>
      <dc:creator>Hein Htoo</dc:creator>
      <pubDate>Wed, 18 Sep 2024 12:15:53 +0000</pubDate>
      <link>https://dev.to/heinhtoo/integrating-linkedin-authentication-with-nextauthjs-a-step-by-step-guide-4dmg</link>
      <guid>https://dev.to/heinhtoo/integrating-linkedin-authentication-with-nextauthjs-a-step-by-step-guide-4dmg</guid>
      <description>&lt;p&gt;I am going to share my experience about integrating linkedin authentication with NextAuth.js. &lt;/p&gt;

&lt;p&gt;Previous Post: &lt;a href="https://dev.to/heinhtoo/implementing-authjs-v5-with-prisma-and-supabase-in-nextjs-lie"&gt;Implementing auth.js v5 with Prisma and Supabase in Next.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My GitHub Repo for next-auth &lt;a href="https://github.com/heinhtoo/next-auth-template" rel="noopener noreferrer"&gt;Repo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create LinkedIn Application
&lt;/h2&gt;

&lt;p&gt;To allow Next.js application to use LinkedIn as an authentication provider, first create an app inside &lt;a href="https://developer.linkedin.com/" rel="noopener noreferrer"&gt;LinkedIn Developer Portal&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is pretty straight forward but you will need to create LinkedIn Page&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fep0i93y0mc5hi61mdrj2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fep0i93y0mc5hi61mdrj2.png" alt="LinkedIn App setup"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that go to &lt;code&gt;Auth&lt;/code&gt; section and configure your redirect URLs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv223iphr6r8vnjsrf7kd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv223iphr6r8vnjsrf7kd.png" alt="Adding redirect URLs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The redirect url should point to Next.js API routes where NextAuth.js handles authentication&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

http://localhost:3000/api/auth/callback/linkedin


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Copy the Client ID and Client Secret for environment variables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frbr6ne2qotv8nwoyguzj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frbr6ne2qotv8nwoyguzj.png" alt="Secrets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Adding Environment variables
&lt;/h2&gt;

&lt;p&gt;Create environment variables in &lt;code&gt;.env.local&lt;/code&gt; by adding the following environment variables&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

AUTH_LINKEDIN_ID=your-linkedin-client-id
AUTH_LINKEDIN_SECRET=your-linkedin-client-secret


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 3: Configure NextAuth.js for LinkedIn
&lt;/h2&gt;

&lt;p&gt;In previous post, we added auth.js like this&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// 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: [],
})


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now we are going to add LinkedIn provider&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: PrismaAdapter(prisma),
  providers: [
    LinkedInProvider({
      clientId: process.env.AUTH_LINKEDIN_ID ?? "",
      clientSecret: process.env.AUTH_LINKEDIN_SECRET ?? "",
    })
  ],
})


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That's all the part of setting up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Login with LinkedIn
&lt;/h2&gt;

&lt;p&gt;You can either login from &lt;code&gt;http://localhost:3000/api/auth/signin&lt;/code&gt; or you can create your custom sign-in page.&lt;/p&gt;

&lt;p&gt;If you want to customize the sign-in experience, you can create a custom sign-in page. &lt;/p&gt;

&lt;p&gt;First, create a file at &lt;code&gt;app/sign-in/page.tsx&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

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

export default function SignIn() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Sign In&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; signIn('linkedin')}&amp;gt;Sign in with LinkedIn&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And that's all for setting up with LinkedIn integration in Auth.js&lt;/p&gt;

&lt;p&gt;Happy Coding ^_^&lt;/p&gt;

</description>
      <category>authjs</category>
      <category>nextjs</category>
      <category>supabase</category>
      <category>prisma</category>
    </item>
    <item>
      <title>Implementing auth.js v5 with Prisma and Supabase in Next.js</title>
      <dc:creator>Hein Htoo</dc:creator>
      <pubDate>Tue, 17 Sep 2024 06:28:31 +0000</pubDate>
      <link>https://dev.to/heinhtoo/implementing-authjs-v5-with-prisma-and-supabase-in-nextjs-lie</link>
      <guid>https://dev.to/heinhtoo/implementing-authjs-v5-with-prisma-and-supabase-in-nextjs-lie</guid>
      <description>&lt;p&gt;With the new version auth.js v5 on the way, I am going to share my knowledge about step-by-step integrating it with Prisma and Supabase in Next.js. &lt;/p&gt;

&lt;p&gt;Version&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"next-auth": "^5.0.0-beta.20"&lt;/li&gt;
&lt;li&gt;"@prisma/client": "^5.19.1"&lt;/li&gt;
&lt;li&gt;"&lt;a class="mentioned-user" href="https://dev.to/auth"&gt;@auth&lt;/a&gt;/prisma-adapter": "^2.4.2"&lt;/li&gt;
&lt;li&gt;"prisma": "^5.19.1"&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting up Prisma
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Install &lt;a href="https://prisma.io/docs/getting-started/quickstart" rel="noopener noreferrer"&gt;Prisma&lt;/a&gt;
&lt;code&gt;npm install prisma --save-dev&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Initialize prisma &lt;code&gt;npx prisma init&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;This will creates a new &lt;strong&gt;prisma&lt;/strong&gt; directory with a &lt;strong&gt;schema.prisma&lt;/strong&gt; file and configures &lt;strong&gt;PostgresSQL&lt;/strong&gt; as your database also creating &lt;strong&gt;.env&lt;/strong&gt; file with &lt;strong&gt;DATABASE_URL&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Initialize Supabase
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create organization in supabase (optional if you are individual developer)&lt;/li&gt;
&lt;li&gt;Create database&lt;/li&gt;
&lt;li&gt;Get connection string by clicking connect button.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpyshmsb8dq225u0kfbzy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpyshmsb8dq225u0kfbzy.png" alt="Connect with supabase" width="722" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Click ORMS and in Prisma, copy the .env.local into .env&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fow3zlc84udgd5ohk5b02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fow3zlc84udgd5ohk5b02.png" alt="Env for Prisma" width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click prisma/schema.prisma and copy the code inside your prisma.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnu3l46uaccoupgllrihk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnu3l46uaccoupgllrihk.png" alt="Prisma Schema" width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note. Prisma doesn't work on .env.local and it only works with .env so make sure to change the correct .env file&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting NextAuth
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Install &lt;a href="https://authjs.dev/getting-started/installation" rel="noopener noreferrer"&gt;auth.js v5&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Adding NEXTAUTH_SECRET in env file &lt;code&gt;npx auth secret&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add auth.ts file under src folder
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// auth.ts
import NextAuth from 'next-auth';
export const { handlers, auth, signIn, signOut } = NextAuth({
  providers: [],
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add a route handler under &lt;code&gt;app/api/auth/[...nextauth]/route.ts&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/api/auth/[...nextauth]/route.ts
import { handlers } from "@/auth" // Referring to the auth.ts we just created
export const { GET, POST } = handlers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add a middleware to keep session alive.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// middleware.ts
export { auth as middleware } from "@/auth"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add &lt;a href="https://authjs.dev/getting-started/adapters/prisma" rel="noopener noreferrer"&gt;PrismaAdapter&lt;/a&gt;
The Prisma Adapter in NextAuth.js is a tool that allows NextAuth to store and manage authentication data (such as user accounts, sessions, verification tokens, etc.) in a Prisma-managed database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install @prisma/client @auth/prisma-adapter&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To improve performance using Prisma ORM, we can set up the Prisma instance to ensure only one instance is created throughout the project and then import it from any file as needed. This approach avoids recreating instances of PrismaClient every time it is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// prisma.ts
import { PrismaClient } from "@prisma/client"
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient }
export const prisma = globalForPrisma.prisma || new PrismaClient()
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add prisma Adapter in auth.ts
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 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: [],
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Define database tables for authentication by adding this in prisma/schema.prisma
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// prisma/schema.prisma
model User {
  id            String          @id @default(cuid())
  firstName     String?
  lastName      String?
  email         String          @unique
  password      String? // Make sure this field exists in your schema
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  // Optional for WebAuthn support
  Authenticator Authenticator[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
model Account {
  userId            String
  type              String
  provider          String
  providerAccountId String
  refresh_token     String?
  access_token      String?
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String?
  session_state     String?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)
  @@id([provider, providerAccountId])
}
model Session {
  sessionToken String   @unique
  userId       String
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
model VerificationToken {
  identifier String
  token      String
  expires    DateTime
  @@id([identifier, token])
}
// Optional for WebAuthn support
model Authenticator {
  credentialID         String  @unique
  userId               String
  providerAccountId    String
  credentialPublicKey  String
  counter              Int
  credentialDeviceType String
  credentialBackedUp   Boolean
  transports           String?
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)
  @@id([userId, credentialID])
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Perform migration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In prisma, migrations need to perform except MongoDB.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;migration&lt;/strong&gt; in the context of databases refers to the process of modifying the database schema in a controlled, versioned manner. This allows developers to make changes to the database structure (such as adding or removing tables, changing columns, or creating relationships) while keeping track of the changes. Migrations help ensure consistency across environments (like development, testing, and production) and make it easier to manage schema changes over time.&lt;/p&gt;

&lt;p&gt;And in order to create SQL migration file,&lt;br&gt;
&lt;code&gt;npx prisma migrate dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note: Migration need to perform only when the schema changes&lt;/p&gt;

&lt;p&gt;This will take a while if we are using Supabase URL and will prompt to ask migration name and after finished running this command, all the changes in schema will be updated in supabase and also creating migrations folder inside prisma/migrations &lt;/p&gt;

&lt;p&gt;Note: In order to sync the migrations with supabase, don’t delete migrations folder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx prisma migrate dev&lt;/code&gt; will also generate the Prisma client so the developer who is using this command may not need to run &lt;code&gt;npx prisma generate.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But developers who pull from git will need to run &lt;code&gt;npx prisma generate&lt;/code&gt; first to generate Prisma client.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting SessionProvider
&lt;/h2&gt;

&lt;p&gt;We need to set session provider so that the client can use &lt;code&gt;useSession&lt;/code&gt; hook.&lt;/p&gt;

&lt;p&gt;Note: Need to add SessionProvider for all parent layout&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/layout.tsx
import type { Metadata } from "next";
import localFont from "next/font/local";
import "./globals.css";
import { SessionProvider } from "next-auth/react";
import { auth } from "@/auth";

const geistSans = localFont({
  src: "./fonts/GeistVF.woff",
  variable: "--font-geist-sans",
  weight: "100 900",
});
const geistMono = localFont({
  src: "./fonts/GeistMonoVF.woff",
  variable: "--font-geist-mono",
  weight: "100 900",
});

export const metadata: Metadata = {
  title: "Create Next App",
  description: "Generated by create next app",
};

export default async function RootLayout({
  children,
}: Readonly&amp;lt;{
  children: React.ReactNode;
}&amp;gt;) {
  const session = await auth();

  return (
    &amp;lt;html lang="en"&amp;gt;
      &amp;lt;body
        className={`${geistSans.variable} ${geistMono.variable} antialiased`}
      &amp;gt;
        &amp;lt;SessionProvider session={session}&amp;gt;{children}&amp;lt;/SessionProvider&amp;gt;
      &amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Calling session from server pages or from api
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { auth } from '@/auth';
const session = await auth();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Calling session from client components
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use client";
import { useSession } from 'next-auth/react';
const { data: session, status } = useSession();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By combining the power of Prisma, Auth.js v5 and Supabase (as database), we can easily manage authentication in a Next.js app with minimal configuration but I haven't added any providers yet. This is the initial setup for now and will share about others providers later.&lt;/p&gt;

&lt;p&gt;My GitHub Repo for next-auth &lt;a href="https://github.com/heinhtoo/next-auth-template" rel="noopener noreferrer"&gt;Repo&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Integrating LinkedIn Authentication &lt;a href="https://dev.to/heinhtoo/integrating-linkedin-authentication-with-nextauthjs-a-step-by-step-guide-4dmg"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Integrating GitHub Authentication &lt;a href="https://dev.to/heinhtoo/integrating-github-authentication-with-nextauthjs-a-step-by-step-guide-1fo4"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy Coding ^_^&lt;/p&gt;

</description>
      <category>authjs</category>
      <category>nextjs</category>
      <category>prisma</category>
      <category>supabase</category>
    </item>
    <item>
      <title>Deploy React App to GitHub Pages using GitHub Actions</title>
      <dc:creator>Hein Htoo</dc:creator>
      <pubDate>Wed, 24 Jul 2024 06:09:22 +0000</pubDate>
      <link>https://dev.to/heinhtoo/deploy-react-app-to-github-pages-using-github-actions-2g9g</link>
      <guid>https://dev.to/heinhtoo/deploy-react-app-to-github-pages-using-github-actions-2g9g</guid>
      <description>&lt;p&gt;Deploy your React app seamlessly to &lt;strong&gt;GitHub Pages&lt;/strong&gt; using &lt;strong&gt;GitHub Actions&lt;/strong&gt;. With this setup, every push to your repository's main branch triggers an automated workflow that builds your app and publishes it to GitHub Pages. This ensures your latest updates are always live, without manual intervention. By leveraging &lt;strong&gt;gh-pages&lt;/strong&gt; and a simple YAML configuration, you can maintain a smooth and efficient deployment pipeline directly from your GitHub repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating an app
&lt;/h2&gt;

&lt;p&gt;Create react app if you don't have one by typing this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app my-app --template typescript
cd myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add &lt;a href="https://www.npmjs.com/package/gh-pages" rel="noopener noreferrer"&gt;gh-pages&lt;/a&gt; library
&lt;/h3&gt;

&lt;p&gt;gh-pages library is a simple tool for publishing files to a &lt;code&gt;gh-pages&lt;/code&gt; branch on GitHub, which GitHub Pages uses to serve static websites.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install gh-pages --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add home page in package.json
&lt;/h3&gt;

&lt;p&gt;Specifying homepage tells React where the app will be hosted. It helps in generating the correct URLs for assets in the built files, ensuring everything works correctly when the app is deployed to &lt;code&gt;GitHub Pages&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"homepage": "https://{github_username}.github.io/{repo_name}",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "myapp,
  "version": "0.1.0",
  "private": true,
  "homepage": "https://{github_username}.github.io/{repo_name}",
  "dependencies": {
    "@testing-library/jest-dom": "^5.17.0",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.5.2",
    "@types/node": "^16.18.101",
    "@types/react": "^18.3.3",
    "@types/react-dom": "^18.3.0",
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "react-scripts": "5.0.1",
    "typescript": "^4.9.5",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "deploy": "gh-pages -d build"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      "&amp;gt;0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "gh-pages": "^6.1.1",
    "tailwindcss": "^3.4.4"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deploying to GitHub Pages
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Changing GitHub Repository Settings
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create &lt;code&gt;public&lt;/code&gt; repository in order to deploy to &lt;code&gt;GitHub Pages&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Push your code to the &lt;code&gt;GitHub&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add -A
git commit -m "init proj"
git remote add origin https://github.com/USERNAME/REPOSITORY_NAME.git
git push -u origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Allowing read and write permissions in settings -&amp;gt; Actions -&amp;gt; General.
This is to allow write access to gh-pages Branch. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31kcmkelcm5yuewzco52.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31kcmkelcm5yuewzco52.png" alt="Allowing read and write permissions" width="800" height="568"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create workflow yml file under &lt;code&gt;.github/workflows/action.yml&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Deploy App to GitHub Pages

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [20.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
      - uses: actions/checkout@v4
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"
      - run: npm ci
      - run: npm run build --if-present
      - run: npm test
      - name: Deploy with gh-pages
        run: |
          git remote set-url origin https://git:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
          npx gh-pages -d build -u "github-actions-bot &amp;lt;support+actions@github.com&amp;gt;"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;After that commit the changes and push to &lt;code&gt;GitHub&lt;/code&gt; and let the action run.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .github
git commit -m "add workflow"
git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can see the actions running in &lt;code&gt;Actions&lt;/code&gt; tab in GitHub.&lt;/li&gt;
&lt;li&gt;After that push, go to settings -&amp;gt; pages
Change Branch to &lt;code&gt;gh-pages &amp;amp; /root&lt;/code&gt; and save.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvhuoz5njn78a8faytajh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvhuoz5njn78a8faytajh.png" alt="Change branch" width="800" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7j27hshcbkxjjlyclbg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7j27hshcbkxjjlyclbg.png" alt="Highlight" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This will create another action -&amp;gt; pages-build-deployment.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fww5n2mdpzd8rapfmjmdb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fww5n2mdpzd8rapfmjmdb.png" alt="Another action" width="800" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once completed, &lt;code&gt;https://{github_username}.github.io/{repo_name}&lt;/code&gt; can be accessed. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By leveraging &lt;code&gt;GitHub Actions&lt;/code&gt; and &lt;code&gt;gh-pages&lt;/code&gt;, you can maintain a robust and automated pipeline for deploying your React applications. &lt;/p&gt;

</description>
      <category>react</category>
      <category>githubactions</category>
      <category>github</category>
    </item>
  </channel>
</rss>
