<?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: danial</title>
    <description>The latest articles on DEV Community by danial (@danial2026).</description>
    <link>https://dev.to/danial2026</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%2F762306%2Fcc9738ac-f076-48ac-9540-9452b400b108.jpeg</url>
      <title>DEV Community: danial</title>
      <link>https://dev.to/danial2026</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/danial2026"/>
    <language>en</language>
    <item>
      <title>Authentication Service with Spring Boot and React client — Part 2</title>
      <dc:creator>danial</dc:creator>
      <pubDate>Mon, 24 Apr 2023 14:13:48 +0000</pubDate>
      <link>https://dev.to/danial2026/authentication-service-with-spring-boot-and-react-client-part-2-1dnk</link>
      <guid>https://dev.to/danial2026/authentication-service-with-spring-boot-and-react-client-part-2-1dnk</guid>
      <description>&lt;h3&gt;
  
  
  part 1:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://dev.to/danial2026/authentication-service-with-spring-boot-and-react-client-22gi"&gt;Authentication Service with Spring Boot and React client&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the Part 1, we implemented an authentication service using Spring Boot on the backend and ReactJS on the frontend. And in this part, we will add a redirect page, which enables the authentication of users in other clients by utilizing the JWT token saved within the auth client.&lt;/p&gt;
&lt;h2&gt;
  
  
  Implementing the Redirect page in Auth Service
&lt;/h2&gt;

&lt;p&gt;Create a new file called &lt;code&gt;redirect.js&lt;/code&gt; in the &lt;code&gt;auth&lt;/code&gt; directory and add the following 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 React, { useEffect, useState } from "react";

function RedirectPage({ match }) {
  useEffect(() =&amp;gt; {
    // Get the query parameters
    const urlParams = new URLSearchParams(window.location.search);
    const redirectParam = urlParams.get("redirect");

    // Get user's accessToken
    const accessToken = localStorage.getItem('accessToken');

    if (accessToken &amp;amp;&amp;amp; redirectParam) {
      // Redirect to the link with token as param
      const redirectLink = `${redirectParam}?token=${accessToken}`;
      window.location.href = redirectLink;
    } else {
      // Redirect to main page if user is not logged in
      window.location.href = "/";
    }
  }, []);

  return &amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;;
}

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

&lt;/div&gt;



&lt;p&gt;By utilizing the useEffect hook, we can ensure that the redirection logic is executed as soon as the redirect page is loaded.&lt;/p&gt;

&lt;p&gt;First, get the redirect URL from the query parameters using URLSearchParams . If no URL is provided, user is redirected to the login page.&lt;/p&gt;

&lt;p&gt;Next, retrieve the user’s token from local storage. Then check if the user is logged-in and the redirect parameter is available. If both conditions are true, the user is redirected to the URL, along with the JWT token included as a query parameter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the Redirect Page in Another Service
&lt;/h2&gt;

&lt;p&gt;To use the redirect page in another service, we need a link to the redirect page in our service, and a login page that accepts the access token as a query param.&lt;/p&gt;

&lt;p&gt;For example, in our chat service at &lt;a href="https://chat.danials.space"&gt;https://chat.danials.space&lt;/a&gt;, we use this component for login:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a href="https://auth.danials.space/redirect?redirect=https://chat.danials.space"&amp;gt;Login&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And for login page, create a new file called &lt;code&gt;login.js&lt;/code&gt; and add the following 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 React, { useEffect, useState } from "react";

function LoginPage({ match }) {
  useEffect(() =&amp;gt; {
    // Get the query parameters
    const urlParams = new URLSearchParams(window.location.search);
    const redirectParam = urlParams.get("token");

    if (redirectParam) {
      // Save user's accessToken
      localStorage.setItem('accessToken', redirectParam);

      // Redirect to main page if user is not logged in
      window.location.href = '/';
    } else {
      // Redirect to main page if user is not logged in
      window.location.href = "/";
    }
  }, []);

  return &amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;;
}

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

&lt;/div&gt;



&lt;p&gt;After the user is redirected to this page with their token, the token is automatically saved in local storage for future use, ensuring a secure and seamless user experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  In conclusion
&lt;/h2&gt;

&lt;p&gt;The redirect page fetches the token from local storage and redirects authenticated users to the requested URL with the token as a query parameter, and redirects unauthenticated users to the login page, providing added security and a seamless user experience.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Authentication Service with Spring Boot and React client</title>
      <dc:creator>danial</dc:creator>
      <pubDate>Mon, 24 Apr 2023 14:12:13 +0000</pubDate>
      <link>https://dev.to/danial2026/authentication-service-with-spring-boot-and-react-client-22gi</link>
      <guid>https://dev.to/danial2026/authentication-service-with-spring-boot-and-react-client-22gi</guid>
      <description>&lt;p&gt;Hello there! Have you ever wanted to build an authentication service using Spring Boot and MongoDB? If so, you’ve come to the right place! In this post, I’m going to show you how to create a simple authentication service using Spring Boot and MongoDB, which you can use as a starting point for your own projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This service allows users to register, login and manage their profiles. We will use JWT tokens for authentication and authorization and will store them in a Redis database. Also the user’s data will be stored in a MongoDB database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before we get started, make sure you have Docker installed. You’ll also need a MongoDB and a Redis database.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PS: You can use a free MongoDB Atlas account as your primary database.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To build the service, we’ll use the following tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring Boot: a popular Java framework for building web applications.&lt;/li&gt;
