<?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: Manish Kr Prasad</title>
    <description>The latest articles on DEV Community by Manish Kr Prasad (@naprila).</description>
    <link>https://dev.to/naprila</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%2F954754%2F2171a1e8-625d-40f0-b6e5-fd2c543f69e4.png</url>
      <title>DEV Community: Manish Kr Prasad</title>
      <link>https://dev.to/naprila</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/naprila"/>
    <language>en</language>
    <item>
      <title>"Rate Limiting, Simplified": My Journey with Unkey, the Open-Source API Management Platform</title>
      <dc:creator>Manish Kr Prasad</dc:creator>
      <pubDate>Thu, 24 Oct 2024 20:58:38 +0000</pubDate>
      <link>https://dev.to/naprila/rate-limiting-simplified-my-journey-with-unkey-the-open-source-api-management-platform-11f</link>
      <guid>https://dev.to/naprila/rate-limiting-simplified-my-journey-with-unkey-the-open-source-api-management-platform-11f</guid>
      <description>&lt;p&gt;As a developer, one of the most rewarding experiences is bringing an idea to life. I recently embarked on a project called &lt;a href="https://github.com/Naprila/devTInder" rel="noopener noreferrer"&gt;devTinder&lt;/a&gt;, a platform designed to connect developers and tech enthusiasts for collaboration and networking. As I prepared for the deployment phase (only backend), I realized that ensuring fair usage of resources was paramount to the success of my project. I wanted to protect my application from potential misuse and abuse, so I decided to implement rate limiting using &lt;a href="https://www.unkey.com/" rel="noopener noreferrer"&gt;Unkey&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Unkey &amp;amp; How I came to know about Unkey?
&lt;/h2&gt;

&lt;p&gt;My journey with Unkey began during the ongoing Hacktoberfest 2024, an event that celebrates open-source contributions and encourages developers to collaborate on exciting projects. As I learned more about its capabilities, I realized that Unkey also offered exactly what I needed for devTinder ie rate-limiting. Now let's answer the question why Unkey:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Open Source:&lt;/strong&gt;&lt;br&gt;
Unkey is built on an open-source model, allowing developers to contribute, customize, and adapt the tool to fit their specific needs. This fosters a collaborative community that continuously improves the platform.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Protection Against DoS Attacks:&lt;/strong&gt;&lt;br&gt;
With robust rate limiting features and simple to integrate, Unkey can provides essential protection against Denial of Service (DoS) attacks. It prevent excessive or abusive use of a resource and to ensure that the resource is available to all legitimate users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Credit Card Required:&lt;/strong&gt;&lt;br&gt;
Getting started with Unkey is hassle-free—there’s no need to provide credit card information. This allows developers to explore the platform without any financial commitments, making it accessible for projects of all sizes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using Unkey rate-limiting feature
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to &lt;a href="https://app.unkey.com/settings/root-keys/new" rel="noopener noreferrer"&gt;https://app.unkey.com/settings/root-keys/new&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Give a name and in the &lt;strong&gt;Workspace&lt;/strong&gt; section make sure to check all options of &lt;strong&gt;Ratelimit&lt;/strong&gt;. Then click on &lt;strong&gt;Create new Key&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy your key, it will be shown as &lt;strong&gt;Your API key&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you feel lost, you can follow their well documented &lt;a href="https://www.unkey.com/docs/security/root-keys" rel="noopener noreferrer"&gt;doc&lt;/a&gt;.&lt;br&gt;
Now copy the above key in your .env file and make sure to keep the key as UNKEY_ROOT_KEY.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;eg. UNKEY_ROOT_KEY="unkey_************************"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we need to create a file rateLimit.ts (filename is not fixed)&lt;br&gt;
to store the ratelimit configuration.&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%2Fh3uqovuf1z0we41mlglr.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%2Fh3uqovuf1z0we41mlglr.png" alt="rateLimitConfig" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Final step
&lt;/h2&gt;

&lt;p&gt;I want to protect my routes /signup and /login from being misuse, so I will protect this two routes. Below is the code&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%2Fy0rnb7nez6v18gnyea1p.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%2Fy0rnb7nez6v18gnyea1p.png" alt="signup" width="800" height="312"&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%2Fhyjty8zmvhfhn02ae2x7.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%2Fhyjty8zmvhfhn02ae2x7.png" alt="login" width="800" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: you only need those 4-5 line to get rate-limiting in your app. Yes, it's that easy and it works like magic.&lt;/p&gt;

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


&lt;div&gt;
  &lt;iframe src="https://loom.com/embed/aa57e469d11548a781436f761f730df6"&gt;
  &lt;/iframe&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;If you’re considering launching your own application, I highly recommend looking into rate limiting as a means of protecting your resources and ensuring fair usage. Tools like &lt;a href="https://www.unkey.com/" rel="noopener noreferrer"&gt;Unkey&lt;/a&gt; make it easier than ever to implement these features, allowing you to focus on what you do best—building great software!&lt;/p&gt;

</description>
      <category>ratelimiting</category>
      <category>opensource</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Introducing Hanko: the open-source alternative to clerk</title>
      <dc:creator>Manish Kr Prasad</dc:creator>
      <pubDate>Fri, 18 Oct 2024 17:56:03 +0000</pubDate>
      <link>https://dev.to/naprila/introducing-hanko-the-open-source-alternative-to-clerk-md8</link>
      <guid>https://dev.to/naprila/introducing-hanko-the-open-source-alternative-to-clerk-md8</guid>
      <description>&lt;p&gt;In the world of authentication management, Clerk has been a popular choice for many developers. However, with the rise of open-source alternatives, Hanko has emerged as a strong contender in the market. In this blog, we will explore Hanko, its features, and how it provides easy authentication management.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Hanko?&lt;/strong&gt;&lt;br&gt;
