Sign in
Log inSign up
Next-auth with google provider in Next.js

Photo by Solen Feyissa on Unsplash

Next-auth with google provider in Next.js

Gitartha Kashyap's photo
Gitartha Kashyap
·Feb 9, 2022·

3 min read

At first, we need to create a firebase project, go to authentication, enable google sign in provider and get the web client id and secret

Screenshot from 2022-02-09 13-27-19.png

now we bootstrap a next js project with npx create-next-app@latest

we don't remove anything from the index file, we'll keep it as it is, mostly we will be focusing on the functional parts and the UI. After this, we install next auth.

edit the _app.js, wrap the entire thing with session providers from next-auth

Screenshot from 2022-02-09 13-40-11.png

Create a folder auth inside pages/api and then create [...nextauth].js file inside the auth folder

Screenshot from 2022-02-09 13-42-01.png

don't mess up the folder structure and the naming conventions, if you do so it will result in errors.

we create .env.local file in the root of the folder to store ower sensitive information. this is where we put ower google provider secret and id that we got from firebase

Screenshot from 2022-02-09 13-53-04.png

the NEXTAUTH_URL and JWT_SECRET are necessary we can read about it in the docs.

Screenshot from 2022-02-09 13-55-11.png

provider is an array, we can pass multiple providers like Twitter, Facebook, Spotify here we are working with google provider so we only pass one single provider

in the google provider we pass in two values the clientId and the client secret these are the same values we got from our firebase

inside the callbacks, we have a session that returns the user after signing in, as we have wrapped our application with a session provider this is available to the whole application. Lastly, we pass the secret.

now we got to our index.js file and edit it to this

Screenshot from 2022-02-09 14-08-44.png

In index.js we do ssr with getServerSideProps to get the providers and session and return it. The providers will return all the providers, in our case Google sign-in and session. We get the session from useSession hook in line no 8 and then in line no. 10 we check if the session is empty i.e now user logged in we return the login component with the providers passed as a prop

Screenshot from 2022-02-09 14-14-07.png

Inside the login.js, we map the providers, as I have mentioned we can provide multiple providers, so providers is an array. In line no 10, in the button we get the signIn function from next-auth which is built-in and works across all the providers.

Now when we run npm run dev we get this as an output in our browser

Screenshot from 2022-02-09 14-19-50.png

now when you will click on this button you will get an error

Screenshot from 2022-02-09 14-20-54.png

what we have to do is copy the redirect uri, got to our google cloud console, select our project, navigate to credentials, in credentials select Oauth 2.0 client

Screenshot from 2022-02-09 14-25-15.png

Inside Oauth 2.0, in the authorised redirect URL options, paste your redirect uri

Screenshot from 2022-02-09 14-26-21.png

now when you back and click on the sign in button it will show you with all the google accounts you have at the moment, choose one and it will sign you in

Screenshot from 2022-02-09 14-28-28.png

In our index.js we add a logout button to signout, we use the built in signout function from next-auth

Screenshot from 2022-02-09 14-29-25.png

now when you click on the logout button it will log you out.

the code is available in my github

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct