<?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: Sumit Chahar</title>
    <description>The latest articles on Forem by Sumit Chahar (@chaharsumit).</description>
    <link>https://forem.com/chaharsumit</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%2F691130%2F5d5e8ef2-cddd-4a8b-9c96-23a86e2f21bd.png</url>
      <title>Forem: Sumit Chahar</title>
      <link>https://forem.com/chaharsumit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/chaharsumit"/>
    <language>en</language>
    <item>
      <title>Serializing and deserializing our user data in passport.js</title>
      <dc:creator>Sumit Chahar</dc:creator>
      <pubDate>Wed, 28 Dec 2022 06:37:20 +0000</pubDate>
      <link>https://forem.com/chaharsumit/serializing-and-deserializing-our-user-data-in-passportjs-2fjd</link>
      <guid>https://forem.com/chaharsumit/serializing-and-deserializing-our-user-data-in-passportjs-2fjd</guid>
      <description>&lt;p&gt;As mentioned in the &lt;a href="https://dev.to/chaharsumit/storing-the-user-data-in-our-database-3e"&gt;previous article&lt;/a&gt; passport by default uses &lt;code&gt;serializeUser and deserializeUser methods&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SerializeUser method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is used to store user data in the session storage. It also decides which part of user object will be stored in the session.&lt;/p&gt;

&lt;p&gt;When we the auth process runs in passport.js before handling the success redirect it always looks for a serializer method to put the user information in the session.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User.create(profileData, (err, newUser) =&amp;gt; {
            if (err) {
              return done(err);
            }
            return done(null, newUser);
          });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the reason why it is recommended to pass a second truthy value to the &lt;code&gt;done&lt;/code&gt; callback of the strategy after we have created and stored the user in the database. The second argument of the done callback will be the user object and the serializeUser method will have access to it so we can save the user info by passing a key in the &lt;code&gt;done&lt;/code&gt; callback of serializeUser method. The key passed is useful in desearilizing the user as mentioned below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DeserializeUser method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We also have deserializeUser method which can be used to get access to the user data. It has two arguments with the first argument being the key that we passed to the &lt;code&gt;done&lt;/code&gt; method in our serializeUser method. We can access the entire user object by using only the key i.e., the first argument of the deserializeUser method.&lt;/p&gt;

&lt;p&gt;We will add both of these methods now to our &lt;code&gt;passport.js&lt;/code&gt; module file and it will look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm2vm8v9rlk00dnjt8y55.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm2vm8v9rlk00dnjt8y55.png" alt="Image description" width="800" height="879"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can disable the serializeUser method as well if don't want to use it by default. We just need to pass &lt;code&gt;sessions: false&lt;/code&gt; in our &lt;code&gt;index.js&lt;/code&gt; file &lt;code&gt;/auth/github/callback&lt;/code&gt; route.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router.get(
  "/auth/github/callback",
  passport.authenticate("github", { failureRedirect: "/failure", session: false }),
  (req, res) =&amp;gt; {
    res.redirect("/success");
  }
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it we have completed our Github OAuth application.&lt;/p&gt;

&lt;p&gt;Thanks for reading the article.&lt;/p&gt;

</description>
      <category>devjournal</category>
    </item>
    <item>
      <title>Storing the user data in our database</title>
      <dc:creator>Sumit Chahar</dc:creator>
      <pubDate>Wed, 28 Dec 2022 06:36:42 +0000</pubDate>
      <link>https://forem.com/chaharsumit/storing-the-user-data-in-our-database-3e</link>
      <guid>https://forem.com/chaharsumit/storing-the-user-data-in-our-database-3e</guid>
      <description>&lt;p&gt;Now that we have handled our routes and also written the strategy for OAuth in the &lt;a href="https://dev.to/chaharsumit/creating-module-for-our-github-oauth-strategy-1md5"&gt;previous article&lt;/a&gt;. We will now save the user information to our database. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; We are using mongoDB and mongoose for our application.&lt;/p&gt;

