<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Swapnil Sagar</title>
    <description>The latest articles on Forem by Swapnil Sagar (@swapnil09).</description>
    <link>https://forem.com/swapnil09</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F503650%2F81b2eb43-c891-4fba-8d8d-6b56e14cbac3.jpeg</url>
      <title>Forem: Swapnil Sagar</title>
      <link>https://forem.com/swapnil09</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/swapnil09"/>
    <language>en</language>
    <item>
      <title>JWT Access Tokens &amp; Refresh Tokens</title>
      <dc:creator>Swapnil Sagar</dc:creator>
      <pubDate>Tue, 16 May 2023 12:17:37 +0000</pubDate>
      <link>https://forem.com/swapnil09/jwt-access-tokens-refresh-tokens-1c4o</link>
      <guid>https://forem.com/swapnil09/jwt-access-tokens-refresh-tokens-1c4o</guid>
      <description>&lt;p&gt;&lt;strong&gt;Access tokens&lt;/strong&gt; and &lt;strong&gt;refresh tokens&lt;/strong&gt; are integral components of the JSON Web Token (JWT) authentication mechanism. Let's break down their concepts:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access Token&lt;/strong&gt;:&lt;br&gt;
An access token is a digitally signed and encoded token that is issued by an authentication server to a client application after successful authentication. It represents the authorization granted to the client to access protected resources or perform specific actions on behalf of an authenticated user. The access token contains claims such as user identification, expiration time, and scope.&lt;br&gt;
Once a client receives an access token, it includes it in the Authorization header or sends it as a parameter in API requests to the server. The server then verifies the token's authenticity and checks its claims to ensure the requested action is permitted for the authenticated user.&lt;/p&gt;

&lt;p&gt;Access tokens typically have a relatively short expiration time, such as a few minutes or hours, to ensure security. When an access token expires, the client needs to obtain a new one to continue accessing protected resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Refresh Token&lt;/strong&gt;:&lt;br&gt;
A refresh token is a long-lived credential that is also issued alongside the access token during the authentication process. While the access token provides temporary access, the refresh token serves as a means to obtain new access tokens without re-authenticating the user.&lt;br&gt;
When the access token expires, the client can send the refresh token to a token endpoint or authorization server. The server verifies the refresh token's validity, and if it is valid, issues a new access token. The client replaces the expired access token with the new one and continues accessing resources seamlessly.&lt;/p&gt;

&lt;p&gt;Refresh tokens have a longer expiration time compared to access tokens, which can range from days to months. They are typically securely stored on the client-side and transmitted over secure channels to prevent unauthorized access.&lt;/p&gt;

&lt;p&gt;The use of refresh tokens enhances security by minimizing the time an access token is active, reducing the risk of token interception and misuse. It also provides a smoother user experience since the user doesn't need to re-enter their credentials frequently.&lt;/p&gt;

&lt;p&gt;It's important to note that the implementation details of access and refresh tokens can vary depending on the authentication framework or service being used, but the core concepts remain consistent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import jwt from 'jsonwebtoken';

// Function to generate an access token
function generateAccessToken(userId) {
  const payload = {
    userId: userId,
    exp: Math.floor(Date.now() / 1000) + 15 * 60, // Token expires in 15 minutes
  };
  const accessToken = jwt.sign(payload, 'your-secret-key');
  return accessToken;
}

// Function to generate a refresh token
function generateRefreshToken(userId) {
  const payload = {
    userId: userId,
    exp: Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60, // Token expires in 30 days
  };
  const refreshToken = jwt.sign(payload, 'your-secret-key');
  return refreshToken;
}

// Example usage
const userId = 123;

const accessToken = generateAccessToken(userId);
console.log('Access Token:', accessToken);

const refreshToken = generateRefreshToken(userId);
console.log('Refresh Token:', refreshToken);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>tokens</category>
    </item>
    <item>
      <title>PWA / SPA / AMP</title>
      <dc:creator>Swapnil Sagar</dc:creator>
      <pubDate>Sat, 28 Nov 2020 05:23:03 +0000</pubDate>
      <link>https://forem.com/swapnil09/pwa-spa-amp-55pk</link>
      <guid>https://forem.com/swapnil09/pwa-spa-amp-55pk</guid>
      <description>&lt;p&gt;If you are into Web Development in 2020 and working in latest technologies like React, Gatsby, Etc. then you must be familiar with SPA, PWA and AMP. These are some new abbreviations which every dev should be familiar with. These technologies gave a step-up to the community in terms of Web functionalities. Lets have a look in them one-by-one.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;PWA&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;PWA&lt;/strong&gt;&lt;/em&gt; stands for PROGRESSIVE WEB APP. This is an app built from the web technologies we all know and love, like HTML, CSS, and JavaScript, but with a feel and functionality that rivals an actual native app. Thanks to a couple of smart additions, you can turn almost any website into a progressive web app. This means that you can build a PWA rather quickly, in regards to a native app that’s pretty difficult to develop. Plus, you can offer all the features of native apps, like push notifications, offline support, and much more.&lt;/p&gt;

