<?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: Janos Bakos</title>
    <description>The latest articles on DEV Community by Janos Bakos (@baxy5).</description>
    <link>https://dev.to/baxy5</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%2F807002%2Fb6706a55-3618-4461-ab57-e13988ed925c.jpg</url>
      <title>DEV Community: Janos Bakos</title>
      <link>https://dev.to/baxy5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/baxy5"/>
    <language>en</language>
    <item>
      <title>NextJs 14 + Kinde Setup | Authentication/Authorization</title>
      <dc:creator>Janos Bakos</dc:creator>
      <pubDate>Sun, 12 May 2024 22:04:14 +0000</pubDate>
      <link>https://dev.to/baxy5/nextjs-14-kinde-setup-authenticationauthorization-36e8</link>
      <guid>https://dev.to/baxy5/nextjs-14-kinde-setup-authenticationauthorization-36e8</guid>
      <description>&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%2Fwc6pksm9i28cvvvjp43q.jpg" 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%2Fwc6pksm9i28cvvvjp43q.jpg" alt="Image description" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The 90% of the websites uses some kind of authentication or authorization. Such as Facebook, Instagram, Google etc. In some point this kind of solution is inevitable to use in your website or service.&lt;/p&gt;

&lt;p&gt;This guide will show you Kinde's auth service, that you can setup in minutes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requirements:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NextJS 13 or higher&lt;/li&gt;
&lt;li&gt;Kinde auth&lt;/li&gt;
&lt;li&gt;that's it, simple&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What Kinde offers you:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy login/signup with many socials (Google, Apple, Slack…)&lt;/li&gt;
&lt;li&gt;Multi-factor auth (MFA)&lt;/li&gt;
&lt;li&gt;Password and passwordless auth&lt;/li&gt;
&lt;li&gt;Dashboard (managing users)&lt;/li&gt;
&lt;li&gt;Site protection/authorization&lt;/li&gt;
&lt;li&gt;And so many great things…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's get started. I'm going to show you how can you setup Kinde in NextJS environment in few steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new NextJS Project:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Choose './src' directory option&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Finally your project's folder strcuture is going to be look like this -&amp;gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/public
/src
  /app
next.config.js
.gitignore

and more.. but these are the important right now.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Install Kinde with NPM (also works with YARN)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @kinde-oss/kinde-auth-nextjs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create '.env' file and copy-paste environment variables&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Go to Admin -&amp;gt; [Your application] -&amp;gt; Details -&amp;gt; Quick start -&amp;gt; Scroll down a little bit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will find something like this with your keys:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;KINDE_CLIENT_ID=""
KINDE_CLIENT_SECRET=""
KINDE_ISSUER_URL=""
KINDE_SITE_URL=http://localhost:3000
KINDE_POST_LOGOUT_REDIRECT_URL=http://localhost:3000
KINDE_POST_LOGIN_REDIRECT_URL=http://localhost:3000/dashboard
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now, let's create a '.env' file in the root of your project file&lt;/li&gt;
&lt;li&gt;Copy-paste the variables&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;You should also include '.env' file in your '.gitignore' file&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Using the API&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, you must create a route file in your API folder, to handle all the routing&lt;/p&gt;

&lt;p&gt;How can you do that? Let me show you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In your '/app' folder create an '/api' folder&lt;/li&gt;
&lt;li&gt;Then, in your '/api' folder create '/auth' folder&lt;/li&gt;
&lt;li&gt;Next, in your '/auth' folder create an another folder called '[kindeAuth]'&lt;/li&gt;
&lt;li&gt;And last but not least, create in this folder a 'route.js' file and copy paste this code:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { handleAuth } from "@kinde-oss/kinde-auth-nextjs/server";
export const GET = handleAuth();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you followed the steps right, you will get this structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/app
  /api
    /auth
      /[kindeAuth]
        route.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, we almost finished all the configuration, we:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Created a NextJS project&lt;/li&gt;