&lt;p&gt;We will create a &lt;code&gt;models folder&lt;/code&gt; in our project folder and create a &lt;code&gt;User.js&lt;/code&gt; file in which will be our User model and we will write the user schema in this file.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;User.js&lt;/code&gt; file will look like this - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foddahu8ts0qe9rofl8tq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foddahu8ts0qe9rofl8tq.png" alt="Image description" width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we will connect our local database with our application we will need to import &lt;code&gt;mongoose&lt;/code&gt; connect it using &lt;code&gt;mongoose.connect&lt;/code&gt; middleware&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongoose.connect("mongodb://localhost/sampleLogin", err =&amp;gt; {
  console.log(err ? err : "Database connected successfully");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also need to add a store to our session using instance of MongoStore in which we will pass the url of our local database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use(
  session({
    secret: process.env.SECRET,
    saveUninitialized: false,
    resave: false,
    store: new MongoStore({ mongoUrl: "mongodb://localhost/sampleLogin" })
  })
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;app.js&lt;/code&gt; file will appear like this now - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvflddpwfg134nr0fykj9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvflddpwfg134nr0fykj9.png" alt="Image description" width="800" height="1137"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We've also added passport.session() middleware in &lt;code&gt;app.js&lt;/code&gt; which can give us information about the current logged in user in the session.&lt;/p&gt;

&lt;p&gt;At last we should also import our User model in the &lt;code&gt;passport.js&lt;/code&gt; file. To create new user.&lt;/p&gt;

&lt;p&gt;Now by default passport uses serializeUser method to persist user data in the session. We'll use it in the next article to persist user information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/chaharsumit/serializing-and-deserializing-our-user-data-in-passportjs-2fjd"&gt;Go to next article 👉&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>kubernetes</category>
      <category>terraform</category>
      <category>devops</category>
    </item>
    <item>
      <title>Creating module for our Github OAuth strategy</title>
      <dc:creator>Sumit Chahar</dc:creator>
      <pubDate>Wed, 28 Dec 2022 06:35:50 +0000</pubDate>
      <link>https://forem.com/chaharsumit/creating-module-for-our-github-oauth-strategy-1md5</link>
      <guid>https://forem.com/chaharsumit/creating-module-for-our-github-oauth-strategy-1md5</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.to/chaharsumit/handling-routes-for-our-passport-application-223o"&gt;In the previous article&lt;/a&gt; we set up our routes in the index router file. Now we need to create the strategy to use for our Github OAuth application from passport.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a auth Strategy ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Strategies are a defined authentication mechanism which is responsible for handling auth requests. A strategy provides information on what basis of data we provide like &lt;code&gt;client_id&lt;/code&gt;, &lt;code&gt;client_secret&lt;/code&gt; to verify the application with these credentials.&lt;/p&gt;

&lt;p&gt;In our Github strategy we need to pass our &lt;code&gt;client_id, client_secret&lt;/code&gt; which are required to verify the application and if the application is registered with Github OAuth or not and only then the OAuth flow proceeds. We also pass a third param i.e., &lt;code&gt;callback_url&lt;/code&gt; to which we are redirected after the auth process has ended.&lt;/p&gt;

&lt;p&gt;The Github strategy will also return us a callback function with &lt;code&gt;accessToken, refreshToken, profile and data&lt;/code&gt;. We can perform any operation we want with the data we get from this callback.&lt;/p&gt;

&lt;p&gt;We can use the accessToken and refreshToken to authenticate the user once we have them stored in our local database in case we don't want our user to login by going to the Github consent screen every time they want to login.&lt;/p&gt;

&lt;p&gt;We will create a &lt;code&gt;modules folder&lt;/code&gt; in our project folder and create a file name for example &lt;code&gt;passport.js&lt;/code&gt; in the folder. The file will have the following structure.&lt;/p&gt;

&lt;p&gt;before we start writing our module file we will need to add it to &lt;code&gt;app.js&lt;/code&gt; and preferably before we are initialising our application. As we want to make sure that the first time when our application is initialised our strategy in the module is already stored in the memory.&lt;/p&gt;

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

&lt;p&gt;After importing it in &lt;code&gt;app.js&lt;/code&gt; we can proceed further.&lt;/p&gt;

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

&lt;p&gt;In the above file,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We are redirected to the callback url after the auth process is complete the &lt;code&gt;/auth/github/callback&lt;/code&gt; is already present in our index.js file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, we need to use the callback function that contains the user data and tokens in the Github strategy and create a user and store it in our local database.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://dev.to/chaharsumit/storing-the-user-data-in-our-database-3e"&gt;Go to next article 👉&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Handling Routes for our passport application</title>
      <dc:creator>Sumit Chahar</dc:creator>
      <pubDate>Wed, 28 Dec 2022 06:35:11 +0000</pubDate>
      <link>https://forem.com/chaharsumit/handling-routes-for-our-passport-application-223o</link>
      <guid>https://forem.com/chaharsumit/handling-routes-for-our-passport-application-223o</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.to/chaharsumit/register-oauth-application-on-github-5hm7"&gt;In the previous article&lt;/a&gt; we registered our application on Github and we've got our config variable. We need to setup our routes for passport authentication with Github.&lt;/p&gt;

&lt;p&gt;In our index.js file in the routes folder we will set up the routes to use OAuth.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now we need to configure our routes for handling the OAuth process&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our index.js file will look like this, &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fevvbxavcp0li7hmw2tcv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fevvbxavcp0li7hmw2tcv.png" alt="Index.js file" width="800" height="672"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;'/'&lt;/code&gt; the index route to render our index page where we'll have our login button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;'/success'&lt;/code&gt; to redirect user if auth is successful.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;'/failure'&lt;/code&gt; to redirect user to failure page if auth is unsuccessful or an error occurs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;'/auth/github'&lt;/code&gt; we'll make a &lt;code&gt;get&lt;/code&gt; request to &lt;code&gt;auth/github&lt;/code&gt; route and pass the middleware passport.authenticate passing &lt;code&gt;"github" as param&lt;/code&gt; to authenticate with Github strategy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;'/auth/github/callback'&lt;/code&gt; this route will work after the OAuth process is complete and we get either an error which means the failureRedirect option will trigger and we'll be redirect to the &lt;code&gt;/failure&lt;/code&gt; route. If auth is successful then we'll be redirected to the success page and the users profile data will be available to us.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;'/logout' for logging out of current session.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The auth/github route makes use of Github Strategy which is a defined protocol that has to be followed and there are different strategies for different OAuth provider for example, google OAuth strategy will be different in comparision to the Github OAuth Strategy.&lt;/p&gt;

&lt;p&gt;We'll set up our OAuth strategy in the next article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/chaharsumit/creating-module-for-our-github-oauth-strategy-1md5"&gt;Go to next article 👉&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Register OAuth Application on Github</title>
      <dc:creator>Sumit Chahar</dc:creator>
      <pubDate>Wed, 28 Dec 2022 06:34:30 +0000</pubDate>
      <link>https://forem.com/chaharsumit/register-oauth-application-on-github-5hm7</link>
      <guid>https://forem.com/chaharsumit/register-oauth-application-on-github-5hm7</guid>
      <description>&lt;p&gt;The first step in creating our Github OAuth application is registering our app on Github.&lt;/p&gt;

&lt;p&gt;You will need to follow the below steps - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;go to your Github account and navigate to /setting/developers.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QT6koMCc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dn7b5ytov7loi4oj75w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QT6koMCc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dn7b5ytov7loi4oj75w.png" alt="Image description" width="880" height="154"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose OAuth apps and create a New OAuth app.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In this step we have to give our application a name and a homepage URL I am using it on localhost so I've given a localhost URL. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We also need to provide an authorisation callback url here which is the url where the user is redirected to after Github authorise our application.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;After Registering the application we'll be redirected to our application settings and provided with the clientID and we need to manually generate the client secret. The clientID and client secret are to be stored in the &lt;code&gt;.env&lt;/code&gt; file as these should not be exposed anywhere being sensitive information.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;a href="https://dev.to/chaharsumit/handling-routes-for-our-passport-application-223o"&gt;Go to next Article 👉&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Creating an OAuth application with Express &amp; Passport in 5 simple steps</title>
      <dc:creator>Sumit Chahar</dc:creator>
      <pubDate>Wed, 28 Dec 2022 06:34:24 +0000</pubDate>
      <link>https://forem.com/chaharsumit/creating-an-oauth-application-with-express-passport-in-5-simple-steps-5dh2</link>
      <guid>https://forem.com/chaharsumit/creating-an-oauth-application-with-express-passport-in-5-simple-steps-5dh2</guid>
      <description>&lt;p&gt;In this series of articles we will be creating a basic Github OAuth application using Express.js. We'll also use Passport.js as our authentication middleware.&lt;/p&gt;

&lt;p&gt;We will be using mongoDB, mongoose for the database and also use session storage for this application.&lt;/p&gt;

&lt;p&gt;To setup our app we need to follow the following steps - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;install node, npm, mongoDB, express, express generator if not installed.&lt;/li&gt;
&lt;li&gt;create a project folder and in your project folder use the express generator to initialise our application.&lt;/li&gt;
&lt;li&gt;while creating the express application make sure we have installed the following dependencies these will be available in &lt;code&gt;package.json&lt;/code&gt; -&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;express&lt;/li&gt;
&lt;li&gt;passport&lt;/li&gt;
&lt;li&gt;passport-github&lt;/li&gt;
&lt;li&gt;express-session&lt;/li&gt;
&lt;li&gt;dotenv&lt;/li&gt;
&lt;li&gt;connect-mongo&lt;/li&gt;
&lt;li&gt;debug&lt;/li&gt;
&lt;li&gt;ejs (or any templating engine)&lt;/li&gt;
&lt;li&gt;mongoose&lt;/li&gt;
&lt;li&gt;http-errors&lt;/li&gt;
&lt;li&gt;morgan&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will setup our ejs templates first - &lt;/p&gt;

&lt;p&gt;In the views folder we will create our templates since this will be only a backend application. &lt;/p&gt;

&lt;p&gt;we will just add a link that points to our OAuth route for now, you can create these to your liking later. You can also create success, failure views, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fejy1oe3gb63yaid65nfp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fejy1oe3gb63yaid65nfp.png" alt="Image description" width="800" height="345"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The folder structure after we complete our app will look like this for reference - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuh2vr7y6tt3weq0t2s7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuh2vr7y6tt3weq0t2s7w.png" alt="Image description" width="742" height="1036"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating the application&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are 5 steps in which we will be creating our Github OAuth application -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Register Application.&lt;/li&gt;
&lt;li&gt;Handle Routes.&lt;/li&gt;
&lt;li&gt;Create Strategy for Github OAuth.&lt;/li&gt;
&lt;li&gt;Adding copy of user data in our local database.&lt;/li&gt;
&lt;li&gt;Serialize &amp;amp; deserialise.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://dev.to/chaharsumit/register-oauth-application-on-github-5hm7"&gt;Go to next article 👉 &lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
    </item>
    <item>
      <title>Introduction to OAuth and OAuth Flow</title>
      <dc:creator>Sumit Chahar</dc:creator>
      <pubDate>Tue, 27 Dec 2022 08:43:46 +0000</pubDate>
      <link>https://forem.com/chaharsumit/introduction-to-oauth-and-oauth-flow-2l8g</link>
      <guid>https://forem.com/chaharsumit/introduction-to-oauth-and-oauth-flow-2l8g</guid>
      <description>&lt;h2&gt;
  
  
  &lt;u&gt;What is OAuth&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;OAuth is a delegated authorisation protocol that allows users to share information between services without exposing their password.&lt;/p&gt;

&lt;p&gt;An example of OAuth can be a website or an application asking their users to register or login via some other applications like google, Github, etc.&lt;/p&gt;

&lt;p&gt;Main motive of using OAuth is to provide authorisation and not authenticating the users.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;OAuth Flow&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;In OAuth we deal with three interfaces -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Browser application&lt;/li&gt;
&lt;li&gt;Server&lt;/li&gt;
&lt;li&gt;OAuth Provider&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Below is how OAuth flow works we can take example of logging in with google,&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In our application or website in the browser we choose to sign in with google.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The login router passes the instruction to the OAuth middleware which is middleware performing operation like providing the correct strategy based on our OAuth service provider, granting tokens, attaching the user information with the current session, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When permission is granted from the consent screen by the user an access token is generated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The access token is passed to the authorisation server which provides user information if the access token passed is correct.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The response from the OAuth provider server is passed into our callback function and profile data can be extracted from it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After we have the profile data we can provide authorise to the user or store data in our local database, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;👏 Thanks For Reading the article !&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Function Hoisting In JavaScript</title>
      <dc:creator>Sumit Chahar</dc:creator>
      <pubDate>Sat, 10 Sep 2022 07:23:15 +0000</pubDate>
      <link>https://forem.com/chaharsumit/function-hoisting-in-javascript-1bj9</link>
      <guid>https://forem.com/chaharsumit/function-hoisting-in-javascript-1bj9</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/chaharsumit/variable-hoisting-in-javascript-1h9i"&gt;previous post&lt;/a&gt; we talked about variable hoisting. Now we are going to look at function hoisting in JavaScript.&lt;/p&gt;

&lt;p&gt;There are two main types of functions in JavaScript - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function Declarations&lt;/li&gt;
&lt;li&gt;Function Expressions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this post, We'll can understand function hoisting by looking at how function declarations and function expressions behave in the creation and execution phase.&lt;/p&gt;

&lt;p&gt;**Hoisting Function Declarations&lt;/p&gt;

&lt;p&gt;we'll analyse the below example to understand how function declarations are hoisted - &lt;/p&gt;

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

&lt;p&gt;The below happens in the above example -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the creation phase the javascript engine encounters the function keyboard and the default behaviour is that when a function declaration is encountered it's hoisted in the memory with its complete function body.&lt;/li&gt;
&lt;li&gt;When the execution phase starts and the add function is called the full function body is already available in the execution phase so this program outputs 3.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;**Hoisting Function Expressions&lt;/p&gt;

&lt;p&gt;The way function expressions are hoisted is similar to how variables get hoisted as a function expressions is simply assigning an anonymous function to a variable so that we can later execute that by referring to the variable name.&lt;/p&gt;

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

&lt;p&gt;We can conclude from above examples that hoisting the function expressions happens the same way as the variable hoisting.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the first example, we get an error &lt;code&gt;add is not a function&lt;/code&gt; as the variable add was hoisted in memory with a value of &lt;code&gt;undefined&lt;/code&gt; thus the Js Engine expects add to be undefined and not a function so it throws an error in case we access it before initialisation.&lt;/li&gt;
&lt;li&gt;For the second one i've replaced &lt;code&gt;var&lt;/code&gt; with &lt;code&gt;let&lt;/code&gt; and it gives an error again as let/const declarations are only given memory in the creation phase but not a value so it results in the error &lt;code&gt;cannot access 'add' before initialisation&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The third example does print the expected output as we are accessing the variable add only after its initialised in out program so the variable add gets initialised to the anonymous function and we get the output 3 on calling it.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Variable Hoisting in JavaScript</title>
      <dc:creator>Sumit Chahar</dc:creator>
      <pubDate>Sat, 03 Sep 2022 10:22:38 +0000</pubDate>
      <link>https://forem.com/chaharsumit/variable-hoisting-in-javascript-1h9i</link>
      <guid>https://forem.com/chaharsumit/variable-hoisting-in-javascript-1h9i</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hoisting is one of the most interesting and important topics in JavaScript to understand how different variables, function, classes declarations and definitions behave in a JavaScript program.&lt;br&gt;
For someone new to JavaScript with no prior experience hoisting can be a confusing as most languages don't handle variable and function declarations the way javascript does and it requires some knowledge of how the JavaScript Engine behaves during code execution to understand Hoisting. &lt;br&gt;
Hoisting along with Thread of Execution, Scope, Closures is also one of the most popular topics in web dev interviews for entry level jobs.&lt;/p&gt;

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

&lt;p&gt;To understand hoisting one should have some knowledge of thread of execution, Execution Context and what are creation phase &amp;amp; execution phase in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is hoisting?
&lt;/h2&gt;

&lt;p&gt;Hoisting is storing the variables declared in the code into the memory when the code execution is in the creation phase. Variables, functions, etc. are all stored in the memory to be available to use when the execution phase begins.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Hoisting moves all the variables and functions declarations to the top of their scope to make them available to use before they are even declared&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;we can understand the above statements by looking at the below examples -&lt;/p&gt;

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

&lt;p&gt;We'll go through all three of these and try to understand what's happening here. The knowledge of creation and execution phase will come in handy here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; &lt;strong&gt;In the first example the following explains why undefined is logged :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The javascript engine reaches the declaration &lt;code&gt;var helloWorld&lt;/code&gt; it hoists it into the memory and there's a default behaviour in which the &lt;code&gt;var&lt;/code&gt; declarations are hoisted i.e., they are hoisted by assigning them value of &lt;code&gt;undefined&lt;/code&gt; during the creation phase.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The line &lt;code&gt;console.log(helloWorld)&lt;/code&gt; being a function call doesn't get hoisted.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When we come in the execution phase the function call to log the value of helloWorld variable prints the value as undefined since it was hoisted as &lt;code&gt;undefined&lt;/code&gt; in the creation phase.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; &lt;strong&gt;For the second example the result is undefined too :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In creation phase first line being a function call is not hoisted. We reach the variable declaration &lt;code&gt;var helloWorld = 'Hello World'&lt;/code&gt; which is hoisted but now we come to an important point. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Initialisations are not hoisted during the creation phase only declarations are.&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As JS Engine hoists var declarations with a value of &lt;code&gt;undefined&lt;/code&gt; the variable will be hoisted with a value of undefined and not &lt;code&gt;Hello World!&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The reason why &lt;code&gt;console.log(helloWorld)&lt;/code&gt; prints undefined is that when we're in the execution phase and reach line one the initialization has not taken place yet for our helloWorld variable so we're reading it before its initialized meaning it's value is still &lt;code&gt;undefined&lt;/code&gt; as we log it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; &lt;strong&gt;The third example will make things more clear about how hoisting works and what happens during creation and execution phase :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the creation phase helloWorld gets hoisted with a value of undefined.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now in the execution phase the first &lt;code&gt;console.log&lt;/code&gt; will print &lt;code&gt;undefined&lt;/code&gt; as we've already seen in example 2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The execution reaches line 2 where we've initialized the value of &lt;code&gt;helloWorld to Hello World!&lt;/code&gt; so the value for our variable &lt;code&gt;helloWorld&lt;/code&gt; changes from &lt;code&gt;undefined to Hello World!&lt;/code&gt; here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The line prints the message &lt;code&gt;Hello World&lt;/code&gt; because we've initialized the value of our variable before the last &lt;code&gt;console.log&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;From Above examples we've made the below observations :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variables are available to use before initialising/assignment of a value in the execution phase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;initializations are not hoisted during creation phase&lt;/strong&gt; so only the default value of &lt;code&gt;undefined&lt;/code&gt; got assigned to our variable even though we initialized it in &lt;strong&gt;example 3&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In the execution phase a variable will only be assigned a value when it reaches the part of code where its being initialized and not before that.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How Hoisting is different for Var, let &amp;amp; Const ?
&lt;/h2&gt;

&lt;p&gt;The difference between hoisting of a &lt;strong&gt;var declarations&lt;/strong&gt; with those of &lt;strong&gt;let and const declarations&lt;/strong&gt; is that var is hoisted in memory with an actual value i.e., &lt;strong&gt;undefined&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;let declarations&lt;/strong&gt; : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These are allocated memory during the creation phase but not initialized with any value, keeping them in memory as empty boxes that contain no value before they've been initialized in the excecution phase.&lt;/li&gt;
&lt;li&gt;In the execution phase when a let variable is read before its initialization then the variable will be assigned a value of &lt;code&gt;undefined&lt;/code&gt; as we can see from the result of first &lt;code&gt;console.log&lt;/code&gt; in example #2 in the below image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;const declarations&lt;/strong&gt; : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These declarations should always be initialized before they are used as they never get assigned a value of undefined like let declarations are assigned in the execution phase making them always throw an error if not assigned a value while declaring them. &lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;That's it for this post 👏🏻.&lt;br&gt;
Thanks for reading this post. Please post feedback, ask questions if any related to this topic. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>HTML Page Structure &amp; Semantics</title>
      <dc:creator>Sumit Chahar</dc:creator>
      <pubDate>Fri, 27 Aug 2021 11:37:33 +0000</pubDate>
      <link>https://forem.com/chaharsumit/html-page-strucuture-semantics-46k3</link>
      <guid>https://forem.com/chaharsumit/html-page-strucuture-semantics-46k3</guid>
      <description>&lt;h4&gt;
  
  
  Hey coders
&lt;/h4&gt;

&lt;p&gt;We come across different website designs every day and almost every one of them has a different structure which is defined by positioning HTML's grouping elements in different ways.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML Page Structure
&lt;/h2&gt;

&lt;p&gt;You can see The most basic structure that we come across on the web in this image.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ysa18v4r1mu3evmtiui.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ysa18v4r1mu3evmtiui.png" alt="HTML Page Structure"&gt;&lt;/a&gt;&lt;br&gt;
The elements in the given picture are the thematic grouping elements or we can say semantic elements that we use to structure our page. Let's go through these one by one -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;header&lt;/strong&gt; - This element is used for the introductory purpose we usually contain our logo, nav menu, etc. inside this element.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;main&lt;/strong&gt; - The main element is used to hold all the meaningful content of the page that we want our visitors to go through i.e, all the section, article, aside tags come inside it.
&lt;strong&gt;NOTE:&lt;/strong&gt; There should only be one &lt;strong&gt;main&lt;/strong&gt; element in an HTML document.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;section&lt;/strong&gt; - Section is used to group associated content such as different articles, information cards, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;article&lt;/strong&gt; - We use an article when there's a piece of content on our page that is independent of other content on our web page. for example, a blog post or a news story, etc. The article tag is contained in the section tag.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;aside&lt;/strong&gt; - aside is the element used to contain the sidebars on our page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;footer&lt;/strong&gt; - Footer is used for containing content like copyright information, site links, newsletter forms, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;One thing to note here is that the footer and header can be kept inside the main element also but one could argue that these should be outside the main as they usually don't hold content that is directly related to the central topic of our document. So we should try to use them only as a descendent of section, article or any other element while using them inside the main element. Whereas, the header and footer of the whole website should be kept outside the main element.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  HTML Semantics
&lt;/h2&gt;

&lt;p&gt;Now, you must be thinking we can use a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; rather than using any of these elements to make any possible style,and it's correct too but this is also where the necessity of using semantics and semantic elements come into play.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is semantics?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Semantics in terms of HTML simply means describing the meaning of our given content to the browser and ourselves(the developers). The generic elements we discussed above do just that. For example, using a &lt;code&gt;&amp;lt;table&amp;gt;&lt;/code&gt; tag to define a table for our page instead of a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;article&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;section&amp;gt;&lt;/code&gt; provides better semantics as it clearly defines that there's a table here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why not use &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; instead&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So we know the meaning of semantics now and how they provide meaning to our content let's come back to the question of why can't we use &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;, etc. for these purposes. The answer is simple the sole purpose of a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; is for grouping and styling our content but not to provide semantic meaning to our page. &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; does not provide any meaningful information regarding the type of content to the browser or the developer himself.&lt;br&gt;
So, &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; has its use case and that is the grouping and then styling the content.&lt;/p&gt;

&lt;h4&gt;
  
  
  Benefits of using Semantic tags
&lt;/h4&gt;

&lt;p&gt;These are some key benefits of semantic tags that you should know in case you are about to avoid using them&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clear indication of the role played by our content&lt;/li&gt;
&lt;li&gt;Better readability of code&lt;/li&gt;
&lt;li&gt;Offer SEO benefits&lt;/li&gt;
&lt;li&gt;Better consistency with the new HTML5 specifications&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Semantic tags are for containing the content so that it provides some meaningful information about itself and you should try to avoid using them for styling purposes whereas, &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; is for styling the content that we have inside our semantic elements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading the article ✌️. &lt;/p&gt;

</description>
      <category>html</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>A brief introduction to HTML &amp; CSS</title>
      <dc:creator>Sumit Chahar</dc:creator>
      <pubDate>Sun, 22 Aug 2021 07:46:47 +0000</pubDate>
      <link>https://forem.com/chaharsumit/a-brief-introduction-to-html-css-50p5</link>
      <guid>https://forem.com/chaharsumit/a-brief-introduction-to-html-css-50p5</guid>
      <description>&lt;h1&gt;
  
  
  What are HTML &amp;amp; CSS
&lt;/h1&gt;

&lt;p&gt;HTML &amp;amp; CSS are languages made for the web. HTML &amp;amp; CSS are like those best friends that can't stay without each other for one to exist other's existence is important.&lt;/p&gt;

&lt;p&gt;At the same time, One thing to note is that you may have HTML without CSS but it will only come to life when you actually style it with some CSS.&lt;/p&gt;

&lt;p&gt;Now, let's see what HTML and CSS are and how they work.#HTML&lt;/p&gt;

&lt;p&gt;HTML provides the markup for all the raw content that we want to display on our page.&lt;br&gt;
In an HTML document, you can just write anything inside the document and it will display on the page but every HTML page should have a standard document structure that we must follow to make sure the browser renders our code properly and gives meaning to our content.&lt;/p&gt;

&lt;p&gt;Given below is the most basic structure of an html document.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffen0va5qoq81r10c8n9l.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffen0va5qoq81r10c8n9l.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
There are different HTML elements and tags that define the structure of our page let's go through all of them&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/code&gt;&lt;/strong&gt; - This tells the browser what kind of document to expect as in our case here it is an html document.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;&lt;/strong&gt; - This tag represents the root of our document all the html elements are contained inside this.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;&lt;/strong&gt; - It holds information about the metadata, properties of our web page and links to external files that we are using on our page. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;Body&amp;gt;&lt;/code&gt;&lt;/strong&gt; - This element contains all the raw content which we want to display on our web page. Everything we see on a web page is contained inside the body tag.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  CSS
&lt;/h1&gt;

&lt;p&gt;CSS is the style sheet language which used for formatting and styling our HTML document. We use it to beautify our page.&lt;br&gt;
For CSS to work we must have an HTML document because we need a web page to make sure that our CSS styles show up for this we need to embed or link our CSS with the HTML document.&lt;br&gt;
There are three ways by which we add CSS to our HTML document -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Inline CSS&lt;/strong&gt; - Inline CSS is used to apply styles to an element by using the &lt;code&gt;style&lt;/code&gt; property writing it inside the element's opening tag. It is meant to be used when we want to apply some unique styles for a single element.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Internal or Embedded CSS&lt;/strong&gt; - Embedded CSS is written inside the &lt;code&gt;&amp;lt;Head&amp;gt;&lt;/code&gt; element which is where we store all the page related information. We simply open a &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tag inside the head and write all our styles inside it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;External Stylesheet&lt;/strong&gt; - In this method you simply create a '.css' file and link it with your HTML document providing file path inside the &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; tag of &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; element.&lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7qequ0xdcdsjwp7bemd.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7qequ0xdcdsjwp7bemd.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Out of the above three methods inline and embedded/internal CSS are not recommended as they don't separate content from design and are difficult to maintain. Thus it is recommended that you use an external stylesheet for your CSS styles which keeps all your styles at one place making sure you don't need to go through the whole HTML document to debug a small chunk of code.&lt;/p&gt;

&lt;p&gt;We may say that -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;HTML is what we want to say and CSS is how we want to say it&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>html</category>
      <category>css</category>
    </item>
  </channel>
</rss>
