,

NextJS 13 Authentication Made Simple with next-auth and GitHub Oauth

Posted by








Authentication with NextJS 13 – Quick & Easy

Authentication with NextJS 13 – Quick & Easy

Authentication is a crucial part of any web application. With the release of NextJS 13, authentication has become even easier with the introduction of next-auth and its support for GitHub Oauth.

Setting up next-auth with GitHub Oauth

To get started with authentication using next-auth and GitHub Oauth, you’ll first need to install the necessary packages:


npm install next-auth
npm install next-auth/providers

Next, you’ll need to configure the next-auth provider in your NextJS app. This can be done by creating a pages/api/auth/[...nextauth].js file and adding the following code:

    // pages/api/auth/[...nextauth].js

    import NextAuth from 'next-auth'
    import Providers from 'next-auth/providers'

    export default NextAuth({
      providers: [
        Providers.GitHub({
          clientId: process.env.GITHUB_CLIENT_ID,
          clientSecret: process.env.GITHUB_CLIENT_SECRET
        })
      ]
    })
  

After setting up the provider, you can now use the next-auth hooks and components in your application to handle authentication. For example, you can use the useSession hook to access the user’s session information and use the signIn and signOut methods to handle user authentication:

    import { useSession, signIn, signOut } from 'next-auth/react'

    function MyComponent() {
      const { data: session } = useSession()

      return (
        
{session ? : }
) }

Conclusion

With the release of NextJS 13 and the introduction of next-auth with support for GitHub Oauth, implementing authentication in your NextJS app has become quick and easy. By following the steps outlined in this article, you can add authentication to your app in no time, allowing your users to securely log in with their GitHub accounts.


0 0 votes
Article Rating
11 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Steve Giorgi
10 months ago

Stefen, I think if you build out a project and then do a full on tutorial, it will pull in a minimum of 100k. I would recommend a clone but not one that has been done to death. Hit me up if you're interested in hearing some of my ideas. I think you're a hidden gem, I may have a formula to really get you going. I notice you've dropped out for a while, and I know why — you've put a lot of time and effort into your channel and haven't seen the returns I think you deserve.

林冠廷
10 months ago

Why it route me to api/auth/error?

Egovernance India
10 months ago

Ty

DeadHorse
10 months ago

Great and helpful tutorial. On the new routing system with the App folder has some little changes but still thank you for share the content

Oscar David Díaz
10 months ago

Very helpful, thank you!

anjan obalesh
10 months ago

Does any one integrate next js with one login I’m struggling and there might be something wrong with one login

Uzochukwu Emeka
10 months ago

Thanks man

GG FF
10 months ago

These youtubers are using page router and here the funckin hell , i am not getting a single working video of this rtk and package on the latest next js version😡😡😡

Nicolás Scarafia
10 months ago

It was really helpful, thank you!

Oniriik dev
10 months ago

Super Video thx ! can you implement mongodb saving for auth ?

Hamza Ashraf
10 months ago

Hi can i ask you something why you are not using experimental app directory in this tutorial i'm looking next-auth configurations with experimental app directory