<?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: Aayush Kumar bhat</title>
    <description>The latest articles on DEV Community by Aayush Kumar bhat (@aayusbhat26).</description>
    <link>https://dev.to/aayusbhat26</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%2F1143429%2F9c74cc84-2f10-4c0a-9691-96a93fc6c273.jpeg</url>
      <title>DEV Community: Aayush Kumar bhat</title>
      <link>https://dev.to/aayusbhat26</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aayusbhat26"/>
    <language>en</language>
    <item>
      <title>How to integrate Clerk in Next JS application ?</title>
      <dc:creator>Aayush Kumar bhat</dc:creator>
      <pubDate>Tue, 22 Aug 2023 16:28:54 +0000</pubDate>
      <link>https://dev.to/aayusbhat26/how-to-integrate-clerk-in-next-js-application--4hba</link>
      <guid>https://dev.to/aayusbhat26/how-to-integrate-clerk-in-next-js-application--4hba</guid>
      <description>&lt;p&gt;Hello everyone, in this article you will go through the simple process of integrating &lt;strong&gt;Clerk&lt;/strong&gt; as 3rd party user authentication in your &lt;strong&gt;Next JS&lt;/strong&gt; application. &lt;/p&gt;

&lt;p&gt;Note: For the learning purpose, If you haven't build any authentication from scratch, first learn the basics of working of user authentication and build atleast one or two authentication pages.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Clerk&lt;/strong&gt; is nothing but a 3rd party authentication service. Clerk is a secure way to control access to digital platforms or apps. It ensures that only authorized users can use these services. Think of it like a virtual bouncer at a club entrance – it checks IDs (login credentials) to verify who's allowed in. Clerk authentication uses techniques like passwords, biometrics (like fingerprints), and single sign-on to confirm a user's identity. This prevents unauthorized access and safeguards sensitive information. Just like a lock and key, Clerk authentication keeps digital spaces safe from unwanted visitors while letting the right people enjoy the benefits.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                         ## LET'S START 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;&lt;strong&gt;STEP 1&lt;/strong&gt;: &lt;strong&gt;Creating Next JS application!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using the command &lt;code&gt;npx create-next-app ./folderName&lt;/code&gt;, generate a basic Next JS application and folder structure. Make sure that you enter the same as I did. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fyxxszr119ht17xklws8k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fyxxszr119ht17xklws8k.png" alt=" " width="738" height="261"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the folder structure is generated, we can move to the next step.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;STEP 2&lt;/strong&gt;: &lt;strong&gt;Creating an Account in Clerk!&lt;/strong&gt;&lt;br&gt;
Visit to &lt;a href="https://dashboard.clerk.com/sign-up" rel="noopener noreferrer"&gt;THIS&lt;/a&gt; link and create a new account.&lt;/p&gt;

&lt;p&gt;Now it will redirect you to the Application page which would look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fkosp8n3g0s212d8p8ydk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fkosp8n3g0s212d8p8ydk.png" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clerk offers more than 19 methods for authentication.&lt;br&gt;
Some of them are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Google &lt;/li&gt;
&lt;li&gt;Facebook &lt;/li&gt;
&lt;li&gt;GitHub&lt;/li&gt;
&lt;li&gt;Apple &lt;/li&gt;
&lt;li&gt;LinkedIn &lt;/li&gt;
&lt;li&gt;Mircosoft
and so on...
Give a name to the application and select the required methods.
Click on &lt;strong&gt;CREATE APPLICATION&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fg44ee4zm7n5afmvsvixx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fg44ee4zm7n5afmvsvixx.png" alt=" " width="719" height="764"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;STEP 3&lt;/strong&gt;: &lt;strong&gt;SELECTION OF FRAMEWORK TO BE USED&lt;/strong&gt;&lt;br&gt;
This tutorial is for Next JS, so I will be selecting Next.js only.&lt;br&gt;
Otherwise it offers 5+ frameworks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fgz8s4tmoupxoxmysn7ay.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgz8s4tmoupxoxmysn7ay.png" alt=" " width="800" height="188"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;STEP 4&lt;/strong&gt;: &lt;strong&gt;CREATING .env file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now in the root folder of your project create a .env file then add &lt;strong&gt;CLERK_SECRET_KEY&lt;/strong&gt; and &lt;strong&gt;NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY&lt;/strong&gt; &lt;br&gt;
These keys would be different in your case, so make sure that you add yours only.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Remeber to add the .env file in the .gitignore file. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fnc6jl9a0ih4xg8yx2tng.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnc6jl9a0ih4xg8yx2tng.png" alt=" " width="800" height="204"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;STEP 5&lt;/strong&gt;: &lt;strong&gt;INSTALLING CLERK&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install clerk in your project depending on the framework you have choosed.&lt;br&gt;
In this case, we are going Next, if you are using the same make sure you enter the same.&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 @clerk/nextjs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F5ymfiz63i236p9sh5fys.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5ymfiz63i236p9sh5fys.png" alt=" " width="768" height="152"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;STEP 6&lt;/strong&gt;: &lt;strong&gt;MOUNTING THE CLERK&lt;/strong&gt;&lt;br&gt;
For mounting the clerk on entire application we need to import ClerkProvider and wrap it around our main app. &lt;/p&gt;