&lt;p&gt;Progressive Web Apps are web apps that use emerging web browser APIs and features along with traditional progressive enhancement strategy to bring a native app-like user experience to cross-platform web applications.&lt;/p&gt;

&lt;p&gt;Many sites you find online are actually a progressive web app. Take twitter.com, for instance. If you visit that site on your smartphone, you can install it to your home screen. Now, on opening the saved Twitter site, you’ll notice that it looks and performs just like a native app. There’s no browser window or nothing. There’s no difference in running it from an iPhone or an Android smartphone. Simply log in and you’re good to go. That’s a major benefit of building your web app with a PWA in mind.&lt;/p&gt;

&lt;p&gt;
  &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FswQ5x2J%2Fpwa-app.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FswQ5x2J%2Fpwa-app.jpg"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;In order to call a Web App a PWA, technically speaking it should have the following features: &lt;strong&gt;Secure contexts (HTTPS), one or more Service Workers, and a manifest file&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;-&amp;gt; Secure contexts (HTTPS)&lt;br&gt;
The web application must be served over a secure network. Being a secure site is not only a best practice, but it also establishes your web application as a trusted site especially if users need to make secure transactions. Most of the features related to a PWA such as geolocation and even service workers are available only once the app has been loaded using HTTPS.&lt;/p&gt;

&lt;p&gt;-&amp;gt; Service workers&lt;br&gt;
A service worker is a script that allows intercepting and control of how a web browser handles its network requests and asset caching. With service workers, web developers can create reliably fast web pages and offline experiences.&lt;br&gt;
Manifest file&lt;/p&gt;

&lt;p&gt;-&amp;gt; A JSON file that controls how your app appears to the user and ensures that progressive web apps are discoverable. It describes the name of the app, the start URL, icons, and all of the other details necessary to transform the website into an app-like format.&lt;/p&gt;

&lt;p&gt;You can check PWA availablity on a site by accessing &lt;strong&gt;lighout&lt;/strong&gt; through inspect element.&lt;/p&gt;

&lt;p&gt;For Example:&lt;/p&gt;

&lt;p&gt;
  &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FyWVptWc%2Fpwa-web.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FyWVptWc%2Fpwa-web.jpg"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;em&gt;PWAs are gaining popularity. Many big sites are PWAs, like Starbucks.com, Pinterest.com, Washingtonpost.com and Uber.com are actually installable on your home screen and offer a comparable experience to their native apps.&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;SPA&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;single-page application (SPA)&lt;/strong&gt; is a web application or website that interacts with the user by dynamically rewriting the current web page with new data from the web server, instead of the default method of the browser loading entire new pages. The goal is faster transitions that make the website feel more like a native app.&lt;/p&gt;

&lt;p&gt;A (SPA) is a web app or site that fits into one single page. SPA provides a more flexible, desktop-like user-experience and gives better performance.&lt;/p&gt;

&lt;p&gt;It allows the development of a smoother and more controlled UX, hiding web complexity (requests, responses, etc.) SPA swaps only the content that is requested by the user, loading HTML data in chunks.&lt;/p&gt;

&lt;p&gt;There are two types of SPA: one embeds the content of multiple pages into one ‘shell’ page; with the other, one single app uses Ajax to retrieve content whenever a user clicks a link, so only the content that changes will be updated on the page, while the rest of the page remains with no reloads.&lt;/p&gt;

&lt;p&gt;
  &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.aalpha.net%2Fwp-content%2Fuploads%2F2019%2F11%2Fsingle-page-app-india.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.aalpha.net%2Fwp-content%2Fuploads%2F2019%2F11%2Fsingle-page-app-india.gif"&gt;&lt;/a&gt;
&lt;/p&gt;




