<?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: SHAHAB MALIK</title>
    <description>The latest articles on Forem by SHAHAB MALIK (@shahabmalikk).</description>
    <link>https://forem.com/shahabmalikk</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%2F1065507%2F368eba92-7d4b-45d4-aaef-20bb1cfefb0d.jpeg</url>
      <title>Forem: SHAHAB MALIK</title>
      <link>https://forem.com/shahabmalikk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shahabmalikk"/>
    <language>en</language>
    <item>
      <title>Understanding the JavaScript Event Loop 🚀</title>
      <dc:creator>SHAHAB MALIK</dc:creator>
      <pubDate>Sun, 09 Jun 2024 09:12:41 +0000</pubDate>
      <link>https://forem.com/shahabmalikk/understanding-the-javascript-event-loop-44ij</link>
      <guid>https://forem.com/shahabmalikk/understanding-the-javascript-event-loop-44ij</guid>
      <description>&lt;p&gt;Event Loop is another important concept in JavaScript that often comes up in technical interviews&lt;/p&gt;

&lt;p&gt;JavaScript is single-threaded, meaning it can only perform one task at a time. This might make you wonder how JavaScript handles asynchronous operations like fetching data from an API, setTimeout, or event listeners. The magic lies in the Event Loop!&lt;/p&gt;

&lt;p&gt;Let's delve a bit deeper into the components and behavior of the event loop&lt;/p&gt;

&lt;h2&gt;
  
  
  How the Event Loop Works
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Call Stack :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The call stack is where JavaScript keeps track of function execution. When a function is called, it gets pushed onto the stack. When the function completes, it is popped off the stack.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Web APIs :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Certain operations, like asynchronous network requests (XHR, fetch), timers (setTimeout, setInterval), and DOM events, are handled by the browser's Web APIs. When such an operation completes, the Web API pushes a callback function to the task queue.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Task Queue (Callback Queue) : &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The task queue holds the callback functions that are ready to be executed. This includes callbacks from Web APIs and promises that have been resolved.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Event Loop :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The event loop constantly monitors the call stack and the task queue. If the call stack is empty, the event loop pushes the first task from the task queue onto the call stack, allowing it to be executed. This process ensures that tasks are executed in a non-blocking manner, allowing the JavaScript engine to perform other operations while waiting for asynchronous operations to complete.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example of Event Loop
&lt;/h2&gt;

&lt;p&gt;Let's understand Event Loop with help of an Example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('Start');

setTimeout(() =&amp;gt; {
    console.log('Timeout callback');
}, 0);

Promise.resolve().then(() =&amp;gt; {
    console.log('Promise callback');
});

console.log('End');

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Execution Steps:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Call Stack :&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;console.log('Start') is called and logs "Start".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;setTimeout is called, which sets up a timer in the Web API and registers a callback to be executed after 0 milliseconds.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Promise.resolve().then is called, which registers a callback to be executed when the promise resolves.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;console.log('End') is called and logs "End".&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Web API :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The timer for setTimeout completes almost immediately (after 0 milliseconds) and its callback is pushed to the task queue.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Microtask Queue :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The "then" callback from the promise is pushed to the microtask queue.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Event Loop:&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The call stack is empty after executing the synchronous code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The event loop first processes the microtask queue before the task queue. Therefore, the promise callback is executed and logs "Promise callback".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, the event loop processes the task queue. The setTimeout callback is executed and logs "Timeout callback".&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
Promise callback
Timeout callback

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Points
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Microtask Queue: This queue includes tasks such as resolved promise callbacks and process.nextTick in Node.js. It has higher priority than the task queue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Task Queue: This queue includes tasks such as setTimeout, setInterval, and other callback functions. It is processed after the microtask queue.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;The event loop enables JavaScript to handle asynchronous operations in a non-blocking manner, ensuring smooth execution of code without interruptions. Understanding the event loop is crucial for writing efficient and effective JavaScript code, especially when dealing with asynchronous operations.&lt;/p&gt;

&lt;p&gt;So, next time you're working with JavaScript, remember the Event Loop and its role in asynchronous operations!&lt;/p&gt;

