{"id":328369,"date":"2022-04-11T10:09:22","date_gmt":"2022-04-11T10:09:22","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=328369"},"modified":"2026-02-27T11:31:05","modified_gmt":"2026-02-27T11:31:05","slug":"how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/","title":{"rendered":"How to implement Authentication in Nextjs with Magento 2 using NextAuth"},"content":{"rendered":"\n<p>In this blog, we will discuss how to implement authentication in Nextjs with Magento 2 using the NextAuth credentials provider.<\/p>\n\n\n\n<p>When you need to add authentication to your next.js project, NextAuth is a wonderful option. It&#8217;s easy to see why, given its extensive provider support, which includes GitHub, Google, Facebook, Credentials, and more. It can help you set up your authentication in minutes!<\/p>\n\n\n\n<p>However, sometimes you might need to implement your own custom backend with an email\/password login with an API (<a href=\"https:\/\/webkul.com\/blog\/magento2-custom-rest-api\/\">Application Programming Interface<\/a>) server.<\/p>\n\n\n\n<p>To implement the backend, you can use the credentials provider. We are going to use the NextAuth credentials provider to authenticate Nextjs with Magento. <\/p>\n\n\n\n<p>we hope this will help you out if you&#8217;re in the same boat!<\/p>\n\n\n\n<p>Also, <a href=\"https:\/\/webkul.com\/magento2-headless-development\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Headless Development<\/a>&nbsp;services will allow you to gain access to the headless development architecture.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create your Project<\/h2>\n\n\n\n<p>We recommend creating a new Next.js app using, which sets up everything automatically for you. To create a project, Open a command prompt or terminal window in the location where you wish to save your project and run the following command.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">npx create-next-app@latest\n# or\nyarn create next-app\n# or\npnpm create next-app<\/pre>\n\n\n\n<p>After the installation is complete:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Run&nbsp;<code>npm run dev<\/code>&nbsp;or&nbsp;<code>yarn dev<\/code>&nbsp;to start the development server on&nbsp;<code>http:\/\/localhost:3000<\/code><\/li>\n\n\n\n<li>Visit&nbsp;<code>http:\/\/localhost:3000<\/code>&nbsp;to view your application and see the following.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"664\" height=\"668\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png\" alt=\"Screenshot-from-2022-04-09-17-09-55\" class=\"wp-image-328370\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png 664w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55-298x300.png 298w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55-248x249.png 248w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55-120x120.png 120w\" sizes=\"(max-width: 664px) 100vw, 664px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Welcome to NextJs<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Let&#8217;s Start Setting up the API<\/h2>\n\n\n\n<p>Now let&#8217;s add the NextAuth package by running the below command.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">yarn add next-auth<\/pre>\n\n\n\n<p>This time to add NEXTAUTH URL in our .env file. Add the following to your .env file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">NEXTAUTH_URL=http:\/\/localhost:3000<\/pre>\n\n\n\n<p>First, we need to set up the next-auth for the app. It&#8217;s a straightforward process and the instructions can be found&nbsp;<a href=\"https:\/\/next-auth.js.org\/getting-started\/example\">here<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why we are using Magento?<\/h2>\n\n\n\n<p>Magento 2 CMS offers versatile features to build a dynamic and powerful e-store. This CMS enables you to build a robust and highly secured e-store. <\/p>\n\n\n\n<p>Magento CMS can create and manage multiple storefronts with one Admin Panel. Magento CMS supports multiple languages, multiple currencies, and multiple pricing.<\/p>\n\n\n\n<p>Moreover, you can create a headless storefront using the best available frameworks such as <a href=\"https:\/\/webkul.com\/magento-2-react-development-services\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 ReactJS<\/a> or NextJS or GatsbyJS and many more.<\/p>\n\n\n\n<p>Now we have to set up&nbsp;<code>pages\/api\/[..nextauth].js<\/code>&nbsp; in our project. This contains the NextAuth.js dynamic route handler.<\/p>\n\n\n\n<p>Add MAGENTO_URL in our .env file. Add the following to your .env file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">MAGENTO_URL=http:\/\/mymageServer.com<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\">import NextAuth from &quot;next-auth&quot;;\nimport CredentialProvider from &quot;next-auth\/providers\/credentials&quot;;\n\nexport default NextAuth({\n  providers: &#091;\n    CredentialProvider({\n      async authorize(credentials) {\n        const baseUrl = process.env.MAGENTO_URL;\n        const response = await fetch(\n          baseUrl + &quot;\/rest\/V1\/integration\/customer\/token&quot;,\n          {\n            method: &quot;POST&quot;,\n            body: JSON.stringify(credentials),\n            headers: {\n              &quot;Content-Type&quot;: &quot;application\/json&quot;,\n            },\n          }\n        );\n        const data = await response.json();\n        \/\/ Returning token to set in session\n        return {\n            token: data,\n          };\n      },\n    }),\n  ],\n  callbacks: {\n    jwt: async ({ token, user }) =&gt; {\n      user &amp;&amp; (token.user = user);\n      return token;\n    },\n    session: async ({ session, token }) =&gt; {\n      session.user = token.user;  \/\/ Setting token in session\n      return session;\n    },\n  },\n  pages: {\n    signIn: &quot;\/login&quot;, \/\/Need to define custom login page (if using)\n  },\n});<\/pre>\n\n\n\n<p>All requests to&nbsp;<code>\/api\/auth\/*<\/code>&nbsp;(<code>signIn<\/code>,&nbsp;<code>callback<\/code>,&nbsp;<code>signOut<\/code>, etc.) will automatically be handled by NextAuth.js.<\/p>\n\n\n\n<p><strong>Further Reading<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>See the&nbsp;<a href=\"https:\/\/next-auth.js.org\/configuration\/options\">options documentation<\/a>&nbsp;for more details on how to configure providers, databases, and other options.<\/li>\n\n\n\n<li>Read more about how to add authentication providers&nbsp;<a href=\"https:\/\/next-auth.js.org\/providers\">here<\/a>.<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s create your login page which in our case is&nbsp;<code>pages\/login.js<\/code>&nbsp;will have a form submit handler that will use nextauth&#8217;s <code>signIn()<\/code>&nbsp;callback to control if a user is allowed to sign in.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">import { signIn } from &#039;next-auth\/client&#039;\n\nconst handleLogin = () =&gt; {\n    signIn(&#039;credentials&#039;,\n      {\n        email,\n        password,\n        \/\/ The page where you want to redirect to after a \n        \/\/ successful login\n        callbackUrl: `${window.location.origin}\/account_page` \n      }\n    )\n  }<\/pre>\n\n\n\n<p>At this point, if you have entered the correct credentials and your API endpoint is working as you&#8217;d hope it to work, you should be able to log in just fine.<\/p>\n\n\n\n<p>But if there is any issue such as the server being down, invalid credentials, etc. you will be redirected to the default error page (like the image below).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"884\" height=\"483\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-35-45.png\" alt=\"Screenshot-from-2022-04-09-17-35-45\" class=\"wp-image-328371\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-35-45.png 884w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-35-45-300x164.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-35-45-250x137.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-35-45-768x420.png 768w\" sizes=\"(max-width: 884px) 100vw, 884px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Error page<\/figcaption><\/figure>\n\n\n\n<p>Instead what I want is to redirect it to my custom login page and explain the situation in a bit more detail to the user. So here&#8217;s what we do, we can stop the redirection using the below code on the login page.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">const res = await signIn(&#039;credentials&#039;,\n      {\n        email,\n        password,\n        callbackUrl: `${window.location.origin}\/account_page` \n        redirect: false,\n      }\n    )\nif (res?.error) handleError(res.error)\nif (res.url) router.push(res.url);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to get token from the session?<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">using useSession()<\/h2>\n\n\n\n<p>The NextAuth.js client library makes it easy to interact with sessions from React applications.<\/p>\n\n\n\n<p>We can get token from session using useSession()<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Client Side:&nbsp;<strong>Yes<\/strong><\/li>\n\n\n\n<li>Server Side: No<\/li>\n<\/ul>\n\n\n\n<p>The&nbsp;<code>useSession()<\/code>&nbsp;React Hook in the NextAuth.js client is the easiest way to check if someone is signed in.<\/p>\n\n\n\n<p>Make sure that&nbsp;<a href=\"https:\/\/next-auth.js.org\/getting-started\/client#sessionprovider\"><code>&lt;SessionProvider&gt;<\/code><\/a>&nbsp;is added to&nbsp;<code>pages\/_app.js<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">import { useSession } from &quot;next-auth\/react&quot;\n\nexport default function Component() {\n  const { data: session, status } = useSession()\n\n  if (status === &quot;authenticated&quot;) {\n    return &lt;p&gt;Signed in user token {session.user.token}&lt;\/p&gt;\n  }\n\n  return &lt;a href=&quot;\/api\/auth\/signin&quot;&gt;Sign in&lt;\/a&gt;\n}<\/pre>\n\n\n\n<p><code>useSession()<\/code>&nbsp;returns an object containing two values:&nbsp;<code>data<\/code>&nbsp;and&nbsp;<code>status<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>data<\/code><\/strong>: This can be three values:&nbsp;<a href=\"https:\/\/github.com\/nextauthjs\/next-auth\/blob\/8ff4b260143458c5d8a16b80b11d1b93baa0690f\/types\/index.d.ts#L437-L444\" target=\"_blank\" rel=\"noreferrer noopener\"><code>Session<\/code><\/a>&nbsp;\/&nbsp;<code>undefined<\/code>&nbsp;\/&nbsp;<code>null<\/code>.\n<ul class=\"wp-block-list\">\n<li>when the session hasn&#8217;t been fetched yet,&nbsp;<code>data<\/code>&nbsp;will&nbsp;<code>undefined<\/code><\/li>\n\n\n\n<li>in case it failed to retrieve the session,&nbsp;<code>data<\/code>&nbsp;will be&nbsp;<code>null<\/code><\/li>\n\n\n\n<li>in case of success,&nbsp;<code>data<\/code>&nbsp;will be&nbsp;<a href=\"https:\/\/github.com\/nextauthjs\/next-auth\/blob\/8ff4b260143458c5d8a16b80b11d1b93baa0690f\/types\/index.d.ts#L437-L444\" target=\"_blank\" rel=\"noreferrer noopener\"><code>Session<\/code><\/a>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>status<\/code><\/strong>: enum mapping to three possible session states:&nbsp;<code>\"loading\" | \"authenticated\" | \"unauthenticated\"<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Using getSession()<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Client Side:&nbsp;<strong>Yes<\/strong><\/li>\n\n\n\n<li>Server Side:&nbsp;<strong>Yes<\/strong><\/li>\n<\/ul>\n\n\n\n<p>NextAuth.js provides a&nbsp;<code>getSession()<\/code>&nbsp;method which can be called client or server-side to return a session.<\/p>\n\n\n\n<p>It calls&nbsp;<code>\/api\/auth\/session<\/code>&nbsp;and returns a promise with a session object, or null if no session exists.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Client Side Example<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\">async function myFunction() {\n  const session = await getSession()\n  \/* ... *\/\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Server Side Example<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\">import { getSession } from &quot;next-auth\/react&quot;\n\nexport default async (req, res) =&gt; {\n  const session = await getSession({ req })\n  \/* ... *\/\n  res.end()\n}<\/pre>\n\n\n\n<p><strong>Note:<\/strong> When calling&nbsp;<code>getSession()<\/code>&nbsp;server-side, you need to pass&nbsp;<code>{req}<\/code>&nbsp;or&nbsp;<code>context<\/code>&nbsp;object.<\/p>\n\n\n\n<p>For more detailed info: &nbsp;<a href=\"https:\/\/next-auth.js.org\/tutorials\/securing-pages-and-api-routes\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"><code>visit here<\/code><\/a><\/p>\n\n\n\n<p>That&#8217;s all about the NextAuth. If you have any questions please comment below, and we will try to respond to you. <\/p>\n\n\n\n<p>You may also <a href=\"https:\/\/webkul.com\/hire-magento-developers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Hire Magento 2 Developers<\/a> for <a href=\"https:\/\/webkul.com\/headless-commerce-development-services\/\">Headless development<\/a> on Nextjs with Magento 2<\/p>\n\n\n\n<p>Thanks \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will discuss how to implement authentication in Nextjs with Magento 2 using the NextAuth credentials provider. When you need to add authentication to your next.js project, NextAuth is a wonderful option. It&#8217;s easy to see why, given its extensive provider support, which includes GitHub, Google, Facebook, Credentials, and more. It can <a href=\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":377,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121],"tags":[2460,12572,12571],"class_list":["post-328369","post","type-post","status-publish","format-standard","hentry","category-magento-2","tag-magento-2","tag-next-js","tag-nextauth"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Implement NextAuth using credentials provider with Magento 2<\/title>\n<meta name=\"description\" content=\"When you need to add authentication to your next.js project, NextAuth is a wonderful option. It&#039;s easy to see why, given its extensive ...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Implement NextAuth using credentials provider with Magento 2\" \/>\n<meta property=\"og:description\" content=\"When you need to add authentication to your next.js project, NextAuth is a wonderful option. It&#039;s easy to see why, given its extensive ...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-04-11T10:09:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-27T11:31:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png\" \/>\n<meta name=\"author\" content=\"Rahul Chaudhary\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rahul Chaudhary\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/\"},\"author\":{\"name\":\"Rahul Chaudhary\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/07d5b40e4a4b5c6996d171e134826b75\"},\"headline\":\"How to implement Authentication in Nextjs with Magento 2 using NextAuth\",\"datePublished\":\"2022-04-11T10:09:22+00:00\",\"dateModified\":\"2026-02-27T11:31:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/\"},\"wordCount\":813,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png\",\"keywords\":[\"Magento 2\",\"Next.js\",\"NextAuth\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/\",\"name\":\"Implement NextAuth using credentials provider with Magento 2\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png\",\"datePublished\":\"2022-04-11T10:09:22+00:00\",\"dateModified\":\"2026-02-27T11:31:05+00:00\",\"description\":\"When you need to add authentication to your next.js project, NextAuth is a wonderful option. It's easy to see why, given its extensive ...\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png\",\"width\":664,\"height\":668,\"caption\":\"Screenshot-from-2022-04-09-17-09-55\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to implement Authentication in Nextjs with Magento 2 using NextAuth\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/07d5b40e4a4b5c6996d171e134826b75\",\"name\":\"Rahul Chaudhary\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/40e957d926aaa241dc1de7dd09cfea4a0c86ed9fa29bb7eda51de1a8967864e7?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/40e957d926aaa241dc1de7dd09cfea4a0c86ed9fa29bb7eda51de1a8967864e7?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Rahul Chaudhary\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/rahulchaudhary766\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Implement NextAuth using credentials provider with Magento 2","description":"When you need to add authentication to your next.js project, NextAuth is a wonderful option. It's easy to see why, given its extensive ...","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/","og_locale":"en_US","og_type":"article","og_title":"Implement NextAuth using credentials provider with Magento 2","og_description":"When you need to add authentication to your next.js project, NextAuth is a wonderful option. It's easy to see why, given its extensive ...","og_url":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2022-04-11T10:09:22+00:00","article_modified_time":"2026-02-27T11:31:05+00:00","og_image":[{"url":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png","type":"","width":"","height":""}],"author":"Rahul Chaudhary","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Rahul Chaudhary","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/"},"author":{"name":"Rahul Chaudhary","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/07d5b40e4a4b5c6996d171e134826b75"},"headline":"How to implement Authentication in Nextjs with Magento 2 using NextAuth","datePublished":"2022-04-11T10:09:22+00:00","dateModified":"2026-02-27T11:31:05+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/"},"wordCount":813,"commentCount":2,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png","keywords":["Magento 2","Next.js","NextAuth"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/","url":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/","name":"Implement NextAuth using credentials provider with Magento 2","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png","datePublished":"2022-04-11T10:09:22+00:00","dateModified":"2026-02-27T11:31:05+00:00","description":"When you need to add authentication to your next.js project, NextAuth is a wonderful option. It's easy to see why, given its extensive ...","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2022\/04\/Screenshot-from-2022-04-09-17-09-55.png","width":664,"height":668,"caption":"Screenshot-from-2022-04-09-17-09-55"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-implement-authentication-in-nextjs-with-magento2-using-credentials-provider\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to implement Authentication in Nextjs with Magento 2 using NextAuth"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/07d5b40e4a4b5c6996d171e134826b75","name":"Rahul Chaudhary","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/40e957d926aaa241dc1de7dd09cfea4a0c86ed9fa29bb7eda51de1a8967864e7?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/40e957d926aaa241dc1de7dd09cfea4a0c86ed9fa29bb7eda51de1a8967864e7?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Rahul Chaudhary"},"url":"https:\/\/webkul.com\/blog\/author\/rahulchaudhary766\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/328369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/377"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=328369"}],"version-history":[{"count":13,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/328369\/revisions"}],"predecessor-version":[{"id":528617,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/328369\/revisions\/528617"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=328369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=328369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=328369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}