{"componentChunkName":"component---src-templates-blog-list-template-js","path":"/engineering/33","result":{"data":{"allMarkdownRemark":{"edges":[{"node":{"excerpt":"Logs are very helpful in finding the root cause of the issues you may be experiencing in an app. It is an efficient way to resolve issues by…","fields":{"slug":"/engineering/how-to-obtain-ios-application-logs-without-mac/"},"html":"<p>Logs are very helpful in finding the root cause of the issues you may be experiencing in an app. It is an efficient way to resolve issues by knowing the exact reason after checking Logs.</p>\n<p>Let's say we have developed an iOS app. As in many situations, we want to test our app on another's phone for many reasons. And probably that phone cannot be connected to the Mac. So, the console logs of the app can be sent directly from the app to the developer(us).</p>\n<p>This blog will provide step-by-step instructions for mailing iOS app logs directly from the app. </p>\n<h3 id=\"getting-started\" style=\"position:relative;\"><a href=\"#getting-started\" aria-label=\"getting started permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Getting started</h3>\n<p>Here we are having a <a href=\"https://github.com/tanvijn/TestLogs/tree/master\">project</a> with a single ViewController. We may ask our users to install the app on their device, use it and at the end click on “Press for Logs” Button. By clicking IBAction pressForLogs function will get called which will open MailComposer and then the user can mail the Log file to us.</p>\n<h3 id=\"steps-to-do-in-your-own-project\" style=\"position:relative;\"><a href=\"#steps-to-do-in-your-own-project\" aria-label=\"steps to do in your own project permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Steps to do in your own project</h3>\n<ul>\n<li>Find AppDelegate.swift in your project. Define following var and function as global before any import statement.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"java\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">public</span><span class=\"mtk1\"> </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">logFilePath</span><span class=\"mtk15\">:</span><span class=\"mtk1\">String!</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">//======================//</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">func </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk10\">_</span><span class=\"mtk1\"> </span><span class=\"mtk12\">items</span><span class=\"mtk15\">:</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Any</span><span class=\"mtk1\">..., separator</span><span class=\"mtk15\">:</span><span class=\"mtk1\"> String = </span><span class=\"mtk8\">&quot; &quot;</span><span class=\"mtk1\">, terminator</span><span class=\"mtk15\">:</span><span class=\"mtk1\"> String = </span><span class=\"mtk8\">&quot;</span><span class=\"mtk6\">\\n</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    let output = </span><span class=\"mtk12\">items</span><span class=\"mtk1\">.</span><span class=\"mtk12\">map</span><span class=\"mtk1\"> { </span><span class=\"mtk8\">&quot;*</span><span class=\"mtk6\">\\(</span><span class=\"mtk8\">$0)&quot;</span><span class=\"mtk1\">}.</span><span class=\"mtk11\">joined</span><span class=\"mtk1\">(separator</span><span class=\"mtk15\">:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot; &quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk3\">//Swift.print(output, terminator: terminator)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">NSLog</span><span class=\"mtk1\">(output)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p> Above function override any log calls in the app.</p>\n<ul>\n<li>Then define the following method in AppDelegate.Swift file. And call from <code>func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool function.</code></li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"swift\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">func</span><span class=\"mtk1\"> </span><span class=\"mtk11\">printTheDataAtLogFile</span><span class=\"mtk1\">() {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    logFilePath = </span><span class=\"mtk11\">NSTemporaryDirectory</span><span class=\"mtk1\">().</span><span class=\"mtk11\">appending</span><span class=\"mtk1\">(</span><span class=\"mtk10\">String</span><span class=\"mtk1\">.</span><span class=\"mtk4\">init</span><span class=\"mtk1\">(</span><span class=\"mtk11\">format</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;%@.log&quot;</span><span class=\"mtk1\">,Bundle.</span><span class=\"mtk12\">main</span><span class=\"mtk1\">.</span><span class=\"mtk11\">object</span><span class=\"mtk1\">(</span><span class=\"mtk11\">forInfoDictionaryKey</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;CFBundleName&quot;</span><span class=\"mtk1\">) as! </span><span class=\"mtk10\">String</span><span class=\"mtk1\">)) as </span><span class=\"mtk10\">String</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">       </span><span class=\"mtk11\">freopen</span><span class=\"mtk1\">((logFilePath as NSString).</span><span class=\"mtk11\">cString</span><span class=\"mtk1\">(</span><span class=\"mtk11\">using</span><span class=\"mtk1\">: </span><span class=\"mtk10\">String</span><span class=\"mtk1\">.</span><span class=\"mtk11\">Encoding</span><span class=\"mtk1\">(</span><span class=\"mtk11\">rawValue</span><span class=\"mtk1\">: </span><span class=\"mtk10\">String</span><span class=\"mtk1\">.</span><span class=\"mtk12\">Encoding</span><span class=\"mtk1\">.</span><span class=\"mtk12\">ascii</span><span class=\"mtk1\">.</span><span class=\"mtk12\">rawValue</span><span class=\"mtk1\">).</span><span class=\"mtk12\">rawValue</span><span class=\"mtk1\">)!, </span><span class=\"mtk8\">&quot;a+&quot;</span><span class=\"mtk1\">, stderr)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">   }</span></span></code></pre>\n<p>Above function will write all logs into a file instead of console.</p>\n<ul>\n<li>\n<p>Open Viewcontroller.swift. Define below 2 functions in it. </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"swift\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">func</span><span class=\"mtk1\"> </span><span class=\"mtk11\">allOptions</span><span class=\"mtk1\">() {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">let</span><span class=\"mtk1\"> alert = </span><span class=\"mtk11\">UIAlertController</span><span class=\"mtk1\">(</span><span class=\"mtk11\">title</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;Please Select an Option&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk11\">message</span><span class=\"mtk1\">: </span><span class=\"mtk4\">nil</span><span class=\"mtk1\">, </span><span class=\"mtk11\">preferredStyle</span><span class=\"mtk1\">: .</span><span class=\"mtk12\">actionSheet</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">alert.</span><span class=\"mtk11\">addAction</span><span class=\"mtk1\">(</span><span class=\"mtk11\">UIAlertAction</span><span class=\"mtk1\">(</span><span class=\"mtk11\">title</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;Log Mail&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk11\">style</span><span class=\"mtk1\">: .</span><span class=\"mtk12\">default</span><span class=\"mtk1\"> , </span><span class=\"mtk11\">handler</span><span class=\"mtk1\">:{ (UIAlertAction)</span><span class=\"mtk15\">in</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">self</span><span class=\"mtk1\">.</span><span class=\"mtk11\">shareDocument</span><span class=\"mtk1\">(</span><span class=\"mtk11\">documentPath</span><span class=\"mtk1\">: logFilePath)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}))</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">alert.</span><span class=\"mtk11\">addAction</span><span class=\"mtk1\">(</span><span class=\"mtk11\">UIAlertAction</span><span class=\"mtk1\">(</span><span class=\"mtk11\">title</span><span class=\"mtk1\">: </span><span class=\"mtk8\">&quot;dismis&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk11\">style</span><span class=\"mtk1\">: .</span><span class=\"mtk12\">cancel</span><span class=\"mtk1\">, </span><span class=\"mtk11\">handler</span><span class=\"mtk1\">:{ (UIAlertAction)</span><span class=\"mtk15\">in</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;User click Dismiss button&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}))</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">self</span><span class=\"mtk1\">.</span><span class=\"mtk11\">present</span><span class=\"mtk1\">(alert, </span><span class=\"mtk11\">animated</span><span class=\"mtk1\">: </span><span class=\"mtk4\">true</span><span class=\"mtk1\">, </span><span class=\"mtk11\">completion</span><span class=\"mtk1\">: {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;completion block&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">})</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">//  ============================= //</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">// this is to share file //</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">func</span><span class=\"mtk1\"> </span><span class=\"mtk11\">shareDocument</span><span class=\"mtk1\">(</span><span class=\"mtk11\">documentPath</span><span class=\"mtk1\">: </span><span class=\"mtk10\">String</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">if</span><span class=\"mtk1\"> FileManager.</span><span class=\"mtk12\">default</span><span class=\"mtk1\">.</span><span class=\"mtk11\">fileExists</span><span class=\"mtk1\">(</span><span class=\"mtk11\">atPath</span><span class=\"mtk1\">: documentPath){</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk4\">let</span><span class=\"mtk1\"> fileURL = </span><span class=\"mtk11\">URL</span><span class=\"mtk1\">(</span><span class=\"mtk11\">fileURLWithPath</span><span class=\"mtk1\">: documentPath)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk4\">let</span><span class=\"mtk1\"> activityViewController: UIActivityViewController = </span><span class=\"mtk11\">UIActivityViewController</span><span class=\"mtk1\">(</span><span class=\"mtk11\">activityItems</span><span class=\"mtk1\">: [fileURL], </span><span class=\"mtk11\">applicationActivities</span><span class=\"mtk1\">: </span><span class=\"mtk4\">nil</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  activityViewController.</span><span class=\"mtk12\">popoverPresentationController</span><span class=\"mtk1\">?.</span><span class=\"mtk12\">sourceView</span><span class=\"mtk1\">=</span><span class=\"mtk4\">self</span><span class=\"mtk1\">.</span><span class=\"mtk12\">view</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk11\">present</span><span class=\"mtk1\">(activityViewController, </span><span class=\"mtk11\">animated</span><span class=\"mtk1\">: </span><span class=\"mtk4\">true</span><span class=\"mtk1\">, </span><span class=\"mtk11\">completion</span><span class=\"mtk1\">: </span><span class=\"mtk4\">nil</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">else</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;Document was not found&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>Call <code>allOptions()</code> from IBAction pressForLogs. Above functions will open ActivityViewContrrolle to let the user mail log file to you.</p>\n</li>\n</ul>\n<h4 id=\"voila-\" style=\"position:relative;\"><a href=\"#voila-\" aria-label=\"voila  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Voila!! :)</h4>\n<p>Thanks for visiting us! We hope you find this knowledge base useful! Our mission is to make mobile app development happy. We hope we’re living up to the mission with your project.</p>\n<p>Please write to us for suggestions and for the contents we should come up with!</p>\n<p>Thanks for reading the blog. For detailed information and execution example of this blog, please refer to the video below:</p>\n<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/KTnFtIvoDiI\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk6 { color: #D7BA7D; }\n</style>","frontmatter":{"date":"July 22, 2020","updated_date":null,"description":null,"title":"How to obtain iOS application logs without Mac","tags":["Logs","ios","xcode","iPhone","troubleshoot","Mac"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/d20c2fa69dbca2ed569074ba6253a8e3/ee604/Log.png","srcSet":"/static/d20c2fa69dbca2ed569074ba6253a8e3/69585/Log.png 200w,\n/static/d20c2fa69dbca2ed569074ba6253a8e3/497c6/Log.png 400w,\n/static/d20c2fa69dbca2ed569074ba6253a8e3/ee604/Log.png 800w,\n/static/d20c2fa69dbca2ed569074ba6253a8e3/f3583/Log.png 1200w,\n/static/d20c2fa69dbca2ed569074ba6253a8e3/0dadc/Log.png 1500w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Tanvi Jain","github":"tanvijn","avatar":null}}}},{"node":{"excerpt":"What is the “hosts” file A hosts file which is used by operating systems to map a connection between an IP address and domain names before…","fields":{"slug":"/engineering/hosts-file/"},"html":"<h2 id=\"what-is-the-hosts-file\" style=\"position:relative;\"><a href=\"#what-is-the-hosts-file\" aria-label=\"what is the hosts file permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is the “hosts” file</h2>\n<p>A hosts file which is used by operating systems to map a connection between an IP address and domain names before going to domain name servers. This file is a simple text file with the mapping of IPs and domain names.</p>\n<h3 id=\"lets-talk-about-the-usage-of-the-hosts-file\" style=\"position:relative;\"><a href=\"#lets-talk-about-the-usage-of-the-hosts-file\" aria-label=\"lets talk about the usage of the hosts file permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Let’s talk about the usage of the hosts file</h3>\n<h5 id=\"protecting-privacy\" style=\"position:relative;\"><a href=\"#protecting-privacy\" aria-label=\"protecting privacy permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Protecting Privacy</h5>\n<p>You can use this to block advertisers, trackers, block marketing, or third party websites, block ads, banners, 3rd party page counters, or sites to protect your privacy. </p>\n<h5 id=\"block-website-for-your-kids\" style=\"position:relative;\"><a href=\"#block-website-for-your-kids\" aria-label=\"block website for your kids permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Block website for your kids</h5>\n<p>If you don’t want your children to open some websites you can block them by the hosts file. You can decide entirely what you wish to block and even most hijackers and possibly unwanted programs. </p>\n<h5 id=\"security\" style=\"position:relative;\"><a href=\"#security\" aria-label=\"security permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Security</h5>\n<p>We can utilize it as a firewall in our local system. The hosts file is to block Spyware and/or ad Networks you can add all the Spyware sites &#x26; ad Networks domain names in your hosts file also you can block dangerous sites, ransomware sites, blockchain sites. </p>\n<h5 id=\"development\" style=\"position:relative;\"><a href=\"#development\" aria-label=\"development permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Development</h5>\n<p>You know during the development, you need to run your web application on the localhost for verification. Websites can run on the localhost, 127.0.0.1, or localhost IP address. There are some limitations in the localhost, you want to review your website on the custom domain before launching on the public domain suppose you have developed the e-commerce. You want to debug some payment gateway issue but your payment gateway is not supporting localhost URL or IP in the case of successful payment. You can add custom domain in the hosts file and validate the payment process on localhost </p>\n<h4 id=\"how-to-edit-hosts-file\" style=\"position:relative;\"><a href=\"#how-to-edit-hosts-file\" aria-label=\"how to edit hosts file permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to edit hosts file</h4>\n<h5 id=\"windows-8-or-10\" style=\"position:relative;\"><a href=\"#windows-8-or-10\" aria-label=\"windows 8 or 10 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Windows 8 or 10</h5>\n<p>Windows operating system we need to open the host file </p>\n<ol>\n<li>Go to the Start menu and start typing the Notepad.</li>\n<li>Right-click Notepad and choose Run as administrator</li>\n</ol>\n<p>  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 386px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 179.01554404145077%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAkCAYAAACJ8xqgAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEgElEQVRIx61W609bdRg+f4JA4hYvmcwx7NgYW7Fc2tJSLygxSjJlRLcYoxvop30xccZPS/zo3PyqH/1iZowfTAiR24Sy2Xt7egFaejttKdDSK1DE8fh7f+yUbiKNWZs8+b3n/b3nOc/7vKfnHKH+2g946spN1I18h/rh2yz+Fg2f3kbd8C2ep2OCvFfHcg1X2d7H3zDcRP0nrP7KLbbuQWhW6aDTanC+XYVWZQd6DK+h+UwblJ1qqHUGdOt60anV4yTLUdyh0fFYrX8FKha3vtyJJnbc3HqOQ3i2qx9NhkE83/0WXmzXQtFtwNFTShxr68LJDh2aVD08Tzlajys1OMLiEyzfyOJnTqtwRKHkOVqFH38bx09jc7gzNos7v/yKsYkp3J2dw9SMEZN/zHJMMfBcRSznp1l81ziH2fsmzJnMEKyLIcQCPoQCiwj4/ShtbeHvnR1sbW4+ho1DY/knGG1u+PwBOEQ3jEYjJiYmMD09DbfbDa/Xx+A9BHv78/PzKBSKeLC7C2HGasf3MyYY/7yHSUY2OjqK8fFx+Hw+LCws8GICxZWgfRl0vPlQpTBjEzG/uMivZDabeUEoxGyIxcqIx+OQJAnRaJSDYsodSHjftwS/z8OKYlhdXcXy8jJWVlaQSCQQDocRiUQ4gZ/5GwgEOEgx1RKoltYt5j0ntPgTEO0OpNfXsb29za9ULBa5SlkZERPkOJPJIJfLIZvNIp/P87hUKu0RBkNWrGRiEF0ibDYrnE4nP0lWSjGRU6u0Li0tYW1tjZMQMdWtMzFlwvmwCyvpGIJLIT5Z8oTaTSaTXBG1TKCWU6kU74I6IDIC5R4htIiLcDtFiKLIzaWVCKgdKiaVsqekhpQVCgW+X9l62UNnMIq4FGF+JfhESRWZTP4RMQ2B1Hk8HjgczOt0+hGFpI5WUrhL96HLH0UiLmGB3Tomk4l7RGTBYJATysMg8wkyCYGUkfJ0eh1/bZdQ2mGEoXgYhXyhXCRfsfJYzhGI5HFkMtmKobB2iw+vfFDx/kmZf6Fyr+yhGEliI5/D+gEn/NfJlasc0/RJpeCMpRhhlhMeprCaeiIklUIklmBm56q2XI2QT5laDkgJNpTaEPLHlyeaKHv4pIR8KC5prSYe7t828RiKuXztCKVkGPlcviYeckKPFOU3ds0UitJqjYcST9V2KB4pWVuFXinAPCxU/S9Xwz5hJFYeymGPqGrYfwUEY8imU1hd23s3VD72/w9IyMbGBvMwkcaD0hZy7CrLiThbM3iSn/DzpBFWu519Qd3D6O8T/KvKxr5zLE4X7CJ7j7CXlpPB5vJgzuxgex5YbDaYzBY43V5YrA5W64Gd7dudXghN6leh7HsHjR16aAeGcMbQj5e0r6P77UG8cL4TTxsuo0F3GWc1Xbj+1RBa1BqcGrwG1fANHD3XgeEPL0Kj0+K5vl40jvTvEbbo38Qx9iXb/sYAFD19aGaEyr4BnOjS4/T716EY+gKtegMujnwAhVoHxaXP0fbZ1ziu68d7N75E74ULaHn3Es5e/Qj/ANDnuTDoxgo/AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Start Menu\"\n        title=\"Start Menu\"\n        src=\"/static/c1e0fe215fc1a5012e2137091af255db/7bc0b/windows1.png\"\n        srcset=\"/static/c1e0fe215fc1a5012e2137091af255db/7bc0b/windows1.png 386w\"\n        sizes=\"(max-width: 386px) 100vw, 386px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<ol start=\"3\">\n<li>\n<p>Open the hosts file. Click on File > Open and Copy and Paste the following path`</p>\n<p><code>c:\\Windows\\System32\\Drivers\\etc\\hosts</code></p>\n</li>\n</ol>\n<p>  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 63.230769230769226%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAACLElEQVQ4y32T6Y7aMBSFef9H6K9K/dHXqFRV3abSwMBAgAEStuyLnTh2Fk7vNVBmUFukIyfY+XzO9fUgznIUhUBVVajr2kpVJWqtz6pvo6ZRqRpSlhBCwhhD7wp1KWleQukKg7Zp7ERZltj7AQTBjWGAsouvYti9LIwM8Pf8HkURBrj8tHVRo+16GJ1DigSFEOTkrP50Qt/3aNsW+gIoL6k6+qY2DfwgfAtMsgJRSrBSIYhSiJJ2bxjQXKIzyNjnsiLnNFY0FhTfiwTm6+0N2FLMfa6xO0boGoOO3JTCJ6e5nT+RQ5ZdSy6zvEBO6rrO1lVTiXbbV8CGgCE5WvoxFNWz7QCVO4jD3V+BSZLYmnFkKSWyLKPIwVuHodQYLrcIj0cIqVCmDtL4+E9gHMW2VAwsigK+79+ARtcQigobZRSzQEr1lImD5D9AdngF5nl+BySHQmkCJjhR/YSsIGIHWRr8AfY9Q6k81GppmiKOzw655bgT7oAGq42L7z9+YjGfY38IoMUSRXqAodPEqbsuJWhvI2Z0KRpyy5dC0km/AfJO3Hcche3zTTjuX/A0m2O22mG1C+DRgbnUBdxvnudis9lQgvQCz3A4HDDgZuU4vAuD2D4rDn2M1z4+/orw4ese7794ePdpTc9brI8ppos1vj0MMZo4WCwWtIGHLbcN14FdMd11Xfvna3kkx5ljMpng8XGImeNg9bLEcLrE54cxRrMVxuMxnp+nGD2N8Bu70OBAxMChEgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"edit-hosts-file-windows\"\n        title=\"edit-hosts-file-windows\"\n        src=\"/static/a6b91910fadbb359142add8e605b588b/e5715/windows2.png\"\n        srcset=\"/static/a6b91910fadbb359142add8e605b588b/a6d36/windows2.png 650w,\n/static/a6b91910fadbb359142add8e605b588b/e5715/windows2.png 768w,\n/static/a6b91910fadbb359142add8e605b588b/ddc81/windows2.png 837w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>You can edit the hosts file</p>\n<p>Suppose you want to block facebook.com on your system and want to add a custom domain for your website. Just Copy and Paste following Lines</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">0.0.0.0         www.facebook.com</span>\n<span class=\"grvsc-line\">127.0.0.1       www.customdomain.com</span></code></pre>\n<p>  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 735px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 65.53846153846153%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAABy0lEQVQ4y51Sy47TQBDMb+0diRsSJ8R38C2IE8sJ/gBpEUis+AvY+DF+e8aO37GdzabobsdRsuvlwEil7ukZ1VT19MpRHjxXQSmFsixhtEbXdRjHUbDb7fDcOhwOT7BKkxgmy1BVFYwxiOMYmuJmsxEURYGBiIdhEPAj2+0W+/1+mdAYjbbt0DQ1siyn2JDSStRyzg9xnYk557MsM0K6tFau68LzfLiOA+VN1n3fx3p9R1jT3iV4CMIALuVry6L7Ht0JRO1j6yvHselyiCiOxHpG0NogSRKxH0URcqpFxzxNUyQEjtzrmfCkMCSyguzNPWrPPoT3fd+fItvkOud8bya6IGT5SZKKIuVN1liZqCOw/SAIkOc5Kdeyn/vLH/OEkC3Wx+b/zzonE8LJSicvx6SSX+VxmVXNKuq6lr6xcq6fk10orKqaeljJ7BkeD8qLopQxyTcFmrZFVTdik4la2s/D/lidEA5tibGrkaeRRNwP0JGPUNnouwZ4GHHY9afPWVJ1QXj13sKLjz7efA7w8jrA1QeFV9cOXn+y8fZLiHdfNVRxv9i3RYXf/rT4Tri1Ovy4a3Hzu8FPym/tLX7ZU83KpgF+OA7vvxT+BWRo33w9ocBUAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Save-hosts-file-windows\"\n        title=\"Save-hosts-file-windows\"\n        src=\"/static/fac9daf370e72e60b1fd57e8ccd07c43/7608e/windows3.png\"\n        srcset=\"/static/fac9daf370e72e60b1fd57e8ccd07c43/a6d36/windows3.png 650w,\n/static/fac9daf370e72e60b1fd57e8ccd07c43/7608e/windows3.png 735w\"\n        sizes=\"(max-width: 735px) 100vw, 735px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>After finishing the Editing, <strong>Save your hosts file</strong></p>\n<p>  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 688px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 67.07692307692308%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAACIklEQVQ4y21TTY/TMBDt7+HCiTsSB9hfh5A4INCKf7DaKwdAAokDC0IrdttuEydNNkntJM1H4yRNvx4z7rZqt1ga2ePYz++9mfR+2hJSxojCCEmSoqoqCo31eo3FYmFitVrhcGw2m/38OHpxmsFzHaSxxKzMkSoJx7Kg5ARZliHPc8xms5MHlsul2TsB9FWOX39HuBUBvLiEHSS4otzyIlQExIyLoqB1ZYA5mqbBdDpFXddHjHn0vo5KXP4O8NnS+CIafOrnuLgK8O02wr3vGRtUTJZEEdI0RUzriZRQSpl9Znwov1fmGcbCRkCX9awg6QoJyQ2DAMPhEIP+AJZlw/fGEEKYvf5gAGFbsClXKj4GZI8c4VBhlJGjtTavx3GCtm3QdR3m87mZWSoH+8d5XW+/HwGGYQjXcY0U3/fheR4CYieINQPz94BCkcwkSbZ7D/I5+IEjQPaGWfLhsiSGtTaH2HAzUzBrjradG3ZcqEPvjgD5FZbEDPnSrud2LbK7yGP1IHUn+RBsX2Wta3AuSdJkIpFOMyyWK1NVlpoXJfXb9gJ7zBXms5Kk/xew0dRXVYmqyFBk5AnlXO1Wl6hp5mjIhiIvUFI/ZllOirq9xBPAgR/DTRtc2yGuxQQi1riLCnz/M8SPGwcjWUGWLcndyu+6xZ7xIege8NX5GE9eu3h57uHs4z2evnHx7K2LF+8snH0QeP7ex8VNZQ6vN6f/8+P1Py8x2HduXznYAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Save-hosts-file-windows\"\n        title=\"Save-hosts-file-windows\"\n        src=\"/static/9ae12f3a031c3cf9bf210efb2cc18f9c/ebf47/windows4.png\"\n        srcset=\"/static/9ae12f3a031c3cf9bf210efb2cc18f9c/a6d36/windows4.png 650w,\n/static/9ae12f3a031c3cf9bf210efb2cc18f9c/ebf47/windows4.png 688w\"\n        sizes=\"(max-width: 688px) 100vw, 688px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>Open your browser and try to access www.facebook.com and see you can’t access this site</p>\n<p>  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 62.153846153846146%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAAA9ElEQVQoz61TW26DMBDk/P3odSrlKOkHEkKJSYsQtmPj59TriAB5gaJaGu2yDMNod13EGEEIIcA5l+FTHt5EgX8+xejwEejQX5VS1+dXfMJTh6OAtRZlWcIY84w5CZLDNUGKw2Dgvc9u55Fw+81LwXhJwI4MdV2DMYbj4YCmaVBVVa6TeAxTizY57HuO00+L37ZLuYCUCpxLCCFzfwla6209pMjPFm1v0AmLjlv00qVagFBpVTJvw1CQCZeIYBCdSr3UUGcFMwxXzt3arDkkB9+Nx56lYcTb92+sjUsqnzuBjy8BbeciWOzr6lDmpMe15QUYzx8wY7OS8tH6GwAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Facebook-windows\"\n        title=\"Facebook-windows\"\n        src=\"/static/ca2e87ac50ce1b744a0f7f07fc98f479/e5715/windows5.png\"\n        srcset=\"/static/ca2e87ac50ce1b744a0f7f07fc98f479/a6d36/windows5.png 650w,\n/static/ca2e87ac50ce1b744a0f7f07fc98f479/e5715/windows5.png 768w,\n/static/ca2e87ac50ce1b744a0f7f07fc98f479/9f82e/windows5.png 820w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<h5 id=\"linux\" style=\"position:relative;\"><a href=\"#linux\" aria-label=\"linux permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Linux</h5>\n<p>Use following instructions for Linux </p>\n<ol>\n<li>\n<p>In the Linux terminal window, open hosts file using a favorite text editor </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">$ sudo vim /etc/hosts</span></code></pre>\n</li>\n</ol>\n<p>  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 46.61538461538461%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAAASklEQVQoz+2MsQ3AMAgEDRQmOCamY/9FP2SAFJFS+qVr7qVrawXcHWYGGwOqB3rXchfOOTHKPX9EIDPBzBCRV1oNv0JEO7iDH7kBP6m1huAPS3YAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"linux-hosts-file\"\n        title=\"linux-hosts-file\"\n        src=\"/static/e9b2f6ff6089fd0a961fee8495beec03/e5715/linux1.png\"\n        srcset=\"/static/e9b2f6ff6089fd0a961fee8495beec03/a6d36/linux1.png 650w,\n/static/e9b2f6ff6089fd0a961fee8495beec03/e5715/linux1.png 768w,\n/static/e9b2f6ff6089fd0a961fee8495beec03/0f882/linux1.png 1584w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>  It will prompt for the password, enter your administrator password.</p>\n<ol start=\"2\">\n<li>Using the vim or your favorite text editor you can easily edit. The Linux hosts file is similar to the windows hosts file. Now again I am blocking Facebook and adding a custom domain </li>\n</ol>\n<p>  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 41.07692307692307%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAvklEQVQoz62Q1w7CMAxFOzI66QykM4Xy/794cYLECy2CioejSLZ14msvbxpERYG0rpG3LeKyRBhF4HnuYEni4GkKcTqBZ5mr+2EIz/PeORuDdhyhb1dclgVqnlFpDUX1VCkwK7CQTFYVBH1o2RXe1xU1DYx9DzNNmEkuhdge/obVCinuMAzoSTrThozzZ9P3fxd2XYeG7qgp5kjbWaGhuIrihnuxPiFEjCDwwRh7IaV076HIjMnj99oWRn8VPgABiqp9ZOXzsQAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"linux-hosts-file-edit\"\n        title=\"linux-hosts-file-edit\"\n        src=\"/static/96d45a3a2ccedff08dcd2886df06433f/e5715/linux2.png\"\n        srcset=\"/static/96d45a3a2ccedff08dcd2886df06433f/a6d36/linux2.png 650w,\n/static/96d45a3a2ccedff08dcd2886df06433f/e5715/linux2.png 768w,\n/static/96d45a3a2ccedff08dcd2886df06433f/e0577/linux2.png 1796w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<ol start=\"3\">\n<li>Save the Changes</li>\n</ol>\n<h5 id=\"mac-os\" style=\"position:relative;\"><a href=\"#mac-os\" aria-label=\"mac os permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Mac OS</h5>\n<p>Use following instructions for macOS </p>\n<ol>\n<li>Find the terminal application on your system\n-- We can use Spotlight application to search </li>\n</ol>\n<p>  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 63.69230769230769%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAACvElEQVQ4y0WSW2/jVBRG/RNGnZkoTdM4dZrE9vHdcZ0096SepknUkE4pDDeBhJAYUDWPvMEf4JVHbhLwMxfbZ0J5+HRs+Zzltfc+RjVaYyYbVHaDly4x7YxaO6DScKicdKg1PZxoiGl51Bo2z57XeVltclzvcPTCpFI9o266nEpOTrsYVlLQcgfUTE/nxXGLo6pFpdbmqNKQbwlZf47VDjl3epw0utROOxr67KjG85cmx6c29aaiWrMwgnSKn68ILtfY4Zh6o0Pz3OPcjmRVZIMpo/EVjkpodRSu30OFOR35UaPZwTyzOe+GdFWPdtfHmEyXPP70Cz/8/A9fv/sR324TKlsn9l2CIMIPU4Kop1flR3h+jB8kOuWz8mRPEMueRID37xjePZJt3xLefEu0fku8+Y5k+z3J7SNRfylQORz28IJUcoBo0GH1EzFPccTe8L/6lfibv3G+/APzk984++x3HevzP7G++As13OF7gYapg1lpWRpp4/I9yFBRHxVfYry+f2CxKLi5XjEbj4g8l7iMr+hFAWGYHCxiHDcgzXJmszndro2rPMKkjys99dIRKp1gjNYPzK73FJt7knxKmo+5GEzoD2cE8QXuoSzXC3VsR+F5Hu12G6vVIiiB0UCAYwGOpYebjxhf3bJY7VmudoymBVk+ZHA5IYqzJztXGl+m3bFpNptYVosziYpyAV6iEjFMhhirD94wF1ixvmO1vWNZrDWg6/h69Q59csuSVaihunwxL4fgBLlc/AGO9M8RsPFq9zGjxZbF9Y751ZpitaUvduVhPVF9VQ5AgTkqYvFqw2b3Ib3BjI4n0HAgd/h9jKvtA5PiluXNnnmx1ZsH0r84zZ9s/itXA7WZXBGZrO1LxNAO+gLrvweu9p9SCHS9f8P19l5Dy6Ekvb7YBE/D+D+laXIo90Ig+ROszL9NIaGstKV+ZwAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"linux-hosts-file-edit\"\n        title=\"linux-hosts-file-edit\"\n        src=\"/static/725f87402b3ff7b7beed0ce746571631/e5715/mac1.png\"\n        srcset=\"/static/725f87402b3ff7b7beed0ce746571631/a6d36/mac1.png 650w,\n/static/725f87402b3ff7b7beed0ce746571631/e5715/mac1.png 768w,\n/static/725f87402b3ff7b7beed0ce746571631/9685e/mac1.png 1336w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<ol start=\"2\">\n<li>Type <code>sudo vim /etc/hosts</code> in the terminal\n-- It will prompt for the password, enter your administrator password\n-- Enter administrator Password\n</li>\n</ol>\n<p>  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 52.15384615384615%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA/0lEQVQoz62R22rCQBCGx2jxcOMDWCgUqoKHaINJE2OiInihF56akKbe+/4v8HezniZhpYJefAzM7H77Z0L7wwGjcAtv94PxdyxqBF/g7WLJJIgxXIfwxcwPfjEO93C3EexVAEdgZ6C3+QYlw0fVnqFojFDouShPI5jLLhpeHVrXQa5tgVrmkXof9NE/VhXv8xVedAcVIdM6FvKdL+R1F6WeiaI+kLKkryV9gfYPVJssQI1P+fo1yUBgSZJeKuHpXO5cM9DrdAlqGjJFemhKuPRyKSti8ouQD2UKnohVJUxKtWcKk3Nyhych3w8XkOLiLXH6k9mPuSuhYpfKHT7CH5qFIp4Bf2LYAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"mac-password\"\n        title=\"mac-password\"\n        src=\"/static/02f7704766464a68ca840c7ea423d695/e5715/mac2.png\"\n        srcset=\"/static/02f7704766464a68ca840c7ea423d695/a6d36/mac2.png 650w,\n/static/02f7704766464a68ca840c7ea423d695/e5715/mac2.png 768w,\n/static/02f7704766464a68ca840c7ea423d695/e405b/mac2.png 1566w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p><em>Using the vim text editor you can easily edit you. The macOS hosts file is also similar to the windows and Linux hosts file. I am blocking Facebook and adding custom domain here as well</em>.</p>\n<p>  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 34.46153846153846%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAAA+klEQVQoz42Q207CUBBFj9LWFnoJQnzAL1BQ6f1+oQUTTfj/z9nOjEBiX+zDykw7c1Z2RplBBcMvsIgb6Psc6jWC2sbT2I16xvBLWGEFMyihfeQinb1nvz2hXdD3hVSeXXfUNiHJCCfu4CQd5mENNz1gETWCR70dtzLj71VxxDLr5R/vOVRZeLdL/6B4aJGM0xiUgh+7JDH9Shbu3wip2YX0VkUwTmiTwApqSWjRPTnZMh9EzCL1cr1pMo2n9htOfoQRtrCzAZvhjGdi05+xbr7gFSdY8QEPUTcJ5VEaO+2h+zXMqIVHcpfEq+oTj+VJ+jndWaMT8M5//ADu/8pmlGD1WgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"mac-hosts-file-edit\"\n        title=\"mac-hosts-file-edit\"\n        src=\"/static/613a525b29d21509c12da8d4c68919b6/e5715/mac3.png\"\n        srcset=\"/static/613a525b29d21509c12da8d4c68919b6/a6d36/mac3.png 650w,\n/static/613a525b29d21509c12da8d4c68919b6/e5715/mac3.png 768w,\n/static/613a525b29d21509c12da8d4c68919b6/077b7/mac3.png 1166w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<ol start=\"3\">\n<li>Save Changes\nOpen your browser and try to access www.facebook.com and see you can’t access this site.</li>\n</ol>\n<p>  <span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 62.153846153846146%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAAA9ElEQVQoz61TW26DMBDk/P3odSrlKOkHEkKJSYsQtmPj59TriAB5gaJaGu2yDMNod13EGEEIIcA5l+FTHt5EgX8+xejwEejQX5VS1+dXfMJTh6OAtRZlWcIY84w5CZLDNUGKw2Dgvc9u55Fw+81LwXhJwI4MdV2DMYbj4YCmaVBVVa6TeAxTizY57HuO00+L37ZLuYCUCpxLCCFzfwla6209pMjPFm1v0AmLjlv00qVagFBpVTJvw1CQCZeIYBCdSr3UUGcFMwxXzt3arDkkB9+Nx56lYcTb92+sjUsqnzuBjy8BbeciWOzr6lDmpMe15QUYzx8wY7OS8tH6GwAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"edit-hosts-file-mac\"\n        title=\"edit-hosts-file-mac\"\n        src=\"/static/ca2e87ac50ce1b744a0f7f07fc98f479/e5715/mac4.png\"\n        srcset=\"/static/ca2e87ac50ce1b744a0f7f07fc98f479/a6d36/mac4.png 650w,\n/static/ca2e87ac50ce1b744a0f7f07fc98f479/e5715/mac4.png 768w,\n/static/ca2e87ac50ce1b744a0f7f07fc98f479/9f82e/mac4.png 820w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<h3 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h3>\n<p>The hosts file is found on all operating systems. The hosts file is a powerful tool. It can make your computer more secure and safer by blocking malicious sites    </p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","frontmatter":{"date":"July 20, 2020","updated_date":null,"description":"Hosts file maps hostnames to IP addresses, sometimes required to be edited either to block some sites or to test some custom domain.This blog explains how can we edit the hosts in different operating systems.","title":"Benefits and usages of Hosts File","tags":["Computer tricks","Networking","Hosts File"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.4184397163120568,"src":"/static/2b2c60ac5ca62d5201374a722f6a3f2f/f422e/index.jpg","srcSet":"/static/2b2c60ac5ca62d5201374a722f6a3f2f/f836f/index.jpg 200w,\n/static/2b2c60ac5ca62d5201374a722f6a3f2f/2244e/index.jpg 400w,\n/static/2b2c60ac5ca62d5201374a722f6a3f2f/f422e/index.jpg 640w","sizes":"(max-width: 640px) 100vw, 640px"}}},"author":{"id":"Vijay Singh Shekhawat","github":"code-vj","avatar":null}}}},{"node":{"excerpt":"Biggest Challenge in React application is the management of state for frontend developers. In large applications, React alone is not…","fields":{"slug":"/engineering/react-state-management/"},"html":"<p>Biggest Challenge in React application is the management of state for frontend developers. In large applications, React alone is not sufficient to handle the complexity which is why some developers use React hooks and others use state management libraries such as Redux.</p>\n<p>In this post, We are going to take a closer look at both React hooks and Redux to manage the state.</p>\n<h2 id=\"what-is-react-state-management\" style=\"position:relative;\"><a href=\"#what-is-react-state-management\" aria-label=\"what is react state management permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is React State Management?</h2>\n<p>React components have a built-in state object. The state is encapsulated data where you store assets that are persistent between component renderings.</p>\n<p>The state is just a fancy term for a JavaScript data structure. If a user changes state by interacting with your application, the UI may look completely different afterwards, because it's represented by this new state rather than the old state.</p>\n<blockquote>\n<p><strong>Make a state variable responsible for one concern to use efficiently</strong>.</p>\n</blockquote>\n<h2 id=\"why-do-you-need-react-state-management\" style=\"position:relative;\"><a href=\"#why-do-you-need-react-state-management\" aria-label=\"why do you need react state management permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why do you need React state management?</h2>\n<p>React applications are built using components and they manage their state internally and it works well for applications with few components, but when the application grows bigger, the complexity of managing states shared across components becomes difficult.</p>\n<p>Here is a simple example of an e-commerce application, in which the status of multiple components will change when purchasing a product.</p>\n<ul>\n<li>Add that product to the shopping list</li>\n<li>Add product to customer history</li>\n<li>trigger count of purchased products</li>\n</ul>\n<p>If developers do not have scalability in mind then it is really hard to find out what is happening when something goes wrong. This is why you need state management in your application.</p>\n<p>Let’s discuss how to use react state management using react hooks and redux</p>\n<h2 id=\"what-is-redux\" style=\"position:relative;\"><a href=\"#what-is-redux\" aria-label=\"what is redux permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is Redux?</h2>\n<p>Redux was created to resolve this particular issue. it provides a central store that holds all states of your application. Each component can access the stored state without sending it from one component to another.  Here is a simple view of how Redux works.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 56.30769230769231%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAAC/UlEQVQoz01SW0hUURRdd8ZCpaIc0j6KXtiLIEJNpIf2/DCFSjMpLcKfMKUys4dIjxEjk8aZ64hOls507+iMzzF7D0ikaYpkkVhqcx+jToJ+9dFHyGnfIaIL655zWGuvs8/eG8nJyfgfBw/tBWMMs4MM9spvvGCRf7Y+UlSPXZ0Sq+QfgkWSqu/2rbeWvQfzMUStBNKOZSLlcGowHrbyj9xbF+MKcitC0o5mLk8/dspw8dy9MCxGWL1pNI8MPS11itgl+hudVbKLDB33bz5ftScpPvLokYzNx9OyloI+s7FbR5wOjdZJNJi+YsjDEBqF8OitiwzEL7QY36Lm3gAEs4oXTX70eQNoqlZAQSjKt2geOkIo8zNUlw0SNxXk6CcvE3hftGgOrOZvD23gS3s3u6oDawgLyopbOMGi6p83+vW9rwN6MtRTkP7XCENL7azBUTkWbbzqiq0q/bCJDNcStxYiL5c/MUuMnjXvcSjMaZUZEUzkpR3ajdoz/q5BkJ4L7nnJTckwV40877ZJTPMgjpFA2k2HkmabWtBYO3LLaZWKSFhIWa/4a8L9b6idn5i/0+pLFXmlWODHr5HpFdGiXCaukGroR8ODMUz0MK0uCwl6a+kwTDdG8dA4h08vGc5k5eHAwSSkHtmPppoJVGxneFDSg1HiEIIIign3itpUjAGm2290NCZcRnq2ITMje11aSm5kYkw+da5LV3KuXxcfl4iTJ3KQtC8Bl84b4W7o4wCGndtyl6TszzPknC5cdiI9O+r6hdoFzxp+69BkneIIaLXNLBcsypfWOr/cYZf9bY8VH43JHN1q1bImcHUVw9upPL5mmyK31fumqe6kUecovshuGoOjclyv1SIIZ5UvgsRuEnd1iZMdHofaToZee+V4YVzCxqhdiTGrrXd7twhmuZ00nU+FyY72erWNmtNN8WephtQPRfev2O5aBQ6ThGdOPwa6Z/DuxTRcNSoeln9GbMJ6bNoWAdOdV0ENDTn6vTPo7pzWuh6Mp2Q4ajD+ABjguSUc0CB4AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"using react state management with redux flowchart\"\n        title=\"using react state management with redux flowchart\"\n        src=\"/static/878d2cde053633bfea88a8bfcfc28e89/e5715/image1.png\"\n        srcset=\"/static/878d2cde053633bfea88a8bfcfc28e89/a6d36/image1.png 650w,\n/static/878d2cde053633bfea88a8bfcfc28e89/e5715/image1.png 768w,\n/static/878d2cde053633bfea88a8bfcfc28e89/29007/image1.png 1600w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>There are three building parts: actions, store, and reducers. Let’s briefly discuss what each of them does.</p>\n<h4 id=\"actions-in-redux\" style=\"position:relative;\"><a href=\"#actions-in-redux\" aria-label=\"actions in redux permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Actions in Redux</h4>\n<p>Actions are payloads of information that send data from your application to your store. Actions are sent using  <a href=\"https://redux.js.org/api/store#dispatchaction\"><code>store.dispatch()</code></a>. Actions are created via an action creator.\nHere is an example action that represents adding a new todo item:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">{ </span>\n<span class=\"grvsc-line\">type: &quot;ADD_TODO&quot;, </span>\n<span class=\"grvsc-line\">payload: {text:&quot;Hello Foo&quot;}</span>\n<span class=\"grvsc-line\"> }</span></code></pre>\n<p>Here is an example of its action creator:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">ocnst addTodo = (text) =&gt; {</span>\n<span class=\"grvsc-line\">  return {</span>\n<span class=\"grvsc-line\">     type: &quot;ADD_TODO&quot;,</span>\n<span class=\"grvsc-line\">     text</span>\n<span class=\"grvsc-line\">  };</span>\n<span class=\"grvsc-line\">}</span></code></pre>\n<h4 id=\"reducers-in-redux\" style=\"position:relative;\"><a href=\"#reducers-in-redux\" aria-label=\"reducers in redux permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reducers in Redux</h4>\n<p>Reducers specify how the application's state changes in response to actions sent to the store.\nAn example of how Reducer works in Redux is as follows:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">  const TODOReducer= (state = {}, action) =&gt; {</span>\n<span class=\"grvsc-line\">  switch (action.type) {</span>\n<span class=\"grvsc-line\">    case &quot;ADD_TODO&quot;:</span>\n<span class=\"grvsc-line\">      return {</span>\n<span class=\"grvsc-line\">        ...state,</span>\n<span class=\"grvsc-line\">        ...action.payload</span>\n<span class=\"grvsc-line\">      };</span>\n<span class=\"grvsc-line\">    default:</span>\n<span class=\"grvsc-line\">      return state;</span>\n<span class=\"grvsc-line\">  }</span>\n<span class=\"grvsc-line\">};</span></code></pre>\n<h4 id=\"store-in-redux\" style=\"position:relative;\"><a href=\"#store-in-redux\" aria-label=\"store in redux permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Store in Redux</h4>\n<p>The store holds the application state. You can access stored state, update the state, and register or unregister listeners via helper methods.</p>\n<p>Let’s create a store for our TODO app:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">const store = createStore(TODOReducer);</span></code></pre>\n<p>In other words, Redux gives you code organization and debugging superpowers. This makes it easier to build more maintainable code, and much easier to track down the root cause when something goes wrong.</p>\n<h2 id=\"what-is-react-hook\" style=\"position:relative;\"><a href=\"#what-is-react-hook\" aria-label=\"what is react hook permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is React Hook?</h2>\n<p>These are functions that hook you into React state and features from function components. Hooks don't work inside classes  and it allows you to use React features without writing a class. </p>\n<p>Hooks are backwards-compatible, which means it doesn't keep any breaking changes. <a href=\"/react-hooks-guide/\">React provides some built-in Hooks</a> like <code>useState</code>, <code>UseEffect</code> and <code>useReducer</code> etc. You can also make custom hooks.</p>\n<h3 id=\"react-hook-rules\" style=\"position:relative;\"><a href=\"#react-hook-rules\" aria-label=\"react hook rules permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>React Hook Rules</h3>\n<ul>\n<li>Call hook at the top level only means that you need to call inside a loop, nested function, or conditions.</li>\n<li>React function components are called hooks only.</li>\n</ul>\n<p>Please see the following examples of some react hooks as follows:</p>\n<h5 id=\"what-is-usestate-and-how-to--use-it\" style=\"position:relative;\"><a href=\"#what-is-usestate-and-how-to--use-it\" aria-label=\"what is usestate and how to  use it permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is useState and how to  use it</h5>\n<p><code>useState</code> is a Hook that Lets you add React state to function components.\nExample:\nDeclaring a State Variable in class and initialize count state with 0 by setting this.state  to {count:0}.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">class Example extends React.Component {</span>\n<span class=\"grvsc-line\">  constructor(props) {</span>\n<span class=\"grvsc-line\">    super(props);</span>\n<span class=\"grvsc-line\">    this.state = {</span>\n<span class=\"grvsc-line\">      count: 0</span>\n<span class=\"grvsc-line\">    };</span>\n<span class=\"grvsc-line\">  }</span>\n<span class=\"grvsc-line\">  </span></code></pre>\n<p>In a function component, we have no this, so we can’t assign or read this.state. Instead, we call the <code>useState</code> Hook directly inside our component:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">function Example() {</span>\n<span class=\"grvsc-line\">    const [count, setCount] = useState(0);</span>\n<span class=\"grvsc-line\">}</span></code></pre>\n<p>We declare a state variable called count and set it to 0. React will remember its current value between re-renders, and provide the most recent one to our function. If we want to update the current count, we can call setCount.</p>\n<h5 id=\"what-is-usereducer-and-how-to-use-it\" style=\"position:relative;\"><a href=\"#what-is-usereducer-and-how-to-use-it\" aria-label=\"what is usereducer and how to use it permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>what is useReducer and how to use it</h5>\n<p><code>useReducer</code> is a hook I use sometimes to manage the state of the application. It is very similar to the <em>useState</em> hook, just more complex. <em>useReducer</em> hook uses the same concept as the reducers in Redux. It is basically a pure function, with no side-effects.</p>\n<p>Example of useReducer:</p>\n<p>useReducer creates an independent component co-located state container within your component. Whereas Redux creates a global state container that hangs somewhere above your entire application.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">          +----------------+              +----------------+</span>\n<span class=\"grvsc-line\">          |  Component A   |              |                |</span>\n<span class=\"grvsc-line\">          |                |              |                |</span>\n<span class=\"grvsc-line\">          |                |              |      Redux     |</span>\n<span class=\"grvsc-line\">          +----------------+              |                |</span>\n<span class=\"grvsc-line\">          | connect Redux  |&lt;-------------|                |</span>\n<span class=\"grvsc-line\">          +--------+-------+              +--------+-------+</span>\n<span class=\"grvsc-line\">                   |                               |</span>\n<span class=\"grvsc-line\">         +---------+-----------+                   |</span>\n<span class=\"grvsc-line\">         |                     |                   |</span>\n<span class=\"grvsc-line\">         |                     |                   |</span>\n<span class=\"grvsc-line\">+--------+-------+    +--------+-------+           |</span>\n<span class=\"grvsc-line\">|  Component B   |    |  Component C   |           |</span>\n<span class=\"grvsc-line\">|                |    |                |           |</span>\n<span class=\"grvsc-line\">|                |    |                |           |</span>\n<span class=\"grvsc-line\">+----------------+    +----------------+           |</span>\n<span class=\"grvsc-line\">|    useReducer  |    | connect Redux  |&lt;----------+</span>\n<span class=\"grvsc-line\">+----------------+    +--------+-------+</span>\n<span class=\"grvsc-line\">                               |</span>\n<span class=\"grvsc-line\">                      +--------+-------+</span>\n<span class=\"grvsc-line\">                      |  Component D   |</span>\n<span class=\"grvsc-line\">                      |                |</span>\n<span class=\"grvsc-line\">                      |                |</span>\n<span class=\"grvsc-line\">                      +----------------+</span></code></pre>\n<p>Below an example of todo items is completed or not using the useReducer react hook.</p>\n<p>See the following function which  is a reducer function for managing state transitions for a list of items:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"> const todoReducer = (state, action) =&gt; {</span>\n<span class=\"grvsc-line\">      switch (action.type) {</span>\n<span class=\"grvsc-line\">        case &quot;ADD_TODO&quot;:</span>\n<span class=\"grvsc-line\">          return state.map(todo =&gt; {</span>\n<span class=\"grvsc-line\">            if (todo.id === action.id) {</span>\n<span class=\"grvsc-line\">              return { ...todo, complete: true };</span>\n<span class=\"grvsc-line\">            } else {</span>\n<span class=\"grvsc-line\">              return todo;</span>\n<span class=\"grvsc-line\">            }</span>\n<span class=\"grvsc-line\">          });</span>\n<span class=\"grvsc-line\">        case &quot;REMOVE_TODO&quot;:</span>\n<span class=\"grvsc-line\">          return state.map(todo =&gt; {</span>\n<span class=\"grvsc-line\">            if (todo.id === action.id) {</span>\n<span class=\"grvsc-line\">              return { ...todo, complete: false };</span>\n<span class=\"grvsc-line\">            } else {</span>\n<span class=\"grvsc-line\">              return todo;</span>\n<span class=\"grvsc-line\">            }</span>\n<span class=\"grvsc-line\">          });</span>\n<span class=\"grvsc-line\">        default:</span>\n<span class=\"grvsc-line\">          return state;</span>\n<span class=\"grvsc-line\">      }</span>\n<span class=\"grvsc-line\">    };</span></code></pre>\n<p>There are two types of actions which are equivalent to two states. they used to toggle the complete boolean field and additional payload to identify incoming action.</p>\n<p>The state which is managed in this reducer is an array of items:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">const initialTodos = [</span>\n<span class=\"grvsc-line\">      {</span>\n<span class=\"grvsc-line\">        id: &quot;t1&quot;,</span>\n<span class=\"grvsc-line\">        task: &quot;Add Task 1&quot;,</span>\n<span class=\"grvsc-line\">        complete: false</span>\n<span class=\"grvsc-line\">      },</span>\n<span class=\"grvsc-line\">      {</span>\n<span class=\"grvsc-line\">        id: &quot;t2&quot;,</span>\n<span class=\"grvsc-line\">        task: &quot;Add Task 2&quot;,</span>\n<span class=\"grvsc-line\">        complete: false</span>\n<span class=\"grvsc-line\">      }</span>\n<span class=\"grvsc-line\">    ];</span></code></pre>\n<p>In code, The useReducer hook is used for complex state and state transitions. It takes a reducer function and an initial state as input and returns the current state and a dispatch function as output</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"> const [todos, dispatch] = React.useReducer(</span>\n<span class=\"grvsc-line\">    todoReducer,</span>\n<span class=\"grvsc-line\">    initialTodos</span>\n<span class=\"grvsc-line\">  );</span></code></pre>\n<p>Complete file:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">import React from &quot;react&quot;;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">const initialTodos = [</span>\n<span class=\"grvsc-line\">  {</span>\n<span class=\"grvsc-line\">    id: &quot;t1&quot;,</span>\n<span class=\"grvsc-line\">    task: &quot;Add Task 1&quot;,</span>\n<span class=\"grvsc-line\">    complete: false</span>\n<span class=\"grvsc-line\">  },</span>\n<span class=\"grvsc-line\">  {</span>\n<span class=\"grvsc-line\">    id: &quot;t2&quot;,</span>\n<span class=\"grvsc-line\">    task: &quot;Add Task 2&quot;,</span>\n<span class=\"grvsc-line\">    complete: false</span>\n<span class=\"grvsc-line\">  }</span>\n<span class=\"grvsc-line\">];</span>\n<span class=\"grvsc-line\">const todoReducer = (state, action) =&gt; {</span>\n<span class=\"grvsc-line\">  switch (action.type) {</span>\n<span class=\"grvsc-line\">    case &quot;ADD_TODO&quot;:</span>\n<span class=\"grvsc-line\">      return state.map(todo =&gt; {</span>\n<span class=\"grvsc-line\">        if (todo.id === action.id) {</span>\n<span class=\"grvsc-line\">          return { ...todo, complete: true };</span>\n<span class=\"grvsc-line\">        } else {</span>\n<span class=\"grvsc-line\">          return todo;</span>\n<span class=\"grvsc-line\">        }</span>\n<span class=\"grvsc-line\">      });</span>\n<span class=\"grvsc-line\">    case &quot;REMOVE_TODO&quot;:</span>\n<span class=\"grvsc-line\">      return state.map(todo =&gt; {</span>\n<span class=\"grvsc-line\">        if (todo.id === action.id) {</span>\n<span class=\"grvsc-line\">          return { ...todo, complete: false };</span>\n<span class=\"grvsc-line\">        } else {</span>\n<span class=\"grvsc-line\">          return todo;</span>\n<span class=\"grvsc-line\">        }</span>\n<span class=\"grvsc-line\">      });</span>\n<span class=\"grvsc-line\">    default:</span>\n<span class=\"grvsc-line\">      return state;</span>\n<span class=\"grvsc-line\">  }</span>\n<span class=\"grvsc-line\">};</span>\n<span class=\"grvsc-line\">const App = () =&gt; {</span>\n<span class=\"grvsc-line\">  const [todos, dispatch] = React.useReducer(todoReducer, initialTodos);</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">  const handleChange = todo =&gt; {</span>\n<span class=\"grvsc-line\">    dispatch({</span>\n<span class=\"grvsc-line\">      type: todo.complete ? &quot;REMOVE_TODO&quot; : &quot;ADD_TODO&quot;,</span>\n<span class=\"grvsc-line\">      id: todo.id</span>\n<span class=\"grvsc-line\">    });</span>\n<span class=\"grvsc-line\">  };</span>\n<span class=\"grvsc-line\">  return (</span>\n<span class=\"grvsc-line\">    &lt;ul&gt;</span>\n<span class=\"grvsc-line\">      {todos.map(todo =&gt; (</span>\n<span class=\"grvsc-line\">        &lt;li key={todo.id}&gt;</span>\n<span class=\"grvsc-line\">          &lt;label&gt;</span>\n<span class=\"grvsc-line\">            &lt;input</span>\n<span class=\"grvsc-line\">              type=&quot;checkbox&quot;</span>\n<span class=\"grvsc-line\">              checked={todo.complete}</span>\n<span class=\"grvsc-line\">              onChange={() =&gt; handleChange(todo)}</span>\n<span class=\"grvsc-line\">            /&gt;</span>\n<span class=\"grvsc-line\">            {todo.task}</span>\n<span class=\"grvsc-line\">          &lt;/label&gt;</span>\n<span class=\"grvsc-line\">        &lt;/li&gt;</span>\n<span class=\"grvsc-line\">      ))}</span>\n<span class=\"grvsc-line\">    &lt;/ul&gt;</span>\n<span class=\"grvsc-line\">  );</span>\n<span class=\"grvsc-line\">};</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">export default App;</span></code></pre>\n<p>Let’s do a similar example with Redux.</p>\n<p>Store in <em>App.js</em>.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">import React from &quot;react&quot;;</span>\n<span class=\"grvsc-line\">import { Provider } from &quot;react-redux&quot;;</span>\n<span class=\"grvsc-line\">import { createStore } from &quot;redux&quot;;</span>\n<span class=\"grvsc-line\">import rootReducer from &quot;./reducers&quot;;</span>\n<span class=\"grvsc-line\">import Todo from &quot;./Components/TODO&quot;;</span>\n<span class=\"grvsc-line\">const store = createStore(rootReducer);</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">function App() {</span>\n<span class=\"grvsc-line\">  return (</span>\n<span class=\"grvsc-line\">    &lt;div className=&quot;App&quot;&gt;</span>\n<span class=\"grvsc-line\">      &lt;Provider store={store}&gt;</span>\n<span class=\"grvsc-line\">        &lt;Todo /&gt;</span>\n<span class=\"grvsc-line\">      &lt;/Provider&gt;</span>\n<span class=\"grvsc-line\">    &lt;/div&gt;</span>\n<span class=\"grvsc-line\">  );</span>\n<span class=\"grvsc-line\">}</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">export default App;</span></code></pre>\n<p>Actions in <em>actions/index.js</em>.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"12\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">export const addTodo = id =&gt; ({</span>\n<span class=\"grvsc-line\">  type: &quot;ADD_TODO&quot;,</span>\n<span class=\"grvsc-line\">  id</span>\n<span class=\"grvsc-line\">});</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">export const removeTodo = id =&gt; ({</span>\n<span class=\"grvsc-line\">  type: &quot;REMOVE_TODO&quot;,</span>\n<span class=\"grvsc-line\">  id</span>\n<span class=\"grvsc-line\">});</span></code></pre>\n<p>Reducers in <em>reducers/index.js</em>.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"13\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">const initialTodos = [</span>\n<span class=\"grvsc-line\">  {</span>\n<span class=\"grvsc-line\">    id: &quot;t1&quot;,</span>\n<span class=\"grvsc-line\">    task: &quot;Add Task 1&quot;,</span>\n<span class=\"grvsc-line\">    complete: false</span>\n<span class=\"grvsc-line\">  },</span>\n<span class=\"grvsc-line\">  {</span>\n<span class=\"grvsc-line\">    id: &quot;t2&quot;,</span>\n<span class=\"grvsc-line\">    task: &quot;Add Task 2&quot;,</span>\n<span class=\"grvsc-line\">    complete: false</span>\n<span class=\"grvsc-line\">  }</span>\n<span class=\"grvsc-line\">];</span>\n<span class=\"grvsc-line\">const todos = (state = initialTodos, action) =&gt; {</span>\n<span class=\"grvsc-line\">  switch (action.type) {</span>\n<span class=\"grvsc-line\">    case &quot;ADD_TODO&quot;:</span>\n<span class=\"grvsc-line\">      return state.map(todo =&gt; {</span>\n<span class=\"grvsc-line\">        if (todo.id === action.id) {</span>\n<span class=\"grvsc-line\">          return { ...todo, complete: true };</span>\n<span class=\"grvsc-line\">        } else {</span>\n<span class=\"grvsc-line\">          return todo;</span>\n<span class=\"grvsc-line\">        }</span>\n<span class=\"grvsc-line\">      });</span>\n<span class=\"grvsc-line\">    case &quot;REMOVE_TODO&quot;:</span>\n<span class=\"grvsc-line\">      return state.map(todo =&gt; {</span>\n<span class=\"grvsc-line\">        if (todo.id === action.id) {</span>\n<span class=\"grvsc-line\">          return { ...todo, complete: false };</span>\n<span class=\"grvsc-line\">        } else {</span>\n<span class=\"grvsc-line\">          return todo;</span>\n<span class=\"grvsc-line\">        }</span>\n<span class=\"grvsc-line\">      });</span>\n<span class=\"grvsc-line\">    default:</span>\n<span class=\"grvsc-line\">      return state;</span>\n<span class=\"grvsc-line\">  }</span>\n<span class=\"grvsc-line\">};</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">export default todos;</span></code></pre>\n<p>FIle components/Todo.js</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"14\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">import React from &quot;react&quot;;</span>\n<span class=\"grvsc-line\">import { connect } from &quot;react-redux&quot;;</span>\n<span class=\"grvsc-line\">import { addTodo, removeTodo } from &quot;../../redux/actions/authActions&quot;;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">const Todo = ({ todos, addTodo, removeTodo }) =&gt; {</span>\n<span class=\"grvsc-line\">  const handleChange = todo =&gt; {</span>\n<span class=\"grvsc-line\">    if (todo.complete) {</span>\n<span class=\"grvsc-line\">      removeTodo(todo.id);</span>\n<span class=\"grvsc-line\">    } else {</span>\n<span class=\"grvsc-line\">      addTodo(todo.id);</span>\n<span class=\"grvsc-line\">    }</span>\n<span class=\"grvsc-line\">  };</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">  return (</span>\n<span class=\"grvsc-line\">    &lt;ul&gt;</span>\n<span class=\"grvsc-line\">      {todos.map(todo =&gt; (</span>\n<span class=\"grvsc-line\">        &lt;li key={todo.id}&gt;</span>\n<span class=\"grvsc-line\">          &lt;label&gt;</span>\n<span class=\"grvsc-line\">            &lt;input</span>\n<span class=\"grvsc-line\">              type=&quot;checkbox&quot;</span>\n<span class=\"grvsc-line\">              checked={todo.complete}</span>\n<span class=\"grvsc-line\">              onChange={() =&gt; handleChange(todo)}</span>\n<span class=\"grvsc-line\">            /&gt;</span>\n<span class=\"grvsc-line\">            {todo.task}</span>\n<span class=\"grvsc-line\">          &lt;/label&gt;</span>\n<span class=\"grvsc-line\">        &lt;/li&gt;</span>\n<span class=\"grvsc-line\">      ))}</span>\n<span class=\"grvsc-line\">    &lt;/ul&gt;</span>\n<span class=\"grvsc-line\">  );</span>\n<span class=\"grvsc-line\">};</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">const mapStateToProps = state =&gt; ({ todos: state.auth.todos });</span>\n<span class=\"grvsc-line\">const mapDispatchToProps = dispatch =&gt; {</span>\n<span class=\"grvsc-line\">  return {</span>\n<span class=\"grvsc-line\">    addTodo: id =&gt; dispatch(addTodo(id)),</span>\n<span class=\"grvsc-line\">    removeTodo: id =&gt; dispatch(removeTodo(id))</span>\n<span class=\"grvsc-line\">  };</span>\n<span class=\"grvsc-line\">};</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">export default connect(</span>\n<span class=\"grvsc-line\">  mapStateToProps,</span>\n<span class=\"grvsc-line\">  mapDispatchToProps</span>\n<span class=\"grvsc-line\">)(Todo);</span></code></pre>\n<p>React offers react hooks which can be used as an alternative for <code>connect()</code>. You can use built-in hooks mainly useState, UseReducer and useContext and because of these you often may not require Redux. </p>\n<p>But for large applications, you can use both redux and react hooks. it works great! React Hook is a useful new feature, and the addition of React-Redux with Redux-specific hooks is a great step towards simplifying Redux development.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","frontmatter":{"date":"July 17, 2020","updated_date":null,"description":"Learn about React state management and why we need state management and how to use it with React hooks and Redux in the best possible way.","title":"React state management: What is it and why to use it?","tags":["React","Redux","Hooks"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/ef27359624480604211f0e018dd47f18/bc59e/title-image.png","srcSet":"/static/ef27359624480604211f0e018dd47f18/69585/title-image.png 200w,\n/static/ef27359624480604211f0e018dd47f18/497c6/title-image.png 400w,\n/static/ef27359624480604211f0e018dd47f18/bc59e/title-image.png 512w","sizes":"(max-width: 512px) 100vw, 512px"}}},"author":{"id":"Versha Gupta","github":"vershagupta","avatar":null}}}},{"node":{"excerpt":"What are HTTP Security Headers ? When we visit any website in the browser, the browser sends some request headers to the server and the…","fields":{"slug":"/engineering/http-security-headers/"},"html":"<h3 id=\"what-are-http-security-headers-\" style=\"position:relative;\"><a href=\"#what-are-http-security-headers-\" aria-label=\"what are http security headers  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What are HTTP Security Headers ?</h3>\n<p>When we visit any website in the browser, the browser sends some request headers to the server and the server responds with HTTP response headers. These headers are used by the client and server to share information as a part of the HTTP protocol. Browsers have defined behavior of the web page according to these headers during communication with the server. These headers are mainly a combination of key-value pairs separated by a colon <code>:</code>. There are many HTTP headers, but here I'm covering some very useful web security headers, which will improve your website security. </p>\n<h3 id=\"why-http-security-headers-are-necessary-\" style=\"position:relative;\"><a href=\"#why-http-security-headers-are-necessary-\" aria-label=\"why http security headers are necessary  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why HTTP Security Headers are necessary ?</h3>\n<p>As you know, nowadays too many data breaches are happening, many websites are hacked due to misconfiguration or lack of protection. These security headers will protect your website from some common attacks like XSS, code injection, clickjacking, etc. Additionally these headers increases your website SEO score.</p>\n<h4 id=\"1-enforcing-https-http-strict-transport-security-hsts\" style=\"position:relative;\"><a href=\"#1-enforcing-https-http-strict-transport-security-hsts\" aria-label=\"1 enforcing https http strict transport security hsts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Enforcing HTTPS (HTTP Strict Transport Security (HSTS))</h4>\n<p>HTTP Strict Transport Security security header helps to protect websites against man-in-the-middle attacks and cookie hijacking. Let's say, you have one website <code>www.example.com</code> and you have purchased SSL certification for migrating your website from HTTP to HTTPS. Now suppose old users are still opening your website using the HTTP, so they may redirect your HTTP users to HTTPS via redirection as a solution. Since visitors may first communicate with the non-encrypted version of the site before being redirected. This creates an opportunity for a man-in-the-middle attack. The HTTP Strict Transport Security header informs the browser that it should never load a site using HTTP and should automatically convert all attempts to access the site using HTTP to HTTPS requests.</p>\n<h5 id=\"syntax\" style=\"position:relative;\"><a href=\"#syntax\" aria-label=\"syntax permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Syntax</h5>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">  Strict-Transport-Security: max-age=&lt;expire-time&gt;</span>\n<span class=\"grvsc-line\">  Strict-Transport-Security: max-age=&lt;expire-time&gt;; includeSubDomains</span>\n<span class=\"grvsc-line\">  Strict-Transport-Security: max-age=&lt;expire-time&gt;; preload</span></code></pre>\n<h5 id=\"hsts-directives-\" style=\"position:relative;\"><a href=\"#hsts-directives-\" aria-label=\"hsts directives  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>HSTS directives :</h5>\n<table>\n<thead>\n<tr>\n<th>Directives</th>\n<th>Explanation</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>max-age=&#x3C;expire-time></code></td>\n<td>This directive allows us to specify the amount of time (in seconds) that the content of the header will be stored in the browser cache.</td>\n</tr>\n<tr>\n<td><code>includeSubDomains</code></td>\n<td>If this optional parameter is specified, this rule applies to all of the site's subdomains as well.</td>\n</tr>\n<tr>\n<td><code>Preload</code></td>\n<td>This is not part of the specifications. Please see the Preloading Strict Transport Security</td>\n</tr>\n</tbody>\n</table>\n<h4 id=\"2-cross-site-scripting-protection-x-xss\" style=\"position:relative;\"><a href=\"#2-cross-site-scripting-protection-x-xss\" aria-label=\"2 cross site scripting protection x xss permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Cross-Site Scripting Protection (X-XSS)</h4>\n<p>X-XSS header helps protect websites against script injection attacks. When an attacker injects malicious JavaScript code into an HTTP request for accessing confidential information such as session cookies, at that time HTTP X-XSS-Protection header can stop the browsers from loading suce web pages, whenever any detect is reflected cross-site scripting (XSS) attacks. XSS is a very common and effective attack.</p>\n<h4 id=\"syntax-1\" style=\"position:relative;\"><a href=\"#syntax-1\" aria-label=\"syntax 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Syntax</h4>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">  X-XSS-Protection: 0 </span>\n<span class=\"grvsc-line\">  X-XSS-Protection: 1 </span>\n<span class=\"grvsc-line\">  X-XSS-Protection: 1; mode=block </span>\n<span class=\"grvsc-line\">  X-XSS-Protection: 1; report=&lt;reporting-uri&gt;</span></code></pre>\n<h5 id=\"x-xss-protection-directives\" style=\"position:relative;\"><a href=\"#x-xss-protection-directives\" aria-label=\"x xss protection directives permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>X-XSS-Protection directives:</h5>\n<table>\n<thead>\n<tr>\n<th>Attribute</th>\n<th>Description</th>\n<th></th>\n<th></th>\n<th></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>0</code></td>\n<td>Disable the XSS Filtering</td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td><code>1</code></td>\n<td>Enable the XSS Filtering and remove the unsafe part</td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td><code>1; mode=block</code></td>\n<td>Browser prevents the entire page from rendering when an XSS attack is detected.</td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td><code>1; report=&#x3C;reporting-URI> (Chromium only)</code></td>\n<td>When a cross-site scripting attack is detected, the browser will remove the unsafe parts from the page and report the violation on the reporting URL.</td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<h4 id=\"3-website-iframe-protection\" style=\"position:relative;\"><a href=\"#3-website-iframe-protection\" aria-label=\"3 website iframe protection permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Website IFrame Protection</h4>\n<p>The X-Frame-Options HTTP response header can be used to instruct the browser whether a web page should be allowed to render a <code>&#x3C;frame>, &#x3C;iframe>, &#x3C;embed> or &#x3C;object></code>  element on website or not.</p>\n<p>This header protects users against ClickJacking attacks. An attacker uses multiple tricks to trick the user into clicking something different than what they think they’re clicking.</p>\n<h4 id=\"syntax-2\" style=\"position:relative;\"><a href=\"#syntax-2\" aria-label=\"syntax 2 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Syntax</h4>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">  X-Frame-Options: DENY </span>\n<span class=\"grvsc-line\">  X-Frame-Options: SAMEORIGIN</span></code></pre>\n<h4 id=\"x-frame-options-directives-include-the-following\" style=\"position:relative;\"><a href=\"#x-frame-options-directives-include-the-following\" aria-label=\"x frame options directives include the following permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>X-Frame-Options directives include the following:</h4>\n<table>\n<thead>\n<tr>\n<th>Directives</th>\n<th>Explanation</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>DENY</code></td>\n<td>This will not allow to page rendering in the <code>&#x3C;frame>, &#x3C;iframe>, &#x3C;embed> or &#x3C;object></code></td>\n</tr>\n<tr>\n<td><code>SAMEORIGIN</code></td>\n<td>This will allow the page to only be displayed in a <code>&#x3C;frame>, &#x3C;iframe>, &#x3C;embed> or &#x3C;object></code> on the same origin.</td>\n</tr>\n</tbody>\n</table>\n<h4 id=\"4-preventing-content-type-sniffing\" style=\"position:relative;\"><a href=\"#4-preventing-content-type-sniffing\" aria-label=\"4 preventing content type sniffing permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. Preventing Content-Type Sniffing</h4>\n<p><strong>X-Content-Type-Options</strong> response header prevents the browser from MIME-sniffing a response away from the declared content-type. A MIME-sniffing vulnerability allows an attacker to inject a malicious resource, such as a malicious executable script, Suppose an attacker changes the response for an innocent resource, such as an image. With MIME sniffing, the browser will ignore the declared image content type, and instead of rendering an image will execute the malicious script. </p>\n<p>Syntax </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">  X-Content-Type-Options: nosniff </span></code></pre>\n<h4 id=\"x-content-type-options-directives\" style=\"position:relative;\"><a href=\"#x-content-type-options-directives\" aria-label=\"x content type options directives permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>X-Content-Type-Options directives:</h4>\n<p><code>nosniff</code> - Blocks a request if the request destination is of type:</p>\n<h4 id=\"6-content-security-policy\" style=\"position:relative;\"><a href=\"#6-content-security-policy\" aria-label=\"6 content security policy permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>6. Content Security Policy</h4>\n<p>Content-Security-Policy header is used to instruct the browser to load only the allowed content defined in the policy. This uses the whitelisting approach which tells the browser from where to load the images, scripts, CSS, applets, etc. If implemented properly, this policy prevents the exploitation of Cross-Site Scripting (XSS), ClickJacking, and HTML injection attacks.</p>\n<h4 id=\"syntax-3\" style=\"position:relative;\"><a href=\"#syntax-3\" aria-label=\"syntax 3 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Syntax</h4>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">  Content-Security-Policy: &lt;policy-directive&gt;; &lt;policy-directive&gt;</span></code></pre>\n<p>More information about the Content Security policy can be found on  <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy\">Mozilla’s website </a>.</p>\n<h3 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h3>\n<p>As a web developer, you can follow this guide for adding security headers to your website. HTTP headers can be configured on the server to enhance the overall security of the web application. You can easily validate your website security headers. Multiple free online tools are available to check the same:</p>\n<ul>\n<li><a href=\"https://securityheaders.com/\">Security Headers</a></li>\n<li><a href=\"https://www.serpworx.com/check-security-headers/\">Serpworx</a></li>\n</ul>\n<p>These headers reduce the potential vulnerabilities of the application. Alongside these headers will add one layer of security still we need to add more layer's of security in our web application  </p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","frontmatter":{"date":"July 15, 2020","updated_date":null,"description":"HTTP security headers are a fundamental part of website security. Upon implementation, they protect you against the types of attacks that your site is most likely to come across. These headers protect against XSS, code injection, clickjacking, etc. This article explains most commonly used HTTP headers in context to application security","title":"HTTP Security Headers","tags":["Security","HTTP Headers"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/bd7410a85a59f44b894624a2b98f28d4/f422e/http-security-headers.jpg","srcSet":"/static/bd7410a85a59f44b894624a2b98f28d4/f836f/http-security-headers.jpg 200w,\n/static/bd7410a85a59f44b894624a2b98f28d4/2244e/http-security-headers.jpg 400w,\n/static/bd7410a85a59f44b894624a2b98f28d4/f422e/http-security-headers.jpg 640w","sizes":"(max-width: 640px) 100vw, 640px"}}},"author":{"id":"Vijay Singh Shekhawat","github":"code-vj","avatar":null}}}},{"node":{"excerpt":"Have you ever heard of SonarQube? Would you like to know What it is? And how to use it? And its benefits to the production phase of software…","fields":{"slug":"/engineering/sonarqube/"},"html":"<p>Have you ever heard of SonarQube? Would you like to know What it is? And how to use it? And its benefits to the production phase of software development. Then you are at the right place; in this tutorial of SonarQube, I will give you a walkthrough of every aspect of SonarQube.</p>\n<p>Let's dive in!!!</p>\n<h2 id=\"what-is-sonarqube\" style=\"position:relative;\"><a href=\"#what-is-sonarqube\" aria-label=\"what is sonarqube permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is SonarQube?</h2>\n<p>SonarQube is an open-source platform developed by SonarSource for continuous inspection of code quality. Sonar does static code analysis, which provides a detailed report of bugs, code smells, vulnerabilities, code duplications. </p>\n<p>It supports 25+ major programming languages through built-in rulesets and can also be extended with various plugins.</p>\n<h2 id=\"benefits-of-sonarqube\" style=\"position:relative;\"><a href=\"#benefits-of-sonarqube\" aria-label=\"benefits of sonarqube permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Benefits of SonarQube</h2>\n<ol>\n<li><strong>Sustainability</strong> - Reduces complexity, possible vulnerabilities, and code duplications, optimising the life of applications.</li>\n<li><strong>Increase productivity</strong> - Reduces the scale, cost of maintenance, and risk of the application; as such, it removes the need to spend more time changing the code</li>\n<li><strong>Quality code</strong> - Code quality control is an inseparable part of the process of software development.</li>\n<li><strong>Detect Errors</strong> - Detects errors in the code and alerts developers to fix them automatically before submitting them for output.</li>\n<li><strong>Increase consistency</strong> - Determines where the code criteria are breached and enhances the quality</li>\n<li><strong>Business scaling</strong> - No restriction on the number of projects to be evaluated</li>\n<li><strong>Enhance developer skills</strong> - Regular feedback on quality problems helps developers to improve their coding skills</li>\n</ol>\n<h2 id=\"why-sonarqube\" style=\"position:relative;\"><a href=\"#why-sonarqube\" aria-label=\"why sonarqube permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why SonarQube?</h2>\n<p>Developers <a href=\"/agile-development-team/\">working with hard deadlines</a> to deliver the required functionality to the customer. It is so important for developers that many times they compromise with the code quality, potential bugs, code duplications, and bad distribution of complexity.</p>\n<p>Additionally, they tend to leave unused variables, methods, etc. In this scenario, the code would work in the desired way.</p>\n<p>Do you think this is a proper way to deliver functionality? </p>\n<p>The answer is NO.</p>\n<p>To avoid these issues in code, developers should always follow the good coding practice, but sometimes it is not possible to follow the rules and maintain the good quality as there may be many reasons.</p>\n<p>In order to achieve continuous code integration and deployment, developers need a tool that not only works once to check and tell them the problems in the code but also to track and control the code to check continuous code quality. To satisfy all these requirements, here comes SonarQube in the picture.</p>\n<h2 id=\"how-to-setup-and-use-the-sonarqube-plugin\" style=\"position:relative;\"><a href=\"#how-to-setup-and-use-the-sonarqube-plugin\" aria-label=\"how to setup and use the sonarqube plugin permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to setup and use the sonarqube plugin</h2>\n<p>This section will explain the steps or procedures to configure the sonarqube plugin for all the major programming languages.</p>\n<h3 id=\"prerequisites\" style=\"position:relative;\"><a href=\"#prerequisites\" aria-label=\"prerequisites permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Prerequisites:</h3>\n<p>The only prerequisite for running SonarQube is to have Java (Oracle JRE 11 or OpenJDK 11) installed on your machine. [<a href=\"https://docs.sonarsource.com/sonarqube-server/latest/setup-and-upgrade/installation-requirements/server-host/\">details</a>]</p>\n<p>Set the PATH system variable: <a href=\"https://www.java.com/en/download/help/path.xml\">How do I set or change the PATH system variable?</a></p>\n<blockquote>\n<p>Note: In order to analyze JavaScript code, you need to have Node.js >= 8 installed on the machine running the scan. If a standard node is not available, you have to set the property <code>sonar.nodejs.executable</code> to an absolute path to Node.js executable. [<a href=\"https://docs.sonarqube.org/latest/analysis/languages/javascript/\">details</a>]</p>\n</blockquote>\n<h3 id=\"downloads-sonarqube-and-sonar-scanner\" style=\"position:relative;\"><a href=\"#downloads-sonarqube-and-sonar-scanner\" aria-label=\"downloads sonarqube and sonar scanner permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Downloads Sonarqube and Sonar Scanner:</h3>\n<ul>\n<li><a href=\"https://www.sonarqube.org/downloads\">LTS version of sonarqube</a></li>\n<li><a href=\"https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/\">Download sonar-scanner</a> based on your platform\nAfter the above process, Add the sonar scanner path to environment variables.</li>\n</ul>\n<h3 id=\"run-sonarqube-server\" style=\"position:relative;\"><a href=\"#run-sonarqube-server\" aria-label=\"run sonarqube server permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Run Sonarqube server:</h3>\n<ul>\n<li>\n<p>To start the sonar server open cmd or terminal and set sonarqube bin folder path and choose the platform and run the following command. Examples :</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">#For Windows(cmd):</span>\n<span class=\"grvsc-line\">C:\\sonarqube\\bin\\windows-x86-64&gt;StartSonar.bat</span>\n<span class=\"grvsc-line\">#For other OS (terminal): </span>\n<span class=\"grvsc-line\">C:\\sonarqube\\bin\\[OS]&gt;sonar.sh</span></code></pre>\n<p>Once the sonar server up successfully, then Log in to <code>http://localhost:9000</code> with System Administrator credentials (login=admin, password=admin).</p>\n</li>\n</ul>\n<h3 id=\"creating-and-analysing-a-project\" style=\"position:relative;\"><a href=\"#creating-and-analysing-a-project\" aria-label=\"creating and analysing a project permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Creating and Analysing a Project:</h3>\n<ul>\n<li>Go to create a new project and enter the project key, and display name details. (+ sign on the top right side).</li>\n<li>Provide a token by either generating a token or using an existing token (find existing token on  <code>http://localhost:9000/account/security</code> page).</li>\n<li>Run analysis on the project by selecting the programming language used in the repository and your system OS. </li>\n<li>As we already downloaded and set up the sonar-scanner path, No need to download it again.</li>\n<li>\n<p>Now copy the displaying command and Go to your project path and Run the copied command. It looks like below:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">sonar-scanner.bat -D&quot;sonar.projectKey=rtetre&quot; -D&quot;sonar.sources=.&quot;-D&quot;sonar.host.url=http://localhost:9000&quot; -D&quot;sonar.login=e932dxxxxx9a8e5c7903xxxxxx96928xxxxxxx&quot;</span></code></pre>\n</li>\n</ul>\n<p>After the analysis is done, you can either browse the provided link to see the sonar report directly [Ex.: <code>http://localhost:9000/dashboard?id=</code>] or go to the project section to see the newly generated sonar report of your project. </p>\n<p>If you want to exclude some files from analysis(like test files), then go to the administration section and navigate to the General setting, select the programming language used, and set a list of file path patterns to be excluded from the analysis.</p>\n<p>Now re-run the analysis command given above for an updated sonar report.</p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>The goal of SonarQube is to empower developers first and to grow an open community around the quality and security of code. I hope with this quick tutorial of sonarqube you have the basic idea of the functionalities, and If you believe so, drop a comment and show your support.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","frontmatter":{"date":"July 11, 2020","updated_date":null,"description":"SonarQube is a universal tool for static code analysis that has become more or less the industry standard. Keeping code clean, simple, and easy to read is also a lot easier with SonarQube.","title":"Sonarqube: What it is and why to use it?","tags":["Code Quality","SonarQube"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.7699115044247788,"src":"/static/d565ea49a97bb0cce8f1937aa87e64b7/ee604/sonarqube.png","srcSet":"/static/d565ea49a97bb0cce8f1937aa87e64b7/69585/sonarqube.png 200w,\n/static/d565ea49a97bb0cce8f1937aa87e64b7/497c6/sonarqube.png 400w,\n/static/d565ea49a97bb0cce8f1937aa87e64b7/ee604/sonarqube.png 800w,\n/static/d565ea49a97bb0cce8f1937aa87e64b7/f3583/sonarqube.png 1200w,\n/static/d565ea49a97bb0cce8f1937aa87e64b7/e4d72/sonarqube.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Kheenvraj Lomror","github":"rajlomror","avatar":null}}}},{"node":{"excerpt":"In this blog, we’ll see how to create and validate a JWT(JSON Web Token) in Deno. For this, we’ll be using djwt, the absolute minimum…","fields":{"slug":"/engineering/jwt-authentication-with-deno/"},"html":"<p>In this blog, we’ll see how to create and validate a JWT(JSON Web Token) in Deno. For this, we’ll be using <a href=\"https://github.com/timonson/djwt\">djwt</a>, the absolute minimum library to make JSON Web Tokens in deno and <a href=\"https://deno.land/x/oak@v17.1.4\">Oak framework</a></p>\n<h2 id=\"before-you-get-started\" style=\"position:relative;\"><a href=\"#before-you-get-started\" aria-label=\"before you get started permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Before You Get Started</h2>\n<p>This tutorial assumes you have:</p>\n<ul>\n<li>A basic understanding of JavaScript and Deno</li>\n<li>Latest Deno version installed on your system</li>\n</ul>\n<h3 id=\"what-is-jwt\" style=\"position:relative;\"><a href=\"#what-is-jwt\" aria-label=\"what is jwt permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is JWT?</h3>\n<p>JSON Web Token is an internet standard used to create tokens for an application. These tokens hold JSON data and are cryptographically signed. </p>\n<p>Here is how a sample Json Web Token looks like</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Im9sYXR1bmRlZ2FydWJhQGdtYWlsLmNvbSIsIm</span></code></pre>\n<p>JWT is a good way of securely sending information between parties. Because JWTs can be signed—for, you can be sure the senders are who they say they are. And, as the signature is generated using the header and the payload, you can also verify that the content hasn't been tampered with.</p>\n<p>JWT can contain user information in the payload and also can be used in the session to authenticate the user. </p>\n<p>If you want to know more about JSON Web Token, We have a very good <a href=\"/jwt/\">article</a> about it.</p>\n<h3 id=\"how-to-generate-jwt-token-in-deno\" style=\"position:relative;\"><a href=\"#how-to-generate-jwt-token-in-deno\" aria-label=\"how to generate jwt token in deno permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to generate JWT token in Deno</h3>\n<p>First, let's set up a Deno server to accept requests, for it, we are using <a href=\"https://deno.land/x/oak\">Oak framework</a>, it is quite simple and few lines of codes as you can see below.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"ts\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">// index.ts</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> { </span><span class=\"mtk12\">Application</span><span class=\"mtk1\">, </span><span class=\"mtk12\">Router</span><span class=\"mtk1\"> } </span><span class=\"mtk15\">from</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;https://deno.land/x/oak/mod.ts&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">router</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Router</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">router</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  .</span><span class=\"mtk11\">get</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/&quot;</span><span class=\"mtk1\">, (</span><span class=\"mtk12\">context</span><span class=\"mtk1\">) </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">body</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&quot;JWT Example!&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  })</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">app</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Application</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">use</span><span class=\"mtk1\">(</span><span class=\"mtk12\">router</span><span class=\"mtk1\">.</span><span class=\"mtk11\">routes</span><span class=\"mtk1\">());</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">use</span><span class=\"mtk1\">(</span><span class=\"mtk12\">router</span><span class=\"mtk1\">.</span><span class=\"mtk11\">allowedMethods</span><span class=\"mtk1\">());</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">await</span><span class=\"mtk1\"> </span><span class=\"mtk12\">app</span><span class=\"mtk1\">.</span><span class=\"mtk11\">listen</span><span class=\"mtk1\">({ </span><span class=\"mtk12\">port:</span><span class=\"mtk1\"> </span><span class=\"mtk7\">8000</span><span class=\"mtk1\"> });</span></span></code></pre>\n<p>Once our program is ready for accepting request Let's import djwt functions to generate JWT token, In below code we can use a secret key, expiry time for JWT token in 1 hour from the time program will run and we are using HS256 algorithm.</p>\n<p>Add the below code in index.ts and update the router as shown below, you can now get a brand new token on <code>http://localhost:8000/generate</code></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"ts\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">// index.ts</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">...</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> { </span><span class=\"mtk12\">makeJwt</span><span class=\"mtk1\">, </span><span class=\"mtk12\">setExpiration</span><span class=\"mtk1\">, </span><span class=\"mtk12\">Jose</span><span class=\"mtk1\">, </span><span class=\"mtk12\">Payload</span><span class=\"mtk1\"> } </span><span class=\"mtk15\">from</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;https://deno.land/x/djwt/create.ts&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">key</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&quot;secret-key&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">payload</span><span class=\"mtk1\">: </span><span class=\"mtk10\">Payload</span><span class=\"mtk1\"> = {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">iss:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;Jon Doe&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">exp:</span><span class=\"mtk1\"> </span><span class=\"mtk11\">setExpiration</span><span class=\"mtk1\">(</span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Date</span><span class=\"mtk1\">().</span><span class=\"mtk11\">getTime</span><span class=\"mtk1\">() + </span><span class=\"mtk7\">60000</span><span class=\"mtk1\">),</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">};</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">header</span><span class=\"mtk1\">: </span><span class=\"mtk10\">Jose</span><span class=\"mtk1\"> = {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">alg:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;HS256&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">typ:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;JWT&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">};</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">const</span><span class=\"mtk1\"> </span><span class=\"mtk12\">router</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Router</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">router</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  .</span><span class=\"mtk11\">get</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/&quot;</span><span class=\"mtk1\">, (</span><span class=\"mtk12\">context</span><span class=\"mtk1\">) </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">body</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&quot;JWT Example!&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  })</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  .</span><span class=\"mtk11\">get</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/generate&quot;</span><span class=\"mtk1\">, (</span><span class=\"mtk12\">context</span><span class=\"mtk1\">) </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">body</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">makeJwt</span><span class=\"mtk1\">({ </span><span class=\"mtk12\">header</span><span class=\"mtk1\">, </span><span class=\"mtk12\">payload</span><span class=\"mtk1\">, </span><span class=\"mtk12\">key</span><span class=\"mtk1\"> }) + </span><span class=\"mtk8\">&quot;</span><span class=\"mtk6\">\\n</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  })</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">...</span></span></code></pre>\n<h3 id=\"validating-a-jwt-token\" style=\"position:relative;\"><a href=\"#validating-a-jwt-token\" aria-label=\"validating a jwt token permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Validating a JWT token</h3>\n<p>Once you get a JWT token you can validate the token by <code>validateJwt</code> function in djwt, let us import the validateJwt and add one more route <code>/validate/:token</code></p>\n<p>Now you can verify any token by passing it to a route like - <code>http://localhost:8000/validate/jwt_token</code> (jwt_token is a placeholder, please replace it with a real JWT token)</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"ts\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">// index.ts</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">...</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> { </span><span class=\"mtk12\">validateJwt</span><span class=\"mtk1\"> } </span><span class=\"mtk15\">from</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;https://deno.land/x/djwt/validate.ts&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">...</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">router</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  .</span><span class=\"mtk11\">get</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/&quot;</span><span class=\"mtk1\">, (</span><span class=\"mtk12\">context</span><span class=\"mtk1\">) </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">body</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&quot;JWT Example!&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  })</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  .</span><span class=\"mtk11\">get</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/generate&quot;</span><span class=\"mtk1\">, (</span><span class=\"mtk12\">context</span><span class=\"mtk1\">) </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">body</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">makeJwt</span><span class=\"mtk1\">({ </span><span class=\"mtk12\">header</span><span class=\"mtk1\">, </span><span class=\"mtk12\">payload</span><span class=\"mtk1\">, </span><span class=\"mtk12\">key</span><span class=\"mtk1\"> }) + </span><span class=\"mtk8\">&quot;</span><span class=\"mtk6\">\\n</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  })</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  .</span><span class=\"mtk11\">get</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;/validate/:token&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk4\">async</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">context</span><span class=\"mtk1\">) </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> ( </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">params</span><span class=\"mtk1\"> && </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">params</span><span class=\"mtk1\">.</span><span class=\"mtk12\">token</span><span class=\"mtk1\"> && (</span><span class=\"mtk15\">await</span><span class=\"mtk1\"> </span><span class=\"mtk11\">validateJwt</span><span class=\"mtk1\">(</span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">params</span><span class=\"mtk1\">.</span><span class=\"mtk12\">token</span><span class=\"mtk1\">, </span><span class=\"mtk12\">key</span><span class=\"mtk1\">)).</span><span class=\"mtk12\">isValid</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">body</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&quot;Valid JWT</span><span class=\"mtk6\">\\n</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    } </span><span class=\"mtk15\">else</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      </span><span class=\"mtk12\">context</span><span class=\"mtk1\">.</span><span class=\"mtk12\">response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">body</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&quot;Invalid JWT</span><span class=\"mtk6\">\\n</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  });</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">...</span></span></code></pre>\n<p>Now you know how to generate and verify a JWT token in Deno, you can easily use it in your application, The complete source code used in this blog can be found in this <a href=\"https://github.com/LoginRadius/engineering-blog-samples/tree/master/Deno/JWTAuthentication\">Github Repo</a></p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk6 { color: #D7BA7D; }\n</style>","frontmatter":{"date":"July 10, 2020","updated_date":null,"description":null,"title":"How to create and validate JSON Web Tokens in Deno","tags":["Deno","JWT","JSON Web Token"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.492537313432836,"src":"/static/4f62fb01daec253b0246b5ba0f244846/ee604/deno_jwt.png","srcSet":"/static/4f62fb01daec253b0246b5ba0f244846/69585/deno_jwt.png 200w,\n/static/4f62fb01daec253b0246b5ba0f244846/497c6/deno_jwt.png 400w,\n/static/4f62fb01daec253b0246b5ba0f244846/ee604/deno_jwt.png 800w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Puneet Singh","github":"puneetsingh24","avatar":null}}}}]},"markdownRemark":{"excerpt":"Google has prepared a roadmap to restrict third-party cookies in Chrome. Since 04 January 2024, Chrome has rolled out third-party cookie…","fields":{"slug":"/engineering/identity-impact-of-google-chrome-thirdparty-cookie-restrictions/"},"html":"<p>Google has prepared a roadmap to restrict third-party cookies in Chrome. Since 04 January 2024, Chrome has rolled out third-party cookie restrictions for 1% of stable clients and 20% of Canary, Dev, and Beta clients.</p>\n<p><strong>What does it mean for user authentication?</strong></p>\n<p>On one hand, Google believes third-party cookies are widely used for cross-site tracking, greatly affecting user privacy. Hence, Google wants to phase out (or restrict) supporting third-party cookies in Chrome by early Q2 2025 (subject to regulatory processes).</p>\n<p>On the other hand, Google introduced Privacy Sandbox to support the use cases (other than cross-site tracking and advertising) previously implemented using third-party cookies.</p>\n<p>In this article, we’ll discuss:</p>\n<ul>\n<li>How is user authentication (identity) affected?</li>\n<li>What is Google offering as part of Privacy Sandbox to support various identity use cases when third-party cookies are phased out?</li>\n</ul>\n<h2 id=\"how-is-user-authentication-affected\" style=\"position:relative;\"><a href=\"#how-is-user-authentication-affected\" aria-label=\"how is user authentication affected permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How is User Authentication Affected?</h2>\n<p>Third-party cookie restrictions affect user authentication in three ways, as follows.</p>\n<h3 id=\"external-identity-providers\" style=\"position:relative;\"><a href=\"#external-identity-providers\" aria-label=\"external identity providers permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>External Identity Providers</h3>\n<p>If your website or app uses an external Identity Provider (IdP) — like LoginRadius, the IdP sets a third-party cookie when the user authenticates on your app.</p>\n<h3 id=\"web-sso\" style=\"position:relative;\"><a href=\"#web-sso\" aria-label=\"web sso permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Web SSO</h3>\n<p>If you have multiple apps across domains within your organization and authentication is handled using an IdP (internal or external) with web SSO, you already use third-party cookies to facilitate seamless access for each user using a single set of credentials.</p>\n<p>If you have implemented web SSO with one primary domain and multiple sub-domains of the primary domain, third-party cookie restrictions may not apply. For now, Google doesn’t consider the cookies set by sub-domains as third-party cookies, although this stance may change in the future.</p>\n<p>For example, you have apps at <code>example.com</code>, <code>travel.example.com</code>, <code>stay.example.com</code>, and web SSO is handled by <code>auth.example.com</code>. In this case, third-party cookie restrictions don’t apply.</p>\n<h3 id=\"federated-sso\" style=\"position:relative;\"><a href=\"#federated-sso\" aria-label=\"federated sso permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Federated SSO</h3>\n<p>Federated SSO is similar to, albeit different from, web SSO. It can handle multiple IdPs and applications—aka., Service Providers (SPs)—spanning multiple organizations. It can also implement authentication scenarios that are usually implemented through web SSO.</p>\n<p>Usually, authentication is handled on a separate pop-up or page when the user wants to authenticate rather than on the application or website a user visits. </p>\n<p>For example, you already use federated SSO if you facilitate authentication for a set of apps through multiple social identity providers as well as traditional usernames and passwords.</p>\n<blockquote>\n<p><strong>Note</strong>: It is also possible to store tokens locally, not within cookies. In this case, third-party cookie restrictions won’t affect token-based authentication. However, the restrictions still affect authentication where tokens are stored within third-party cookies (a common and secure method).</p>\n</blockquote>\n<h2 id=\"chromes-alternatives-for-third-party-cookies\" style=\"position:relative;\"><a href=\"#chromes-alternatives-for-third-party-cookies\" aria-label=\"chromes alternatives for third party cookies permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Chrome’s Alternatives for Third-Party Cookies</h2>\n<p>Google has been developing alternative features and capabilities for Chrome to replace third-party cookies as part of its Privacy Sandbox for Web initiative.</p>\n<p>Specific to authentication, Google recommends the following:</p>\n<ol>\n<li>Cookies Having Independent Partitioned State (CHIPS)</li>\n<li>Storage Access API</li>\n<li>Related Website Sets</li>\n<li>Federated Credential Management (FedCM) API</li>\n</ol>\n<h3 id=\"cookies-having-independent-partitioned-state-chips\" style=\"position:relative;\"><a href=\"#cookies-having-independent-partitioned-state-chips\" aria-label=\"cookies having independent partitioned state chips permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Cookies Having Independent Partitioned State (CHIPS)</h3>\n<p><a href=\"https://developers.google.com/privacy-sandbox/3pcd/chips\">CHIPS</a> are a restricted way of setting third-party cookies on a top-level site without making them accessible on other top-level sites. Thus, they limit cross-site tracking and enable specific cross-site functionalities, such as maps, chat, and payment embeds.</p>\n<p>For example, a user visits <code>a.com</code> with a map embed from <code>map-example.com</code>, which can set a partitioned cookie that is only accessible on a.com. </p>\n<p>If the user visits <code>b.com</code> with a map embed from <code>map-example.com</code>, it cannot access the partitioned cookie set on <code>a.com</code>. It has to create a separate partitioned cookie specific to <code>b.com</code>, thus blocking cross-site tracking yet allowing limited cross-site functionality.</p>\n<p>You should specifically opt for partitioned cookies (CHIPS), which are set with partitioned and secure cookie attributes.</p>\n<p>If you’re using an external identity provider for your application, CHIPS is a good option to supplant third-party cookie restrictions. </p>\n<p>However, CHIPS may not be ideal if you have a web SSO or federated SSO implementation. It creates separate partitioned cookies for each application with a separate domain, which can increase complexity and create compatibility issues.</p>\n<h3 id=\"storage-access-api\" style=\"position:relative;\"><a href=\"#storage-access-api\" aria-label=\"storage access api permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Storage Access API</h3>\n<p>With <a href=\"https://developers.google.com/privacy-sandbox/3pcd/storage-access-api\">Storage Access API</a>, you can access the local storage in a third-party context through iframes, similar to when users visit it as a top-level site in a first-party context. That is, it gives access to unpartitioned cookies and storage.</p>\n<p>Storage Access API requires explicit user approval to grant access, similar to locations, camera, and microphone permissions. If the user denies access, unpartitioned cookies and storage won’t be accessible in a third-party context.</p>\n<p>It is most suitable when loading cross-site resources and interactions, such as:</p>\n<p>Verifying user sessions when allowing interactions on an embedded social post or providing personalization for an embedded video.\nEmbedded documents requiring user verification status to be accessible.</p>\n<p>As it requires explicit user approval, it is advisable to use Storage Access API when you can’t implement an identity use case with the other options.</p>\n<h3 id=\"related-website-sets\" style=\"position:relative;\"><a href=\"#related-website-sets\" aria-label=\"related website sets permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Related Website Sets</h3>\n<p>With <a href=\"https://developers.google.com/privacy-sandbox/3pcd/related-website-sets\">Related Website Sets</a>, you can declare a <code>primary</code> website and <code>associatedSites</code> for limited purposes to grant third-party cookie access and local storage for a limited number of sites.</p>\n<p>Chrome automatically recognizes related website sets declared, accepted, and maintained in this open-source GitHub repository: <a href=\"https://github.com/GoogleChrome/related-website-sets\">Related Website Sets</a></p>\n<p>It provides access through Storage Access API directly without prompting for user approval, but only after the user interacts with the relevant iframe.</p>\n<p>It is important to declare a limited number of domains in related website sets that are meaningful and used for specific purposes. Google may block or suspend any exploitative use of this feature.</p>\n<p>The top-level site can also request approval for specific cross-site resources and scripts to Storage Access API using <code>resuestStorageAccessFor()</code> API.</p>\n<p>If you’re using an external identity provider for your web application, you can declare the domain of the identity provider in the related set to ensure limited third-party cookies and storage access to the identity provider, thus ensuring seamless user authentication.</p>\n<p>Related Website Sets can also work to supplement third-party cookie restrictions in web SSO and federated SSO if the number of web applications (or domains) is limited.</p>\n<h3 id=\"federated-credential-management-fedcm-api\" style=\"position:relative;\"><a href=\"#federated-credential-management-fedcm-api\" aria-label=\"federated credential management fedcm api permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Federated Credential Management (FedCM) API</h3>\n<p>FedCM API enables federated SSO without third-party cookies.</p>\n<p>With FedCM API, a user follows these steps for authentication:</p>\n<ol>\n<li>The User navigates to a Service Provider (SP) — aka., Relying Party (RP)</li>\n<li>As the user requests to authenticate, the SP requests the browser through FedCM API to initiate authentication.</li>\n<li>The browser displays a list of available identity providers (supported by the RP), such as social IdPs like Google, Apple, LinkedIn, and Facebook, or other OAuth IdPs like LoginRadius.</li>\n<li>Once the user selects an IdP, the browser communicates with the IdP. Upon valid authentication, the IdP generates a secure token.\nThe browser delivers this secure token to the RP to facilitate user authorization.</li>\n</ol>\n<p>You can access a user demo of FedCM here: <a href=\"https://fedcm-rp-demo.glitch.me/\">FedCM</a>. </p>\n<p>For more information about implementing federated SSO with FedCM API, go through the <a href=\"https://developers.google.com/privacy-sandbox/3pcd/fedcm-developer-guide\">FedCM developer guide</a>.</p>\n<h2 id=\"how-is-loginradius-preparing-for-the-third-party-cookie-phase-out\" style=\"position:relative;\"><a href=\"#how-is-loginradius-preparing-for-the-third-party-cookie-phase-out\" aria-label=\"how is loginradius preparing for the third party cookie phase out permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How is LoginRadius Preparing for the Third-party Cookie Phase-out?</h2>\n<p>Firstly, we’re committed to solving our customers' user identity pain points — and preparing for the third-party cookies phase-out is no different.</p>\n<p>We’ll implement the most relevant and widely useful solutions to facilitate a smooth transition for our customers.</p>\n<p>Please subscribe to our blog for more information. We’ll update you on how we help with the third-party cookie phase-out.</p>\n<h2 id=\"in-conclusion\" style=\"position:relative;\"><a href=\"#in-conclusion\" aria-label=\"in conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>In Conclusion</h2>\n<p>The proposed changes to phase out third-party cookies and suggested alternatives are evolving as Google has been actively collaborating and discussing changes with the border community.</p>\n<p>Moreover, browsers like Firefox, Safari, and Edge may approach restricting third-party cookies differently than Google does.</p>\n<p>From LoginRadius, we’ll keep you updated on what we’re doing as a leading Customer Identity and Access Management (CIAM) vendor to prepare for the third-party cookie phase-out.</p>\n<h2 id=\"glossary\" style=\"position:relative;\"><a href=\"#glossary\" aria-label=\"glossary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Glossary</h2>\n<p><strong>Top-level site</strong>: It is the primary site a user has visited.</p>\n<p><strong>First-party cookie</strong>: A cookie set by the top-level site.</p>\n<p><strong>Third-party cookie</strong>: A cookie set by a domain other than the top-level site. For example, let’s assume that a user has visited <code>a.com</code>, which might use an embed from <code>loginradius.com</code> to facilitate authentication. If <code>loginradius.com</code> sets a cookie when the user visits <code>a.com</code>, it is called a third-party cookie as the user hasn’t directly visited <code>loginradius.com</code>.</p>\n<h2 id=\"references\" style=\"position:relative;\"><a href=\"#references\" aria-label=\"references permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>References</h2>\n<ul>\n<li><a href=\"https://developers.google.com/privacy-sandbox/3pcd/prepare/prepare-for-phaseout\">Changes to Chrome's treatment of third-party cookies</a></li>\n<li><a href=\"https://developers.google.com/privacy-sandbox/3pcd/guides/identity\">Check the impact of the third-party cookie changes on your sign-in workflows</a></li>\n</ul>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n</style>","frontmatter":{"date":"July 08, 2024","updated_date":null,"description":"Google Chrome has planned to phase out third-party cookies, which will affect different website functionalities depending on third-party cookies. This blog focuses on how this phase-out affects identity and user authentication and discusses alternatives for overcoming challenges.","title":"How Chrome’s Third-Party Cookie Restrictions Affect User Authentication?","tags":["Identity","Cookies","Chrome"],"pinned":null,"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/eb7396060c0adc430dbed2d04b63d431/ee604/third-party-cookies-phaseout-chrome.png","srcSet":"/static/eb7396060c0adc430dbed2d04b63d431/69585/third-party-cookies-phaseout-chrome.png 200w,\n/static/eb7396060c0adc430dbed2d04b63d431/497c6/third-party-cookies-phaseout-chrome.png 400w,\n/static/eb7396060c0adc430dbed2d04b63d431/ee604/third-party-cookies-phaseout-chrome.png 800w,\n/static/eb7396060c0adc430dbed2d04b63d431/f3583/third-party-cookies-phaseout-chrome.png 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Raghunath Reddy","github":"raghunath-r-a","avatar":null}}}},"pageContext":{"limit":6,"skip":192,"currentPage":33,"type":"//engineering//","numPages":52,"pinned":"17fa0d7b-34c8-51c4-b047-df5e2bbaeedb"}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}