&lt;p&gt;Want to learn about Web Development, Follow &lt;a href="https://www.linkedin.com/in/shahab-malik/"&gt;Shahab Malik&lt;/a&gt; 😀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>frontend</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Getting Started with Firebase Authentication: A Comprehensive Guide</title>
      <dc:creator>SHAHAB MALIK</dc:creator>
      <pubDate>Sun, 16 Apr 2023 01:12:50 +0000</pubDate>
      <link>https://forem.com/shahabmalikk/getting-started-with-firebase-authentication-a-comprehensive-guide-1j81</link>
      <guid>https://forem.com/shahabmalikk/getting-started-with-firebase-authentication-a-comprehensive-guide-1j81</guid>
      <description>&lt;h4&gt;
  
  
  Start using Firebase today and thank me later 😃
&lt;/h4&gt;

&lt;p&gt;Firebase Authentication is a service provided by Google that allows you to add user authentication to your app with ease. It provides a range of authentication methods and features that make it easy to manage user authentication, protect user data, and build secure and scalable apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why You Should go for Firebase Authentication 🤔 ?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Easy to use: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Firebase Authentication provides a simple and easy-to-use interface for authentication. The Firebase SDKs offer a range of APIs and libraries for various platforms, making it easy to integrate authentication into your app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Security: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Firebase Authentication offers a range of security features, including multi-factor authentication, custom claims, OAuth2 integration, and more. This ensures that your app and user data remain secure.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple sign-in methods: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Firebase Authentication supports multiple sign-in methods, including email and password, Google, Facebook, Twitter, GitHub, and more. This allows you to offer your users a range of sign-in options.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customization: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Firebase Authentication offers a range of customization options, such as custom email templates, that allow you to tailor the authentication experience to your app's needs.&lt;/p&gt;

&lt;p&gt;Overall, Firebase Authentication is a reliable and secure authentication solution that can save you time and effort in implementing authentication in your app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Here's a step-by-step guide to help you get started with Firebase Authentication:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Step 1: Set up your Firebase project
&lt;/h4&gt;

&lt;p&gt;To get started with Firebase Authentication, you'll need to create a Firebase project. Go to the Firebase console, create a new project, and enable Firebase Authentication in the Authentication tab.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4prbh7kxo53wn79bawvl.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4prbh7kxo53wn79bawvl.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: Choose an authentication method
&lt;/h4&gt;

&lt;p&gt;Firebase Authentication supports a range of authentication methods, including email and password, Google, Facebook, Twitter, GitHub, and more. Choose the authentication method that best suits your app's needs.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgkmlkchwwsfsvt7go3qp.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgkmlkchwwsfsvt7go3qp.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Let's Create a Login SignUp Form :
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Create a Signup.Html File
&lt;/h5&gt;

