<?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: Sandesh Rauth </title>
    <description>The latest articles on DEV Community by Sandesh Rauth  (@sandesh56).</description>
    <link>https://dev.to/sandesh56</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F652731%2Fce84be12-ad7d-4b50-96a5-2daf24b5e623.jpeg</url>
      <title>DEV Community: Sandesh Rauth </title>
      <link>https://dev.to/sandesh56</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sandesh56"/>
    <language>en</language>
    <item>
      <title>Deployment of NextJs app in S3 using AWS CI/CD tools</title>
      <dc:creator>Sandesh Rauth </dc:creator>
      <pubDate>Mon, 20 Mar 2023 08:30:34 +0000</pubDate>
      <link>https://dev.to/sandesh56/deployment-of-nextjs-app-in-s3-using-aws-cicd-tools-14im</link>
      <guid>https://dev.to/sandesh56/deployment-of-nextjs-app-in-s3-using-aws-cicd-tools-14im</guid>
      <description>&lt;p&gt;Hey there, in this article, I am going to walk you through the process of deploying a Next.js app using AWS CI/CD tools, &lt;code&gt;CodeCommit&lt;/code&gt;and &lt;code&gt;CodePipeline&lt;/code&gt;. Before we begin, make sure you have an AWS account and are familiar with the basic concepts of AWS and Git flow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create a Next.js app
&lt;/h2&gt;

&lt;p&gt;First, let's create a new Next.js app using the command line. You can choose to create it with TypeScript by using the flag --typescript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn create next-app my-next-app
cd my-next-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 2: Create a &lt;code&gt;CodeCommit&lt;/code&gt;repository
&lt;/h2&gt;

&lt;p&gt;Now that we have our app set up, let's create a new repository in AWS &lt;code&gt;CodeCommit&lt;/code&gt;to store our code. To do this, go to the AWS &lt;code&gt;CodeCommit&lt;/code&gt;console and click on the "Create repository" button. &lt;br&gt;
For this you can follow given steps shown in images. &lt;/p&gt;

&lt;p&gt;Click on &lt;code&gt;Create repository&lt;/code&gt; button you will see the following console. Here you need to write a name of your repo and add description if you want (It is optional). After that, hit &lt;code&gt;Create button&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pzVRwVk9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2dy9nfidy1zzhiscneh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pzVRwVk9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2dy9nfidy1zzhiscneh.PNG" alt="step 1" width="880" height="602"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After creating repo, you will see the following instructions in the console. Please go through the each steps_(personal recommendation)_. Under the step 3 section, you will find the git command to clone this repo.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jCFjatXc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnjzc9vxxzyoz4wszub9.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jCFjatXc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnjzc9vxxzyoz4wszub9.PNG" alt="step 2" width="880" height="503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now for cloning this repo, you must be able to create the Git credentials. You can follow this &lt;a href="https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-gc.html?icmpid=docs_acc_console_connect_np"&gt;steps&lt;/a&gt; shown in official documentation.&lt;/p&gt;

&lt;p&gt;Add this git origin to your existing code base. If you are not familiar with this steps go through &lt;a href="https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes"&gt;this&lt;/a&gt; official documentation.&lt;/p&gt;


&lt;h2&gt;
  
  
  Step 3:Create a &lt;code&gt;CodePipeline&lt;/code&gt;for deployment
&lt;/h2&gt;

&lt;p&gt;Next, we need to create a new &lt;code&gt;CodePipeline&lt;/code&gt;in AWS &lt;code&gt;CodePipeline&lt;/code&gt;to handle the deployment of our app. Go through this steps.  &lt;/p&gt;