&lt;h4&gt;
  
  
  Why You Need a Single Page Web App
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;It’s perfect if you have a large-scale app with a complex user interface and hundreds of content pages, and you need to satisfy a large and diverse audience (thousands of users) that uses multiple devices&lt;/li&gt;
&lt;/ol&gt;

&lt;ol&gt;
&lt;li&gt;If you need a super rapid site with dynamic data loading and improved UX&lt;/li&gt;
&lt;/ol&gt;




&lt;h4&gt;
  
  
  Advantages of Single Page Apps
&lt;/h4&gt;

&lt;p&gt;➤ Faster and snappier user experience&lt;/p&gt;

&lt;p&gt;➤ Decrease in page-load time&lt;/p&gt;

&lt;p&gt;➤ No reload or execution with each new page request&lt;/p&gt;

&lt;p&gt;➤ Dynamic loading of page or part of page’s content&lt;/p&gt;

&lt;p&gt;➤ Simpler interaction for end-user with a single page&lt;/p&gt;




&lt;h4&gt;
  
  
  WHEN TO USE A SPA
&lt;/h4&gt;

&lt;p&gt;You might be asking, “When should I use a single page application?” When you have a business or a personal website that needs a dynamic platform and small data volume, the single-page application is a good idea. It is also a great option if you are planning to develop a mobile app in the future, since, as we mentioned above, the backend API can be used for both the web and mobile apps.&lt;/p&gt;

&lt;p&gt;The main drawback is the SEO, but the architecture is suitable for Software-as-a-Service (SaaS) platforms, closed communities, and social networks (which is why Facebook uses it). The reason for this is that these sites don’t need optimization for search on Google.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;AMP&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;AMP&lt;/strong&gt;&lt;/em&gt; (originally an acronym for &lt;strong&gt;Accelerated Mobile Pages&lt;/strong&gt;) is an open source HTML framework developed by the AMP Open Source Project It was originally created by Google as a competitor to Facebook Instant Articles and Apple News.&lt;/p&gt;

&lt;p&gt;AMP provides a straightforward way to create web pages that are fast, smooth-loading and prioritize the user-experience above all else.&lt;br&gt;
AMP is a simple and robust format to ensure your website is fast, user-first, and makes money. AMP provides long-term success for your web strategy with distribution across popular platforms and reduced operating and development costs.&lt;/p&gt;


&lt;h4&gt;
  
  
  In Simpler Words
&lt;/h4&gt;

&lt;p&gt;Technically speaking, AMP removes cumbersome HTML, CSS and JavaScript elements, resulting in a stripped-down page that only includes the most vital content (text, images, videos, and of course, site ads). This makes AMP lightning speed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To experience AMP Website yourself &lt;a href="https://amp-article.herokuapp.com/" rel="noopener noreferrer"&gt;Click Here&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;h5&gt;
  
  
  &lt;strong&gt;What makes AMP pages load so quickly?&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;Google has a strict set of optimisations that significantly improve page loading times:&lt;/p&gt;

&lt;p&gt;➥ Allow only asynchronous scripts&lt;/p&gt;

&lt;p&gt;➥ Size all resources statically&lt;/p&gt;

&lt;p&gt;➥ Don’t let extension mechanisms block rendering&lt;/p&gt;

&lt;p&gt;➥ Keep all third-party JavaScript out of the critical path&lt;/p&gt;

&lt;p&gt;➥ All CSS must be inline and size-bound&lt;/p&gt;

&lt;p&gt;➥ Font triggering must be efficient&lt;/p&gt;

&lt;p&gt;➥ Minimise style recalculations&lt;/p&gt;

&lt;p&gt;➥ Only run GPU-accelerated animations&lt;/p&gt;

&lt;p&gt;➥ Prioritise resource loading&lt;/p&gt;