&lt;h6&gt;
  
  
  HTML CODE :
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="card-container"&amp;gt;
    &amp;lt;div class="box"&amp;gt;
      &amp;lt;h1 class="form-heading"&amp;gt;Signup&amp;lt;/h1&amp;gt;
      &amp;lt;form action="" class="form" method="post"&amp;gt;
        &amp;lt;div class="input-cotainer"&amp;gt;
          &amp;lt;input type="text" name="fname" placeholder="Enter First Name" id="fname"&amp;gt;
          &amp;lt;input type="text" name="lname" placeholder="Enter Last Name" id="lname"&amp;gt;

        &amp;lt;/div&amp;gt;
        &amp;lt;input type="email" name="email" placeholder="Enter your Email" id="email"&amp;gt;
        &amp;lt;input type="password" name="password" placeholder="Enter your Password" id="password"&amp;gt;
        &amp;lt;button type="submit" id="signUpbutton"&amp;gt;Signup&amp;lt;/button&amp;gt;
        &amp;lt;p&amp;gt;Already Have Account &amp;lt;a href="login.html" class="link"&amp;gt;Login&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;
      &amp;lt;/form&amp;gt;

    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  JS CODE For Signing up New Users :
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script type="module"&amp;gt;
  import { app } from './index.js'

  import { getAuth, createUserWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/9.18.0/firebase-auth.js";

  const auth = getAuth()

  import { set, ref, push, child, } from "https://www.gstatic.com/firebasejs/9.18.0/firebase-database.js";

  const signUpButton = document.getElementById('signUpbutton')


  signUpButton.addEventListener('click', (e) =&amp;gt; {
    e.preventDefault()
    const fname = document.getElementById('fname').value
    const lname = document.getElementById('lname').value
    const email = document.getElementById('email').value
    const password = document.getElementById('password').value

    console.log(email)
    createUserWithEmailAndPassword(auth, email, password)
      .then((userCredential) =&amp;gt; {
        // Signed in 
        const user = userCredential.user;
        console.log(user)
        const userId = push(child(ref(database), 'users')).key
        set(ref(database, 'users/' + userId), {
          fname: fname,
          lname: lname,
          email: email,
          password: password
        }).then(() =&amp;gt; {

          console.log('Data saved')
          // window.location.href = 'login.html'
        }).catch((error) =&amp;gt; {
          console.log(error)
        })
        // ...
      })
      .catch((error) =&amp;gt; {
        const errorCode = error.code;
        const errorMessage = error.message;
        alert(errorMessage)
        // ..
      });

  })


&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Create a LogIn.Html File
&lt;/h5&gt;

&lt;h6&gt;
  
  
  HTML CODE :
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="card-container"&amp;gt;
    &amp;lt;div class="box"&amp;gt;
      &amp;lt;h1 class="form-heading"&amp;gt;Login&amp;lt;/h1&amp;gt;
      &amp;lt;form action="" class="form"&amp;gt;
        &amp;lt;input type="email" name="email" placeholder="Enter your Email" id="email"&amp;gt;
        &amp;lt;input type="password" name="password" placeholder="Enter your Password" id="password"&amp;gt;
        &amp;lt;button type="submit" id="logInbutton"&amp;gt;Log In&amp;lt;/button&amp;gt;
        &amp;lt;p&amp;gt;Create New Account &amp;lt;a href="index.html" class="link"&amp;gt;Signup&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;
      &amp;lt;/form&amp;gt;

    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  JS CODE for Logging In User:
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script type="module"&amp;gt;
  import { database} from './index.js'
  import { ref, update } from "https://www.gstatic.com/firebasejs/9.18.0/firebase-database.js";
  import { getAuth, signInWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/9.18.0/firebase-auth.js";
  const auth = getAuth();
  const logInButton = document.getElementById('logInbutton')

  logInButton.addEventListener('click', (e) =&amp;gt; {
    e.preventDefault()
    const email = document.getElementById('email').value
    const password = document.getElementById('password').value
    signInWithEmailAndPassword(auth, email, password)
      .then((userCredential) =&amp;gt; {
        // Signed in 

        const user = userCredential.user;
        update(ref(database, 'loggedIn/' + user.uid), {
          email: email,
          password: password,
          lastLogin: new Date().toLocaleString()

        }).then(() =&amp;gt; {
          alert('Login Successful')
          console.log('Data saved')
          window.location.href = 'home.html'

        }).catch((error) =&amp;gt; {
          console.log(error)
        })

        // ...
      })
      .catch((error) =&amp;gt; {
        const errorCode = error.code;
        const errorMessage = error.message;
        alert(errorMessage)
      });


  })

&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have implemented above steps &lt;br&gt;
Congratulations🎉  You are done with Firebase Authentication&lt;/p&gt;

&lt;p&gt;You can download Source Code from here &lt;a href="https://github.com/Shahab-Malikk/Firebase-CRUD-Application" rel="noopener noreferrer"&gt;Authentication in Firebase&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Want to learn about Web Development Follow me 😀 &lt;/p&gt;

&lt;p&gt;LinkedIn : &lt;a href="https://www.linkedin.com/in/shahab-malik/" rel="noopener noreferrer"&gt;Shahab Malik&lt;/a&gt;&lt;br&gt;
Twitter : &lt;a href="https://twitter.com/malik_227710" rel="noopener noreferrer"&gt;Shahab Malik&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>firebase</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