&lt;p&gt;In the AWS &lt;code&gt;CodePipeline&lt;/code&gt;console, click on "Create pipeline" and follow the steps as shown in the images below and hit Next. Here you can select the &lt;code&gt;New service role&lt;/code&gt; and AWS will create a new role for you. You need to edit this role later which is described in the step 5 below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w4ftYglD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zfk3sa7w3yzbzrli8h7v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w4ftYglD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zfk3sa7w3yzbzrli8h7v.png" alt="step 1" width="880" height="631"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the Next step add the source provider. The source provider( code container) for our app is AWS &lt;code&gt;CodeCommit&lt;/code&gt;so choose the one. Here, choose the repository name and the branch name(main branch usually "master" or could be any like "develop", "main" etc) respectively and leave all other configuration as default and hit Next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--16HRKT2I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/npnriza0rj6ipd9p6tob.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--16HRKT2I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/npnriza0rj6ipd9p6tob.PNG" alt="step 2" width="880" height="735"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you are done with the second step, third one is pretty important one. Here you will going to choose the build provider. You can choose any one but here I am sticking with the &lt;code&gt;AWS CodeBuild&lt;/code&gt;. Once you select this option, you will see other configuration options where you need to leave as default and only need to create a project name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fTPpw4JC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghu3pnk9a10wpvpzld5k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fTPpw4JC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghu3pnk9a10wpvpzld5k.png" alt="step 3" width="880" height="681"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;while creating the project, leave everything default. And In the environment section, for &lt;strong&gt;Operating System&lt;/strong&gt;, choose &lt;code&gt;Amazon Linux 2&lt;/code&gt; and choose &lt;code&gt;standard&lt;/code&gt; Runtime. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZXs1ztzf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u4vy8zpu1vyulgnlq1d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZXs1ztzf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u4vy8zpu1vyulgnlq1d.png" alt="step 4" width="880" height="781"&gt;&lt;/a&gt;&lt;br&gt;
In the &lt;strong&gt;Buildspec&lt;/strong&gt; section you can give any custom name for the build yml file. For now choose &lt;code&gt;buildspec.yml&lt;/code&gt; which is the default one and leave all other configuration as it is and hit &lt;strong&gt;Continue to CodePipeline&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gWcQowAG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gz4w39h9b3kp09kd4lp.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gWcQowAG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gz4w39h9b3kp09kd4lp.PNG" alt="step 5" width="880" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, you will be redirect to previous step and console will show your project. Now hit Next button.&lt;/p&gt;

&lt;p&gt;In this step you have to specify the deploy provider. Choose S3 for now and mention Bucket name and check the &lt;code&gt;Extract file before deploy&lt;/code&gt; checkbox as shown and hit Next. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fIPeNtvW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13cwvfxghgkzd2lscbfk.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fIPeNtvW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13cwvfxghgkzd2lscbfk.PNG" alt="step 6" width="880" height="652"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now review all the settings and create the pipeline. You may see automatic progress of the deployment where your build process will fail. So, don't get panic, we still have two more configuration to complete.&lt;/p&gt;


&lt;h2&gt;
  
  
  Step 4: create the IAM policy
&lt;/h2&gt;

&lt;p&gt;As our pipeline needs access to the **S3 **and **CloudFront **services. So, Go to the deployment section to see all the logs by clicking the following link as shown in image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iPaIAc49--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8j19ba7sn9wxw72i6493.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iPaIAc49--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8j19ba7sn9wxw72i6493.png" alt="step 1" width="581" height="395"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now under the &lt;strong&gt;Build details&lt;/strong&gt; tab you will see Service role (created at Step 3 while naming the pipeline). Click in the role and you will be redirect to the IAM console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wSajnJkx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6z2ir3xh0wdvc6ql0x1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wSajnJkx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6z2ir3xh0wdvc6ql0x1.png" alt="step 2" width="880" height="550"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Attach the inline policies as shown in the figure below for &lt;strong&gt;S3&lt;/strong&gt; write (PutObject) and &lt;strong&gt;CloudFront&lt;/strong&gt; write (CreateInvalidation)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1jZbydDW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oaq53lb9h06phn6jngqh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1jZbydDW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oaq53lb9h06phn6jngqh.png" alt="step 3" width="880" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After going to the Create Policy console follow this steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First choose the service &lt;strong&gt;s3&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Under Actions section, expand write box and choose &lt;code&gt;PutObject&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Mention S3 bucket and object.&lt;/li&gt;
&lt;li&gt;Again click on &lt;code&gt;Add additional permissions&lt;/code&gt; section and choose service &lt;strong&gt;CloudFront&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Under Actions, expand write box and choose &lt;code&gt;CreateInvalidation&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;create the policy. &lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  Step 5: create a &lt;code&gt;buildspec.yml&lt;/code&gt; file in the vs code
&lt;/h2&gt;