&lt;li&gt;Maven: a build tool used for testing and running the backend.&lt;/li&gt;
&lt;li&gt;MongoDB: a NoSQL database for storing user data.&lt;/li&gt;
&lt;li&gt;Redis: an in-memory database for storing JWT tokens.&lt;/li&gt;
&lt;li&gt;Docker: a platform for running applications in containers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Building the Spring Boot Module
&lt;/h2&gt;

&lt;p&gt;First, we’ll build the backend service. Create a new Spring Boot project using the Spring Initializr. Add the following dependencies to your project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring Web&lt;/li&gt;
&lt;li&gt;Spring Data MongoDB&lt;/li&gt;
&lt;li&gt;Spring Security&lt;/li&gt;
&lt;li&gt;jjwt: a library for working with JWTs in Java.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next, Create a new MongoDB database and collection to store user data. In your Spring Boot application, create a new class User with the following fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;id: a unique identifier for the user.&lt;/li&gt;
&lt;li&gt;fullName: the user’s full name.&lt;/li&gt;
&lt;li&gt;username: the user’s username.&lt;/li&gt;
&lt;li&gt;password: the user’s password (stored as a hashed value).&lt;/li&gt;
&lt;li&gt;phoneNumber: the user’s phone number.&lt;/li&gt;
&lt;li&gt;birthday: the user’s date of birth.&lt;/li&gt;
&lt;li&gt;bio: a short bio about the user.
Use Spring Data MongoDB to define a repository interface for the User class, which you’ll use to interact with the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Implement the registration endpoint. Create a new REST controller class auth controller with a signup method that accepts a JSON payload containing the user’s registration data. Storing hashed password in the database.&lt;/p&gt;

&lt;p&gt;Implement the login endpoint. Create a new method signin in the auth controller class that accepts a JSON payload containing the user’s login credentials. Use Spring Security’s AuthenticationManager to authenticate the user’s credentials, and if successful, generate and return a new JWT token.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the React Client
&lt;/h2&gt;

&lt;p&gt;Now, let’s move on to the frontend. Create a new React app using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app auth-client
cd auth-client
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the src folder, create a new folder called auth . This folder will contain the logic for making API requests and page UIs.&lt;/p&gt;

&lt;p&gt;Implement the following functions inside :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;register&lt;/code&gt;: sends a POST request to the registration endpoint with the user's -registration data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;verification&lt;/code&gt;: sends a POST request to the verification endpoint with user's username and verify code.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;login&lt;/code&gt;: sends a POST request to the login endpoint with the user's login credentials.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;logout&lt;/code&gt;: clears the JWT token from local storage.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use the &lt;code&gt;useState&lt;/code&gt; hook to create state variables page inputs. Use the &lt;code&gt;onChange&lt;/code&gt; event to update these state variables.&lt;/p&gt;

&lt;p&gt;Use the &lt;code&gt;useEffect&lt;/code&gt; hook to update the isLoggedIn state variable when the component mounts. If a JWT token is stored in local storage, set &lt;code&gt;isLoggedIn&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why we save JWT in local storage ?
&lt;/h2&gt;

&lt;p&gt;Because retrieving the token from local storage instead of cookies provides an added layer of security to our application, which is crucial in ensuring the safety and confidentiality of user data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;First, to generate credentials for Spring Boot module open a terminal and navigate to the root directory of the project, then run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bash generate_passwords.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will generate unique passwords for the MongoDB and Redis databases used by the application, as well as JWT credentials.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PS: Keep in mind that if you’re using a MongoDB Atlas database, you have to update the host and port accordingly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Replace the KAVENEGAR_YOUR_API_KEY placeholder in the src/main/resources/application-dev.yml file with your Kavenegar API key.&lt;/p&gt;

&lt;p&gt;To launch dockerized databases, please run the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose &lt;span class="nt"&gt;-f&lt;/span&gt; docker-compose-dependency.yml up &lt;span class="nt"&gt;-d&lt;/span&gt; - build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To start the Spring Boot module, run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mvn clean &lt;span class="nb"&gt;install
&lt;/span&gt;mvn spring-boot:run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next for the ReactJs app, run the &lt;br&gt;
following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install
&lt;/span&gt;npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To launch Spring Boot module and the React client inside of containers, open a terminal and navigate to the root directory of each project, then run the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--build&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;To sum up, building an authentication service using Spring Boot is a great way to provide user authentication and management. With a simple React frontend, this project can be a starting point for your own authentication service. Give it a try and have fun building!&lt;/p&gt;

&lt;h2&gt;
  
  
  Front-end:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/danial2026/auth-service-react"&gt;source code&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Back-end
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/danial2026/auth-service-springboot"&gt;source code&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