&lt;p&gt;➥ Load pages in an instant&lt;/p&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!doctype html&amp;gt;
&amp;lt;html amp lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8"&amp;gt;
    &amp;lt;script async src="https://cdn.ampproject.org/v0.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;title&amp;gt;Hello, AMPs&amp;lt;/title&amp;gt;
    &amp;lt;link rel="canonical" href="https://amp.dev/documentation/guides-and-tutorials/start/create/basic_markup/"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"&amp;gt;
    &amp;lt;script type="application/ld+json"&amp;gt;
      {
        "@context": "http://schema.org",
        "@type": "NewsArticle",
        "headline": "Open-source framework for publishing content",
        "datePublished": "2015-10-07T12:02:41Z",
        "image": [
          "logo.jpg"
        ]
      }
    &amp;lt;/script&amp;gt;
    &amp;lt;style amp-boilerplate&amp;gt;body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}&amp;lt;/style&amp;gt;&amp;lt;noscript&amp;gt;&amp;lt;style amp-boilerplate&amp;gt;body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}&amp;lt;/style&amp;gt;&amp;lt;/noscript&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Welcome to the mobile web&amp;lt;/h1&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Read the official AMP Documentation &lt;a href="https://amp.dev/documentation/?format=websites" rel="noopener noreferrer"&gt;HERE&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>ux</category>
    </item>
    <item>
      <title>DATA STRUCTURE AND WEB DEVELOPMENT</title>
      <dc:creator>Swapnil Sagar</dc:creator>
      <pubDate>Sun, 01 Nov 2020 10:58:35 +0000</pubDate>
      <link>https://forem.com/swapnil09/data-structure-and-web-development-agg</link>
      <guid>https://forem.com/swapnil09/data-structure-and-web-development-agg</guid>
      <description>&lt;p&gt;As a CS student and also a web development enthusiast I can say that there are two parts to this answer one pertains to what you have to know and the second what you should know. Both were asked and I see those as quite different. The former being the minimum to get by and the later being what sets you up for future success.&lt;/p&gt;

&lt;p&gt;First of all, it's important to define "Web Developer" to understand the have to know. If we are talking about a front-end engineer, I see the following common languages to be required:&lt;/p&gt;

&lt;p&gt;HTML&lt;br&gt;
CSS&lt;br&gt;
JavaScript (including a couple frameworks)&lt;br&gt;
WHERE DOES DATA STRUCTURE COMES IN THE ROLE FOR WEB DEVELOPMENT?&lt;br&gt;
For HTML and CSS, there is a minimal amount of data structure and algorithmic knowledge required or actually none.&lt;/p&gt;

&lt;p&gt;But in order to write effective JavaScript or even use common JS frameworks it's very important to have a good understanding of:&lt;/p&gt;

&lt;p&gt;-&amp;gt; Data structures: at least hashmap (every JS object is really just a hashmap), arrays/lists.&lt;/p&gt;

&lt;p&gt;-&amp;gt; Common algorithms for sorting/searching and their tradeoffs: this is important for writing efficient JS. Say if you have a large data set in memory, whats the best way of displaying it to the user is sorted order? You may not always need to write this yourself, but it's very important to understand how it works.&lt;/p&gt;

&lt;p&gt;-&amp;gt; Memory use and efficiency: you may not need to know exactly how memory is allocated in the browser, but you should definitely know the most efficient ways to store data in memory and how that may trade off with performance.&lt;/p&gt;

&lt;p&gt;For any web development&lt;/p&gt;

&lt;p&gt;HTTP/Network communication - this is really important for debugging issues (things like Chrome's inspect element). Know how each tab works and what it's for.&lt;br&gt;
BUT....WHAT YOU SHOULD KNOW?&lt;br&gt;
Apart from our beloved array what other Data structurs should be known :&lt;/p&gt;

&lt;p&gt;Maps/Dictionaries are useful when you want to store and access items in a key-value way. Some people try to achieve this in an inefficient and cumbersome way with arrays and indexOf. JavaScript objects behave like dictionaries and you should use them instead.&lt;br&gt;
Trees are used to implement the DOM structure, knowing how trees work will help you structure and navigate in your DOM. Such knowledge is also fundamental to have something like Facebook's React diff algorithm that minimizes the changes in the DOM tree to improve rendering performance. And in case you also happen to write server-side code, it will help you understand how database indices work and why they are important.&lt;br&gt;
If you want to have quick access to the maximum or minimum value in a collection, like a priority queue for example, you should probably be using a Heap.&lt;br&gt;
Talking about Queues, they actually model the way events are processed.&lt;br&gt;
And Stacks will probably make it easier for you to understand how to handle the states in your single page app. It will also help you grasp recursion.&lt;/p&gt;

&lt;p&gt;If you start working on the front-end of web applications or web sites as a front-end engineer, you will inevitably run into issues or interactions with backend systems. In these cases it's very advantageous to be able to know a thing or two about the backend systems, how parts (or all!) of it works and the data structures and algorithmic techniques used.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