Hanko is an open-source authentication management platform that allows developers to easily manage user authentication and authorization. It provides a simple and secure way to handle user identities, making it an ideal choice for developers who want to focus on building their applications without worrying about authentication complexities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of Using Hanko&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open-source: &lt;a href="https://hanko.io" rel="noopener noreferrer"&gt;Hanko&lt;/a&gt; is open-source, which means that developers can customize and extend the platform to fit their needs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy to use: Hanko provides a simple and intuitive API that makes it easy to manage user authentication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Secure: Hanko provides enterprise-grade security features, including encryption and access controls.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Customizable: Hanko allows developers to customize the authentication experience to fit their application's needs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Easy Authentication Management with Hanko&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will demonstrate how to create fully functional login and sign up functionality using Hanko's authentication management in a React application, including the implementation of cookie management.&lt;/p&gt;

&lt;p&gt;First, you need to signup on Hanko Cloud &lt;a href="https://www.hanko.io/" rel="noopener noreferrer"&gt;&lt;/a&gt; . Don't worry there is already a youtube video available you can follow that to set up your account, here is the link &lt;a href="https://www.youtube.com/watch?v=dSeYqEhqZc0" rel="noopener noreferrer"&gt;Hanko Cloud setup&lt;/a&gt;.After the setup, you will have your API URL we will need that soon.&lt;/p&gt;

&lt;p&gt;Now let's integrate hanko into our React App, assuming you have set up your React project.Let's follow the steps:&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Install @teamhanko/hanko-elements:
&lt;/h2&gt;

&lt;p&gt;Once you’ve initialized your React app, installing hanko-elements provides you with access to the prebuilt components: hanko-auth and hanko-profile.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm install @teamhanko/hanko-elements&lt;br&gt;
OR&lt;br&gt;
yarn add @teamhanko/hanko-elements&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  2. Add the Hanko API URL:
&lt;/h2&gt;

&lt;p&gt;Now add the API URL which you got while setting the Hanko Cloud,   in your .env file.&lt;br&gt;
&lt;code&gt;REACT_APP_HANKO_API_URL=https://f4****-4802-49ad-8e0b-3d3****ab32.hanko.io&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's create a React component Login which will handle the functionality of both login and signup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useCallback, useEffect, useMemo } from "react";
import { Hanko, register } from "@teamhanko/hanko-elements";
import { useNavigate } from "react-router-dom";
import { hankoApi } from "./constants";

function Login() {

    const hanko = useMemo(() =&amp;gt; new Hanko(hankoApi), []);
    const navigate = useNavigate();
    const defaultOptions = {
      cookieDomain: undefined,
      cookieSameSite: "none", // Specify whether/when cookies are sent with cross-site requests.
    };

    const redirectAfterLogin = useCallback(() =&amp;gt; {
        navigate("/todos");
      }, [navigate]);

    useEffect(
        () =&amp;gt;
          hanko.onSessionCreated(() =&amp;gt; {
            redirectAfterLogin();
          }),
        [hanko, redirectAfterLogin]
      );

    useEffect(() =&amp;gt; {
      const registerHanko = async () =&amp;gt; {
        try {
          await register(hankoApi, defaultOptions);
        } catch (error) {
          // handle error
          console.error("Error: ", error);
        }
      };

      registerHanko();
      }, []);

    return (
        &amp;lt;div  className=" flex justify-center"&amp;gt;
            &amp;lt;hanko-auth /&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}

export default Login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this code, your login and signup functionality is fully functional with cookies and session. Yes, that is all we need to do everything else will be handled by hanko.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verifying the token in hanko&lt;/strong&gt;&lt;br&gt;
We have written an authMiddleware which is in Nodejs(Express) which checks if the user is an authenticated user or not so that you will be able to protect your routes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NextFunction, Request, Response } from "express";
import * as jose from "jose"
import dotenv from 'dotenv'


dotenv.config()

const JWKS = jose.createRemoteJWKSet(
    new URL(`${process.env.HANKO_API_URL}/.well-known/jwks.json`)
  );

export default async function authMiddleware(req: Request, res: Response, next: NextFunction) {
  try {
    let token = req.cookies.hanko
    console.log('Cookie: ', token, typeof(token))

    // Extracting the token
    if (req.cookies &amp;amp;&amp;amp; req.cookies.hanko ) {
      token = req.cookies.hanko;
    } else if ( req.headers.authorization &amp;amp;&amp;amp; req.headers.authorization.split(" ")[0] === "Bearer" ) {
      token = req.headers.authorization.split(" ")[1];
    }

    if (token === null || (token &amp;amp;&amp;amp; token.length === 0) ) {
      res.status(401).send("Unauthorized");
      return;
    }

    // Verifying the token
    let authError = false;
    await jose.jwtVerify(token, JWKS).catch((err) =&amp;gt; {
      authError = true;
      console.log(err);
    });

    if (authError) {
      res.status(401).send("Authentication Token not valid");
      return;
    }
    next()
  } catch (error) {
    console.log("Error:", error)
  }

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
Hanko makes it easy for developers to manage user authentication and authorization.You can find more about the API by visiting &lt;a href="https://docs.hanko.io/introduction" rel="noopener noreferrer"&gt;&lt;/a&gt;.Whether you're building a new application or migrating from an existing authentication solution, Hanko is definitely worth considering.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>authmanagement</category>
      <category>opensource</category>
      <category>alternative</category>
    </item>
  </channel>
</rss>