&lt;p&gt;Go to ./"ProjectName"/app/layout.tsx&lt;/p&gt;

&lt;p&gt;layout.tsx &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initially&lt;/strong&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 './globals.css'
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'

const inter = Inter({ subsets: ['latin'] })

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

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    &amp;lt;html lang="en"&amp;gt;
      &amp;lt;body className={inter.className}&amp;gt;{children}&amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After wrapping the application with ClerkProvider.&lt;br&gt;
We need to import the *&lt;em&gt;ClerkProvider *&lt;/em&gt; from @/clerk/nextjs as a named import.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import { ClerkProvider } from '@clerk/nextjs'
import './globals.css'
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'

const inter = Inter({ subsets: ['latin'] })

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

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    &amp;lt;ClerkProvider&amp;gt;
      &amp;lt;html lang="en"&amp;gt;
        &amp;lt;body className={inter.className}&amp;gt;{children}&amp;lt;/body&amp;gt;
      &amp;lt;/html&amp;gt;
    &amp;lt;/ClerkProvider&amp;gt;
  )
}

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

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;STEP 7&lt;/strong&gt;: &lt;strong&gt;ADDING MIDDLEWARE&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For the protection of our project or application while authentication, we need to a middleware.&lt;/p&gt;

&lt;p&gt;Create a file in the root directory of your project as &lt;strong&gt;middleware.ts&lt;/strong&gt; copy and paste the follwiing code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import { authMiddleware } from "@clerk/nextjs";

export default authMiddleware({});

export const config = {
  matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};


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

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;STEP 8&lt;/strong&gt;: &lt;strong&gt;ADDING SIGNUP AND LOGIN PAGES&lt;/strong&gt;&lt;br&gt;
*&lt;em&gt;PAY ATTENTION *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Clerk requires a parituclar folder structure.&lt;/p&gt;

&lt;p&gt;i. Create 2 folders named as "(auth)" and "(root)".&lt;/p&gt;

&lt;p&gt;ii. Inside (root), create a new file as page.tsx. This file is the main file of the application.&lt;/p&gt;

&lt;p&gt;iii. Inside (auth), create another folder named as "(routes)".&lt;/p&gt;

&lt;p&gt;iv. Inside (routes) create 2 more folder as "sign-in"and "sign-up".&lt;/p&gt;

&lt;p&gt;v. Inside sign-in and sign-up create new folders named as [[...sign-in]] and [[...sign-up]] respectively.&lt;/p&gt;

&lt;p&gt;vi. Create files both named as page.tsx in sign-in and sign-up.&lt;/p&gt;

&lt;p&gt;Now copy the code respectively.&lt;/p&gt;

&lt;p&gt;For page.tsx in [[...sign-in]]:&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 "@clerk/nextjs";

export default function Page() {
      return &amp;lt;SignIn /&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For page.tsx in [[...sign-up]]:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { SignUp } from "@clerk/nextjs";

export default function Page() {
      return &amp;lt;SignUp /&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the end, your folder structure for auth should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fnjom619073pboq6m320j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnjom619073pboq6m320j.png" alt=" " width="428" height="441"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;STEP 9&lt;/strong&gt;: "ADDING ENVIRONMENT VARIABLES IN .env file"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CONGRATS!!! *&lt;em&gt;YOU HAVE SUCCESSFULLY INTEGRATED CLERK WITH YOUR APPLICATION *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;!(&lt;a href="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0d0cuhw17zmd2nyzc2nv.png" rel="noopener noreferrer"&gt;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0d0cuhw17zmd2nyzc2nv.png&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;In order to center the login/signup modal or box, we need to add the layout for auth too. &lt;/p&gt;

&lt;p&gt;*Each route group can have its own layout.&lt;/p&gt;

&lt;p&gt;For adding a layout, you can do the following: &lt;/p&gt;

&lt;p&gt;i. Create a file named as layout.tsx. Empty file will make next.js thrown an error, since next expects a layout file to return a function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fp537yij5gl1ha5bsviax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fp537yij5gl1ha5bsviax.png" alt=" " width="800" height="39"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function AuthLayout({
      children
}: {
      children: React.ReactNode
}) {
      return (
            &amp;lt;div className="flex items-center justify-center h-full"&amp;gt;{children}&amp;lt;/div&amp;gt;
      )
}
// this file is responsible for the layout of entire auth page.

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;FINAL RESULT&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fl8to3l32tshvqhfyfeya.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fl8to3l32tshvqhfyfeya.png" alt=" " width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Faw4dsoa1jdytchbyv6dn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Faw4dsoa1jdytchbyv6dn.png" alt=" " width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