&lt;p&gt;Now you need to create &lt;code&gt;buildspec.yml&lt;/code&gt; file in the root directory_(where your project's node_modules directory lies)_ in vs code. create the file and paste the given map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: 0.1

phases:
  pre_build:
    commands:
      - yarn install

  build:
    commands:
      - yarn run build

  post_build:
    commands:
      - aws s3 cp --recursive public-read ./build s3://&amp;lt;bucketname&amp;gt;/
      - aws s3 cp public-read --cache-control="max-age=0, no-cache, no-store, must-revalidate" ./build/index.html s3://&amp;lt;bucketname&amp;gt;/
      - aws cloudfront create-invalidation --distribution-id &amp;lt;your-cloudfront-id&amp;gt; --paths /index.html

artifacts:
  files:
    - 'build/*'
    - 'build/**/*'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The YAML file described above is quite simple. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the &lt;code&gt;pre_build&lt;/code&gt; stage, the script installs all necessary NPM package dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;During the build stage, the script runs the &lt;code&gt;yarn build&lt;/code&gt; command to compile and bundle the web app into the build directory. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;code&gt;post_build&lt;/code&gt; phase, the script carries out three operations. Firstly, it copies all contents of the build directory to the designated **S3 **bucket and makes them publicly accessible. Next, it copies the &lt;code&gt;index.html&lt;/code&gt; file once more and sets particular caching HTTP headers to prevent the file from being cached by browsers. Finally, it creates an "&lt;em&gt;invalidation&lt;/em&gt;" that instructs the **CloudFront **distribution to refresh its content from the S3 source bucket.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 6: Push the code to the origin
&lt;/h2&gt;

&lt;p&gt;At this point, we have now finished all the configuration that needs to be done in AWS for &lt;code&gt;CodeBuild&lt;/code&gt; to deploy our &lt;code&gt;NextJs&lt;/code&gt;app to &lt;strong&gt;S3&lt;/strong&gt;. So, commit the changes and push the branch to the repository. You will see &lt;code&gt;progress&lt;/code&gt; tag when clicking will shows you a deployment progress and after some minutes your code will be deployed to S3. The complete process looks likes this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S4Fieave--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swykd69363ulgpkfeaa6.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S4Fieave--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swykd69363ulgpkfeaa6.PNG" alt="complete deployment process" width="880" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you enjoyed the article and learned a new skills today. If you want more from me then don't forget to follow. Also, If you have any queries or feedback then please comment. Thank you and happy deployment!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Creating Axios Interceptor in React and NextJs</title>
      <dc:creator>Sandesh Rauth </dc:creator>
      <pubDate>Sun, 19 Mar 2023 20:18:39 +0000</pubDate>
      <link>https://dev.to/sandesh56/creating-axios-interceptor-in-react-and-nextjs-395e</link>
      <guid>https://dev.to/sandesh56/creating-axios-interceptor-in-react-and-nextjs-395e</guid>
      <description>&lt;p&gt;Hello and welcome to this article on creating an Axios interceptor in React and Next.js. In this article, we'll cover how to set up and use Axios, as well as how to create an interceptor that can automatically refresh tokens.&lt;/p&gt;

&lt;p&gt;Step 1: Initializing the Project&lt;br&gt;
First, we need to initialize the project with the following command: &lt;code&gt;yarn create react-app &amp;lt;app-name&amp;gt;&lt;/code&gt; or &lt;code&gt;npx create-react-app &amp;lt;app-name&amp;gt;&lt;/code&gt;. This command will create a new React app with the name of your choice.&lt;/p&gt;

&lt;p&gt;Step 2: Creating the Axios Configuration File&lt;br&gt;
Now that we have a new React app, we can create the directory service and create a file named axiosConfig.ts inside it. Here's the code you need to put in that file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios';
import { getToken, setToken } from '../../../utils/localStorage';
import { redirect } from "react-router-dom";
import { getRefreshToken } from '../login';

const statusCode = [401,402,403]
const HttpAuthInstance = axios.create({
    baseURL: process.env.REACT_APP_BASE_URL,
    headers: {
        "Authorization": "Bearer" + " " + getToken("token").accessToken
    }
})

HttpAuthInstance.interceptors.response.use((response) =&amp;gt; {
    return response;
}, (error) =&amp;gt; {
    if (statusCode.indexOf(error?.response?.status) !== -1) {
        getRefreshToken(getToken("token")?.refreshToken).then(res=&amp;gt;setToken("token",res?.data))
    }
})

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

&lt;/div&gt;



&lt;p&gt;This code sets up an Axios instance that includes a base URL and headers with an authorization token. It also sets up an interceptor that checks for certain error status codes and refreshes the token if necessary.&lt;/p&gt;

&lt;p&gt;Step 3: Using the Axios Configuration File&lt;br&gt;
To use this Axios instance in your application, simply import it into the file where you need to make API calls. Here's an example of how to do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import HttpAuthInstance from './service/axiosConfig';

HttpAuthInstance.get('/api/users').then(response =&amp;gt; {
    console.log(response.data);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're making a GET request to the /api/users endpoint and logging the response data to the console.&lt;/p&gt;

&lt;p&gt;That's it! With this Axios interceptor, your application will automatically refresh tokens when necessary, making it more secure and efficient. I hope you found this article helpful and informative. Happy coding!&lt;/p&gt;

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