&lt;li&gt;Installed Kinde library&lt;/li&gt;
&lt;li&gt;Copy-pasted the envarionment variables&lt;/li&gt;
&lt;li&gt;Setup a route file&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;And there is one extra configuration which will be save a lot of headaches from you.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Go to your 'next.config.js' file&lt;/li&gt;
&lt;li&gt;And include this code in your nextConfig object:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @type {import('next').NextConfig} */
const nextConfig = {
  images: {
    remotePatterns: [
      {
        protocol: "https",
        hostname: "lh3.googleusercontent.com",
      },
    ],
  },
};

module.exports = nextConfig;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;It will be important, when you try to login with Google. (If you want to use other platforms, you also have to include its pattern here.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  And now let's make a simple login/logout and signup application.
&lt;/h2&gt;

&lt;p&gt;Let's import the necessary functions and components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { LoginLink, LogoutLink, RegisterLink} from "@kinde-oss/kinde-auth-nextjs/components";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: getKindeServerSession is a server function, you won't be able to use in client component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Look at this basic example:&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 {
  RegisterLink,
  LoginLink,
  LogoutLink,
} from "@kinde-oss/kinde-auth-nextjs/components";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
import Link from "next/link";

export default async function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {

  const { isAuthenticated, getUser } = getKindeServerSession();
  const user = await getUser();

  return (
    &amp;lt;html lang="en"&amp;gt;
      &amp;lt;body&amp;gt;
        &amp;lt;header&amp;gt;
          &amp;lt;nav className="nav container"&amp;gt;
            &amp;lt;h1 className="text-display-3"&amp;gt;KindeAuth&amp;lt;/h1&amp;gt;
            &amp;lt;div&amp;gt;
              {!(await isAuthenticated()) ? (
                &amp;lt;&amp;gt;
                  &amp;lt;LoginLink className="btn btn-ghost sign-in-btn"&amp;gt;
                    Sign in
                  &amp;lt;/LoginLink&amp;gt;
                  &amp;lt;RegisterLink className="btn btn-dark"&amp;gt;Sign up&amp;lt;/RegisterLink&amp;gt;
                &amp;lt;/&amp;gt;
              ) : (
                &amp;lt;div className="profile-blob"&amp;gt;
                  {user?.picture ? (
                    &amp;lt;img
                      className="avatar"
                      src={user?.picture}
                      alt="user profile avatar"
                      referrerPolicy="no-referrer"
                    /&amp;gt;
                  ) : (
                    &amp;lt;div className="avatar"&amp;gt;
                      {user?.given_name?.[0]}
                      {user?.family_name?.[0]}
                    &amp;lt;/div&amp;gt;
                  )}
                  &amp;lt;div&amp;gt;
                    &amp;lt;p className="text-heading-2"&amp;gt;
                      {user?.given_name} {user?.family_name}
                    &amp;lt;/p&amp;gt;

                    &amp;lt;LogoutLink className="text-subtle"&amp;gt;Log out&amp;lt;/LogoutLink&amp;gt;
                  &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
              )}
            &amp;lt;/div&amp;gt;
          &amp;lt;/nav&amp;gt;
        &amp;lt;/header&amp;gt;
        &amp;lt;main&amp;gt;{children}&amp;lt;/main&amp;gt;
      &amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;getUser - it will collect informations such as Profile Image on Google, and your name&lt;/p&gt;

&lt;p&gt;isAuthenticated - check if the user signed up and signed in.&lt;/p&gt;

&lt;p&gt;Thank you for reading this quickKinde and NextJS guide, I hope I could help you. Follow me for more. (part 2 is coming with more possibilities of Kinde)&lt;/p&gt;

&lt;p&gt;Take care :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Checkout our new design &lt;a href="https://www.whiz.hu/" rel="noopener noreferrer"&gt;click here&lt;/a&gt;!
&lt;/h2&gt;

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