<?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: James F. Thomas</title>
    <description>The latest articles on Forem by James F. Thomas (@jamesfthomas).</description>
    <link>https://forem.com/jamesfthomas</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%2F418779%2F31663dbb-3c9b-4eee-b4bf-41516c6cd1e9.jpg</url>
      <title>Forem: James F. Thomas</title>
      <link>https://forem.com/jamesfthomas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jamesfthomas"/>
    <language>en</language>
    <item>
      <title>Just Which Way Does The Auth Flow? #3</title>
      <dc:creator>James F. Thomas</dc:creator>
      <pubDate>Sat, 19 Sep 2020 22:14:42 +0000</pubDate>
      <link>https://forem.com/jamesfthomas/just-which-way-does-the-auth-flow-3-36dk</link>
      <guid>https://forem.com/jamesfthomas/just-which-way-does-the-auth-flow-3-36dk</guid>
      <description>&lt;h2&gt;
  
  
  &lt;em&gt;The Switch In The Flow From Authenticate to Authorize&lt;/em&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Introduction:
&lt;/h3&gt;

&lt;p&gt;Last time we concluded our exploration of the OAuth process with the secrete calling of the &lt;code&gt;req.login()&lt;/code&gt; function. Remember that it is just one of the little perks of putting Passport.js middleware functions into our project. So, thank you very much Passport.js, and now we can move on up to serialize. At this point in our control flow, having completed authentication, our user information is being passed from the &lt;code&gt;done()&lt;/code&gt; function in our Verify Callback to the next function in the chain, &lt;code&gt;serializeUser()&lt;/code&gt;. This function marks the establishment of a new user session and the last level of our exploration.               &lt;/p&gt;

&lt;h3&gt;
  
  
  What is a  user session?
&lt;/h3&gt;

&lt;p&gt;During my research, I encountered several different ways people defined &lt;em&gt;user session&lt;/em&gt;, but for our purposes, we will think of it as a cache of requests a user makes during a specific time period. This time begins at the invocation of &lt;code&gt;req.login()&lt;/code&gt; and ends, of course, when you call &lt;code&gt;req.logout()&lt;/code&gt;. But wait there is some Passport.js hidden magic going on again.  The &lt;code&gt;logout()&lt;/code&gt; function is exposed on the &lt;em&gt;request&lt;/em&gt; object, and will be called inside the route handler. It is extremely easy to miss this connection, I know I did, mostly because of the differences in scope. So instead of &lt;code&gt;logout()&lt;/code&gt; is found in that chain of Passport.js functions we set up in the authentication strategy, it’s called when the user is on the way out the door, like turning off the lights when the user clicks logout.&lt;/p&gt;

&lt;h4&gt;
  
  
  Code Example: &lt;code&gt;logOut()&lt;/code&gt; function invocation in route handler
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;apiRouter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/logout&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logOut&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above code block, you can visualize where the &lt;code&gt;logout()&lt;/code&gt; function is invoked. This code block was housed in a sperate file from the OAuth strategy setup used in my project and illustrates how someone could leave it out of their map of the control flow. Let’s backtrack a bit before we forget about &lt;code&gt;serializeUser()&lt;/code&gt;.         &lt;/p&gt;

&lt;h3&gt;
  
  
  So where does serialize fit in?
&lt;/h3&gt;

&lt;p&gt;Remember that serialization occurs between &lt;em&gt;login&lt;/em&gt; and &lt;em&gt;logout&lt;/em&gt; functions, once the user’s information is passed from our verify callback to &lt;code&gt;serilazeUser()&lt;/code&gt; at the beginning of our user session. The job of the &lt;code&gt;serailizeUser()&lt;/code&gt; function is to translate certain user data, in this case, the id, into a format that can be stored within a session cookie. The session cookie is sent to the browser and maintained for the length of the session. The baking and decorating of session cookies is a treat we’ll cover another time.    &lt;/p&gt;

&lt;h4&gt;
  
  
  Code Example: Passport.js &lt;code&gt;serializeUser()&lt;/code&gt; function
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serializeUser&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;When we examine the code block above you see that &lt;code&gt;serializeUser()&lt;/code&gt; accepts two arguments, the user object, and the &lt;code&gt;done()&lt;/code&gt; function. It is at this point that &lt;code&gt;serializeUser()&lt;/code&gt; will designate what user information to translate into the session cookie by passing it to done. Once done has been invoked our code is passed onto &lt;code&gt;deserializeUser()&lt;/code&gt; which will then locate a user within our database that matches the translated data.  This serialization and deserialization of user information allows our application to keep all user data safe server-side, while the cookie will only contain a session id that is used to continuously authorize that user’s requests throughout the life of the session.               &lt;/p&gt;

&lt;h4&gt;
  
  
  Code Example: implementation of Passport.js &lt;code&gt;deserializeUser()&lt;/code&gt; function
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializeUser&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;foundUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foundUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;foundUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;no user with that id found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;If the session hasn’t expired or the user hasn’t hit the logout route, the serialize/deserialize set will be working to keep that user happy providing information to their authorized requests. One function I missed mentioning earlier was the &lt;code&gt;destroy()&lt;/code&gt; function. This method is part of the Express.js framework and not native to the Passport.js control flow unless you specifically choose to employ it in your applications’ structure. The &lt;code&gt;destroy()&lt;/code&gt; method will destroy the session cookie, subsequently ending that users authorization,  and requiring the entire OAuth process be restarted for future requests to be served.       &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;At this point, we have explored all the levels as well as players involved in those steps that need to be taken in order to authenticate and authorize a user. I know that I learned a few very interesting things and it is my hope that you did too. Biggest take away from this whole dive:&lt;/p&gt;

&lt;p&gt;Authentication is the process of proving you are who you say you are, while Authorization is the process of being allowed to go into certain areas, use certain services, or view specific content. Here's a fun little analogy: All crew sailors can get permission to come aboard the ship, but only certain members can eat in the officer's mess.    &lt;/p&gt;

&lt;p&gt;I'll be back soon, to tackle some new topics. I've been told by many teachers that &lt;em&gt;the more you write, the more you grow&lt;/em&gt;, so to ensure my development skills continue to grow I will write continuously.     &lt;/p&gt;

&lt;p&gt;Happy Coding!!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7jnGjrQJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9u5m67qdu3lxapcgxces.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7jnGjrQJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9u5m67qdu3lxapcgxces.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Wikipedia.com (&lt;a href="https://en.wikipedia.org/wiki/Session_(computer_science)"&gt;https://en.wikipedia.org/wiki/Session_(computer_science)&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How do Express.js Sessions work? (&lt;a href="https://tilomitra.com/how-do-nodejs-sessions-work/"&gt;https://tilomitra.com/how-do-nodejs-sessions-work/&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Passport.org (&lt;a href="http://www.passportjs.org/docs/configure/"&gt;http://www.passportjs.org/docs/configure/&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A peep beneath the hood of PassportJS' OAuth flow (&lt;a href="https://dev.to/anabella/a-peep-beneath-the-hood-of-passportjs-oauth-flow-eb5"&gt;https://dev.to/anabella/a-peep-beneath-the-hood-of-passportjs-oauth-flow-eb5&lt;/a&gt;) &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>authentication</category>
      <category>authorization</category>
    </item>
    <item>
      <title>Just Which Way Does The Auth Flow? #2 </title>
      <dc:creator>James F. Thomas</dc:creator>
      <pubDate>Sun, 13 Sep 2020 17:13:16 +0000</pubDate>
      <link>https://forem.com/jamesfthomas/just-which-way-does-the-auth-flow-2-1dkf</link>
      <guid>https://forem.com/jamesfthomas/just-which-way-does-the-auth-flow-2-1dkf</guid>
      <description>&lt;h2&gt;
  
  
  Requesting &amp;amp; Utilizing User Information To Authenticate
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Introduction:
&lt;/h3&gt;

&lt;p&gt;Last time on blog post, we took a dive into the subject of user authentication and explored what I like to think is its outermost layer of the process. We briefly defined what the “user auth” model entails and how we can utilize the Passport.js library to accomplish these goals. While we did not cover all the different authentication strategies provided via Passport.js, we did describe the most traditional user auth method the “Local” strategy. If you recall the local strategy involves setting in place a process in which the user can create a username and password, that will be persisted, and later used to grant them access to restricted aspects of your application. My goal for this post is to dive into the second layer of the user authentication control flow, unpacking the events that occur between the first redirect away, the user requesting authentication, and the final redirect back to our application as a trusted guest.        &lt;/p&gt;

&lt;h3&gt;
  
  
  What happens between redirects and the auth request?
&lt;/h3&gt;

&lt;p&gt;After the user lands on our home page and is prompted to log in, they are then redirected to the OAuth provider where they click an authorized link, and now what? Well, now we are inside of the OAuth process, where the user will grant authorization to our application to utilize their profile information in order to prove they are, without exposing their password. The next question should be along the lines of if they do not give out a password then what will our application use to grant them access? The OAuth provider, upon successful user verification, will grant our application, the consumer, a temporary pass also known as an OAuth or access token. The way I like to think of these tokens is like a limited access pass to the user's information, in which exactly what information our application is authorized to use is designated on the token, and any other sensitive information pertaining to the user is never exposed.  &lt;/p&gt;

&lt;h4&gt;
  
  
  Code Example: access token
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;access_token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;RsT5OjbzRn430zqMLgV3Ia&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;expires_in&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3600&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet is an example of the access token that will be returned to our application via the OAuth provider. As you can see, the token contains an id string and an expiration time. &lt;/p&gt;

&lt;h3&gt;
  
  
  What happens after we get the token?
&lt;/h3&gt;

&lt;p&gt;Now that our application has a verified access token, it can make HTTP requests to the provider API for information for as long as that token is valid. But we are more concerned with user profile information returned along with the token from the Passport.js strategy we configured and incorporated into our application. The strategy configuration contains a function called the “Verify Callback”, which is used to locate user information with this matching profile information. If we have never encountered this user then their information will not be persisted in our database and we must now make an entry for them, but if they are located the corresponding information is returned. The information is now passed to the done() function of our verified callback and saved as user on the request object. &lt;/p&gt;

&lt;h4&gt;
  
  
  Code Example: Passport.js OAuth strategy configuration
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;GoogleStrategy&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="c1"&gt;// define the options to use with google strategy&lt;/span&gt;
  &lt;span class="na"&gt;clientID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GOOGLE_CLIENT_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;clientSecret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GOOGLE_CLIENT_SECRET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;callbackURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GOOGLE_CALLBACK_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;refreshToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// deconstruct variables from profile object&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;displayName&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;idDiscord&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;profilePhotoUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;photos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="nx"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userObj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;gotUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gotUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;gotUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;addUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userObj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, you can follow the control flow of the code block contained in the “Verify Callback”. The user id, display name, and photo URL are deconstructed from the returned google profile information as &lt;em&gt;userObj&lt;/em&gt;. The variable &lt;em&gt;userObj&lt;/em&gt; is then passed as an argument to the &lt;code&gt;getUser()&lt;/code&gt; function that will query the database for a matching entry. If a successful match is located that information is returned and then passed onto the &lt;code&gt;done(null, gotUser)&lt;/code&gt; function, but if no match is found in the database a sperate function &lt;code&gt;adduser()&lt;/code&gt; is called to create an entry for that user. The newly persisted users information is now returned and also passed to the &lt;code&gt;done(null, newUser)&lt;/code&gt; function. The part that isn’t shown in the code but rather handled backstage by &lt;em&gt;Passport.js&lt;/em&gt; is the invoking of &lt;code&gt;req.login()&lt;/code&gt;.   &lt;/p&gt;

&lt;h4&gt;
  
  
  Code Example: Passport.js login function
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The Passport.js library has an exposed login function on the request object, that will add the returned user information passed to &lt;code&gt;done()&lt;/code&gt; as &lt;em&gt;req.user&lt;/em&gt;. This information can be utilized in different ways within our application, usually for authorization, but most commonly to establish a new user session. The reason we haven’t seen it in our set up of authentication middleware is that Passport.js will call &lt;code&gt;req.login()&lt;/code&gt; automatically when you are utilizing its middleware. Now, isn't that a great little bit of information to store under your cap for next time?     &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;Much to my surprise, during my research I discovered that user authentication and user sessions are not within the same process and so I’m stopping our dive into level 2 of the OAuth flow right here. Next time we will finish our exploration of OAuth by unpacking what a user session is and all the Passport.js functions involved in that process. Hope you learned a little something new about user authentication, and until next time:      &lt;/p&gt;

&lt;p&gt;Happy Coding!!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7jnGjrQJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9u5m67qdu3lxapcgxces.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7jnGjrQJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9u5m67qdu3lxapcgxces.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;What is OAuth? Definition and How it Works (&lt;a href="http://www.varonis.com"&gt;www.varonis.com&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wikipedia.org (&lt;a href="http://www.en.wikipedia.org/wiki/OAuth#OAuth_2.0"&gt;www.en.wikipedia.org/wiki/OAuth#OAuth_2.0&lt;/a&gt;) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Passport.js docs (passportjs.org)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A peep beneath the hood of PassportJS' OAuth flow (dev.to)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>passportjs</category>
      <category>authentication</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Just Which Way Does The Auth Flow?</title>
      <dc:creator>James F. Thomas</dc:creator>
      <pubDate>Sun, 06 Sep 2020 16:23:27 +0000</pubDate>
      <link>https://forem.com/jamesfthomas/just-which-way-does-the-auth-flow-1p0a</link>
      <guid>https://forem.com/jamesfthomas/just-which-way-does-the-auth-flow-1p0a</guid>
      <description>&lt;h2&gt;
  
  
  The Passport Authentication Control Flow
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Introduction:
&lt;/h3&gt;

&lt;p&gt;I am currently wrapping up the student portion of my journey as a &lt;br&gt;
software developer and that means group projects, huzzah! Now, this is the time in which you sharpen your skills by getting things done with your peers with minimal involvement from program instructors, or at least that is the way it is done in our program. Anyway, while working in my current group, on a great project I might add, a groupmate asked me a question to which I felt I gave a subpar answer at best. So, in my quest to present a more informed answer in addition to satisfying my own standards of knowledge I decided to do some more research which lead to this blog. The question is coming, but first, we need to set the scene and explain the tech involved so no matter what level of developer you are currently, you can follow this story and possibly learn something constructive in the process. This story begins with authentication and since we were using node.js in our project that means I should briefly introduce passport.js.   &lt;/p&gt;
&lt;h3&gt;
  
  
  What does is it mean authenticate?
&lt;/h3&gt;

&lt;p&gt;User authentication involves obtaining unique identifying information from users traditionally including username + password, identification numbers, phone numbers, pin numbers, code words, secret handshakes, or email addresses. I snuck the secret handshake one in there just see if you were really reading, but in a physical setting that would definitely make the list. If you want a physical auth example for digit access, think of eye scans or thumbprint access now employed on phones. &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%2Fi%2Fadmie2yp55n301vylqlc.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%2Fi%2Fadmie2yp55n301vylqlc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once these credentials are obtained and confirmed against a persisted list to be valid, your website will grant that authorized user access to previously restricted sections, services, or resources hosted on the site. Think of a situation in which you visited a site and were prompted to log in or create an account with a username and password. Well, my friend, that is authentication, and in this digital world, we have all experienced it.                 &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%2Fi%2Fmc8cc5t989m3qre74pcq.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%2Fi%2Fmc8cc5t989m3qre74pcq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just to give you a visual reminder, the above picture is a basic example of the traditional authentication process that we are most familiar with. Now that we are clear on what it is to authenticate, let’s tackle the passport part.    &lt;/p&gt;
&lt;h3&gt;
  
  
  What is passport.js and why use it?
&lt;/h3&gt;

&lt;p&gt;Passport.js is an authentication library that allows developers to use external avenues or third-party entities for user authentication. Passport is middleware for Node.js based applications that serves the singular purpose of authenticating users to grant them access to an application. This allows a dev to basically outsource the task of collecting, persisting, and verifying user information to someone else and focus on creating an awesome application. This is great because it is one less user feature for the dev to have to map out, setup, and test. In my opinion, the biggest benefits of using Passport.js rather than setting up your own authentication are familiarity and trust.  In the situation where a user is unfamiliar with a particular site and in turn, does not want to supply any personal information, Passport employs &lt;em&gt;OAuth&lt;/em&gt; services from well know entities that will then be used to send identifying information to the less trusted sites granting users temporary access without exposing personal information or passcodes. Passport utilizes OAuth services with well-known sites like Facebook, Instagram, LinkedIn, Twitter, and Google to request certain information, not your password, known as tokens that will be used to authenticate and then authorize access to sites for a specific amount of time. Because users are more likely to already have an account with one of the trusted OAuth providers, this avenue of user authentication is becoming more popular. Each provider will require different authentication steps and so passport has created a different &lt;em&gt;strategy&lt;/em&gt; for over 500 different OAuth providers from which developers can choose to incorporate into their site authentication process. To browse the full list visit &lt;a href="http://www.passportjs.org/packages/" rel="noopener noreferrer"&gt;http://www.passportjs.org/packages/&lt;/a&gt;. &lt;/p&gt;
&lt;h3&gt;
  
  
  Is he ever going to reveal the original question?
&lt;/h3&gt;

&lt;p&gt;Now that the foundation is laid, and we understand what user authentication is along with how Passport.js fits into that process I feel I can move into the question that was asked to me.  So, after we successfully incorporated Passport.js into our project and employed a Google OAuth strategy for users, and in the process of viewing the returned user profile object to implement other application features, I was asked if I knew the flow of the authentication middleware. I was able to use my intuition and a chain of &lt;code&gt;console.log()&lt;/code&gt; statements to give an answer but that wasn’t enough for me and so here we are. &lt;/p&gt;

&lt;p&gt;What is the flow of information through the Passport.js chain of middleware?&lt;/p&gt;

&lt;p&gt;First, in order to provide an OAuth service on your website, you must choose and configure the strategy. For our application, we choose the Google strategy and so below is how I configured and incorporated it into our application. I created a file titled &lt;code&gt;passport-setup.js&lt;/code&gt; in which I required all needed dependencies and configured the Google strategy. &lt;/p&gt;
&lt;h5&gt;
  
  
  passport-setup.js
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// require passport library&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;passport&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;passport&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// import googleStrategy from passport library&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;GoogleStrategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;passport-google-oauth20&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// import db query functions from database/index.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;addUser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../database/index&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// set up passport middleware to use google strategy in our project&lt;/span&gt;
&lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GoogleStrategy&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="c1"&gt;// options for the strategy, input clientID &amp;amp;&amp;amp; clientSecret&lt;/span&gt;
    &lt;span class="na"&gt;callbackURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/auth/google/redirect&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;clientID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GOOGLE_CLIENT_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;clientSecret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GOOGLE_CLIENT_SECRET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;refreshToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// passport callback function&lt;/span&gt;
    &lt;span class="c1"&gt;// console.log('passport callback fired'); // indication that function fired&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;USER PROFILE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// shows returned profile information&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;photos&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;addUser&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;google_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;profile_photo_url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;photos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Created New User: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// completes the http request, and sends information to next function in middleware chain&lt;/span&gt;
        &lt;span class="nf"&gt;done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;


&lt;p&gt;When an unauthorized user visits and clicks a restricted link, they should be directed to a login page with authentication service options if more than one is set up or directly to the authorization endpoint. In the case of our application when users visited and clicked the login-in button in the navigation bar, they were redirected to google.com. The redirect was accomplished via an authentication route like the code snippet below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;authRouter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/google&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;google&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;}));&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Once the user has completed the authentication steps via &lt;em&gt;Google&lt;/em&gt; they will be redirected back to our application at a specific endpoint. This was accomplished by specifying the callback route like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="cm"&gt;/**
 * google callback route returns users to the home screen following google authentication
 * utilize passport.authenticate() function to authenticate the user via google
 * retrieve the users profile information
 * finish by redirecting the user back to homepage "/"
 */&lt;/span&gt;

&lt;span class="nx"&gt;authRouter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/google/redirect&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;google&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;These three steps are going to serve as our top layer of the &lt;em&gt;OAuth&lt;/em&gt; process because if you really examine each code example you will see portions that need deeper explanation and also require additional pieces of the puzzle to make them work. But from this top layer, we have directed the user to our OAuth provider, allowed them to log in, sent back the user profile information along with temporary access information which will be stored by our site. At this point, the user has been granted access to restricted service on our site and did not have to give any personally sensitive information.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;While authentication is a procedure we are all very familiar with these days, so is data security or lack thereof, and after learning more about the entire &lt;em&gt;OAuth&lt;/em&gt; process I find it to be a simple aspect of UI/UX design that a developer can employ to make potential users more trusting of the applications they build.  I want to continue our deep-dive into the &lt;em&gt;OAuth&lt;/em&gt; process, so in the next installment we will unpack where to retrieve all the information needed to properly set up the OAuth strategy you want to employ, in addition to clarifying how the temporary access information returned is stored and then used on your site. I do hope you learned a little something new about the authentication process and that it sparks your own search for new things to incorporate into future projects. See you soon but until then….      &lt;/p&gt;

&lt;h4&gt;
  
  
  Happy Coding!!!
&lt;/h4&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%2Fi%2Fmn1uf9p3491ult2h0j00.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%2Fi%2Fmn1uf9p3491ult2h0j00.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Passport Docs (&lt;a href="http://www.passportjs.org" rel="noopener noreferrer"&gt;www.passportjs.org&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Microsoft Docs (&lt;a href="http://www.docs.microsoft.com/" rel="noopener noreferrer"&gt;www.docs.microsoft.com/&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Learn OAuth 2  (&lt;a href="http://www.tutorialspoint.com/oauth2.0/index.htm" rel="noopener noreferrer"&gt;www.tutorialspoint.com/oauth2.0/index.htm&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>passport</category>
      <category>growth</category>
    </item>
    <item>
      <title>Unifying our basics of TypeScript </title>
      <dc:creator>James F. Thomas</dc:creator>
      <pubDate>Sun, 30 Aug 2020 16:25:27 +0000</pubDate>
      <link>https://forem.com/jamesfthomas/unifying-our-basics-of-type-of-script-5bbc</link>
      <guid>https://forem.com/jamesfthomas/unifying-our-basics-of-type-of-script-5bbc</guid>
      <description>&lt;h2&gt;
  
  
  Exploring the "Union Type" and how it is applied.
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Introduction:
&lt;/h3&gt;

&lt;p&gt;Last week we ended our journey into the basics of TypeScript (TS) by showcasing how the language implements the features of static typing, optional inferred typing, and how it utilizes the static typing rules with complex data types called interfaces. Remember static typing refers to the dev being required to define the variable data types before use, optional inferred typing is when the compiler sets variables data type from the first value assigned to it, and finally, interfaces are the construction blueprints that outline the member or return types of the values encapsulated within our complex data structures (objects, arrays, functions). These rules were all employed with variables that were assigned to a single value or output type. This week it is my goal to highlight how the TS language utilizes another characteristic in order to combine these usually singular assignments into a variety of inputs and outputs for the variables a developer may need to utilize in the code their composing. Let us jump right in and see how TS unites the selections we have available to make our code more accepting of all the possibilities.             &lt;/p&gt;

&lt;h3&gt;
  
  
  What is the Union Type?
&lt;/h3&gt;

&lt;p&gt;The union type is an attribute of the TS language that allows a developer to choose from many possible value data types for their variable assignments, function inputs, and returns. Up until this point in my coding journey, I have personally been exposed to single value assignments. The closest situation in which I have gotten to experience something similar to the union type in JavaScript (JS) is using conditional statements to evaluate for data type and then controlling the response based on those data types encountered.&lt;/p&gt;

&lt;h4&gt;
  
  
  JS code example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;exampleFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;value is a string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;value is a number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;input must be number or string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;exampleFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="c1"&gt;// prints "input must be number or string"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The above code block is an example of how I have previously utilized aspects of JS to evaluate variable data types and then control program response based solely on the data type entered as an argument to the function. The if conditional coupled with the &lt;code&gt;typeof&lt;/code&gt; operator work well to accomplish this goal, but as you can see there are multiple steps that need to be put into place in order to accomplish this goal.&lt;br&gt;&lt;br&gt;
With each piece of the block that needs to be set in place, there is another chance for error and so enters the TS Union type to make the life of a new dev like me a little bit simpler.  With TS the task of assigning expected specific variable data types is accomplished by using the Union type, and the single “|” pipe operator.  &lt;/p&gt;

&lt;h4&gt;
  
  
  TS code example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;fixCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cartype&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MITSUBISHI&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TOYOTA&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cartype&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MITSUBISHI&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TOYOTA&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;your vehicle is fixed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;We don't service that make of vehicle here.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fixCar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;FORD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;// Argument of type '"FORD"' is not assignable to parameter of type '"MITSUBISHI" | "TOYOTA"'.&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;Looking at the above TS example we can easily see how the code block is far simpler than the JS code which performs the same task. We have fewer &lt;code&gt;if&lt;/code&gt; conditionals to evaluate the parameter ‘cartype’, and we no longer need to utilize the &lt;code&gt;typeof&lt;/code&gt; operator. To me, the beauty of this union type comes about when you attempt to use the function &lt;code&gt;fixCar()&lt;/code&gt;. Remember “Static Typing”, well here it is again, by utilizing the union type when we define the function parameters we will receive an immediate error message from the compiler when we attempt to use the function &lt;code&gt;fixCar()&lt;/code&gt; with any other parameters besides those explicitly defined in the function declaration.   &lt;/p&gt;

&lt;h3&gt;
  
  
  What else can I unite in TS?
&lt;/h3&gt;

&lt;p&gt;Now that we have seen one example use of the Union type in TS, let's quickly look at a few other examples of how we can use the Union type to require certain inputs for our data structures.&lt;/p&gt;

&lt;h4&gt;
  
  
  simple data type example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;singleVar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;singleVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm OK&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Good&lt;/span&gt;

&lt;span class="nx"&gt;singleVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Good&lt;/span&gt;

&lt;span class="nx"&gt;singleVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Bad =&amp;gt; Type '24' is not assignable to type 'string | boolean'.(2322)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above code block, we have utilized the Union type to explicitly require that and value assigned to the &lt;code&gt;singleVar&lt;/code&gt; variable be of the “string” or “Boolean” type. Any other value types will cause the compiler to throw an error and our code won’t run.&lt;/p&gt;

&lt;h4&gt;
  
  
  mixed data type example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;mixedVar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;

&lt;span class="nx"&gt;mixedVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is good&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nx"&gt;mixedVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm good too&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 

&lt;span class="nx"&gt;mixedVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Bad =&amp;gt; Type 'boolean' is not assignable to type 'string | string[]'.&lt;/span&gt;

&lt;span class="nx"&gt;mixedVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Not Good&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
 &lt;span class="c1"&gt;// Type 'number' is not assignable to type 'string'.&lt;/span&gt;
 &lt;span class="c1"&gt;// Type 'boolean' is not assignable to type 'string'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above code block, we have utilized the union type to explicitly require that the value assigned to the “mixedVar” variable be a “string” or an array of “string” types variables. I really thought this was a cool use when I first encountered it. Not only will TS type check your single values, it will do the same to values inside your complex data types. Now honestly how cool is that?  &lt;/p&gt;

&lt;h4&gt;
  
  
  complex data type example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;School&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;programName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CodingStudent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Student&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;School&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;coder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CodingStudent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;James&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;36&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;coder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//Type 'boolean' is not assignable to type 'CodingStudent'.&lt;/span&gt;

&lt;span class="nx"&gt;coder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;programName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Operation Spark&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;In our final code example above, we have utilized not only the Union type but we have also created a custom type by combining the requirements defined in our interfaces. Combining interfaces allowed us to create instances that can contain attributes from either interface and still satisfy our static typing restrictions set within either interface.   &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;In this leg of or journey, we expanded our understanding of the static typing system employed in the TS language by combining it with the Union type. The Union type allows us to increase the choices of possible values that can be assigned to the variables I want to use in my code. The Union type can be combined with simple or complex data values, along with other aspects of the TS language to create custom types via the &lt;code&gt;|&lt;/code&gt; &amp;amp; &lt;code&gt;type&lt;/code&gt; operators. I am really enjoying learning about the new ways that TS is providing me to interact with my code and will continue to explore them in blogs to come. I'm looking forward to unpacking the concept of type guarding in the very near future, but first, more research and practice so it's presented in a clear and correct way. Hope you learned a little something new.          &lt;/p&gt;

&lt;p&gt;Happy Coding!!!  &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%2Fi%2F4wv4h5u2xr0tnpeebncg.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%2Fi%2F4wv4h5u2xr0tnpeebncg.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TypeScript Docs (&lt;a href="https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html" rel="noopener noreferrer"&gt;https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;whiley.org &lt;/li&gt;
&lt;li&gt;howtodoinjava.com&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>typescript</category>
      <category>growth</category>
    </item>
    <item>
      <title>Learning about a new Type of Script</title>
      <dc:creator>James F. Thomas</dc:creator>
      <pubDate>Mon, 24 Aug 2020 01:33:35 +0000</pubDate>
      <link>https://forem.com/jamesfthomas/learning-about-a-new-type-of-script-1mhg</link>
      <guid>https://forem.com/jamesfthomas/learning-about-a-new-type-of-script-1mhg</guid>
      <description>&lt;h2&gt;
  
  
  A Brief Introduction into the language of TypeScript:
&lt;/h2&gt;

&lt;p&gt;When you begin your journey into the world of writing code the nuances of the language you learn will be one of the most difficult things you encounter, or at least it was that way for me when I started. Wrapping my head around the concept of variable types, and then remembering the differences in how each interacted with the other in JavaScript was initially a huge challenge. With time the rules became clearer and I then began to feel more comfortable with writing more robust blocks of JavaScript code. I later experienced, mostly through error handling, that JavaScript is very lenient in what it will allow you to write and will even permit you to construct improper expressions without ever throwing an error. Frankly, I prefer to be warned that something is not set up correctly on line 1 before I can move onto line 2 or even further to 200.  Now that I have suffered through hours of debugging, engraining certain best practices, I am able to catch potential errors before they happen. Continuing my evolution as a developer learning new languages, I was pointed toward TypeScript, which was touted to protect against some of the errors that JavaScript allows. This blog is my dive into better understanding TypeScript starting at its roots.   &lt;/p&gt;

&lt;h3&gt;
  
  
  What is typescript?
&lt;/h3&gt;

&lt;p&gt;TypeScript (TS) is a static scripting language maintained by Microsoft that is open-sourced and intended to be a syntactical superset of JavaScript (JS). This is a very technical definition transposed from the TS docs, but it still needs further deconstruction for a newbie like me to gain a clear understanding of what TS is and how it differs from JS.  Static scripting refers to a type checking format or set of rules that a language enforces to ensure code the developer has written is bug-free. Static typing languages are those that enforce their type constraints at compile-time, and also demand that the developer define the data type of the language construct (variables, expressions, functions, etc.) they intend to use before it is utilized in their code. Static scripting allows for code to be debugged as it is written, prior to being run or executed by the computer, resulting in faster run times, and of course, we can all understand the importance of this feature. Catching bugs before code is run will ensure better and proper functionality of the programs you write. Let's examine how TS interacts with some primitive data types.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example 1
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;James Thomas&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// explicit data typing&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Above is an example of a static scripting language in action, explicitly defining the variable data type before it is assigned in the code. TS has an extra optional feature, due to its strongly typed designation, that needs to be showcased. The term “Strongly Typed” simply refers to the fact that in this language declared variables are bound to a certain data type. Variables must be assigned to values of the data type they “Explicitly Defined” or “Inferred” to be whenever used in the code or the compiler will return a type error. This optional feature mentioned will allow for the dynamic typing of written variables while still providing the security of static typing error protocols by the compiler. “Inferred Typing”, is the languages' ability to determine a variables data types without prior explicit definition. &lt;/p&gt;

&lt;h5&gt;
  
  
  Example 2
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;James Thomas&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// inferred data typing&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Although the above variable definition looks like vanilla JS, the magic happens when the variable is used later in code and fails to adhere to the typing rules of the language.  If the variable is used in a syntactically incorrect manner the compiler will throw an error just as if it were explicitly defined in our first code example.           &lt;/p&gt;

&lt;h5&gt;
  
  
  Example 3
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;36&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;James Thomas&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Error: Type 'string' is not assignable to type 'number'. (2322)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So, we have seen how TS works with a few primitive data types but still need to explore how the language applies the static typing parameters to complex data types.    &lt;/p&gt;

&lt;h3&gt;
  
  
  How Typescript interacts with complex data types?
&lt;/h3&gt;

&lt;p&gt;Because complex data can contain primitive values, the static typing rules employed by TS must interact with complex data types in a slightly different manner. To facilitate this static typing interaction with complex data, TS includes what is called an “Interface”. The Interface should be looked at as a defining blueprint or contract for all elements of the complex data type to be later defined and used in your code. These TS Interfaces operate much like ES6 class constructors, where each newly instantiated class instance will always contain the properties and methods defined in the parent constructor. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Side Note&lt;/strong&gt;&lt;/em&gt; =&amp;gt; TS does include classes as well but interfaces are a new aspect of the language to me and so I'm gonna highlight them here to improve my understanding. &lt;/p&gt;

&lt;p&gt;One major difference between Interfaces and Classes is that Interfaces only contain the declarations of the contained elements and not the values or method functionality to be inherited by each new entity created. Just to clarify “instance” refers to classes while “entity” is referring to interfaces. This means that with interfaces the developer must later define the elements of complex data type outlined by the interface.         &lt;/p&gt;

&lt;h5&gt;
  
  
  Example 4
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;answerRoll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// this interface defines the types of all Student objects to come&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Looking at code example above we see our TS interface called student which defines all the elements any Student object must contain. This interface is our blueprint to follow when we create Student objects for use in our code. Another key difference between interfaces and classes is that interface entities are not instantiated with the new keyword.  The syntax differs in that you must still define the object and all its containing elements.  &lt;/p&gt;

&lt;h5&gt;
  
  
  Example 5
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;senior&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;James&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;Thomas&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;McLaren&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;answerRoll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;Here&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ready&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// new Student object&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Although this way of creating new entities via the interface may seem a bit cumbersome compared to simply using the keyword “new” and a class constructor function the pay off comes at compile time. Remember that TS compiles into JS so when the compiler reads an interface it ignores it. Less code to compile results to smaller build times and no impact on the runtime of the code you wrote. That is a great pay off in my book.   &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;I think this is a great place to stop on this leg of our journey into the basics of TS. We have unpacked a few foundational concepts like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;What a static scripting language is?  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The differences between static and dynamic typing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What defines a strongly typed language.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The TS optional feature of "inferred" typing. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The differences between interfaces and classes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These concepts will allow us on our next leg to explore the Union Type and how it interacts with variables, functions, arrays, and interfaces. Hope you enjoyed the read and in the process learned a little something new.   &lt;/p&gt;

&lt;p&gt;Happy Coding!!!  &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%2Fi%2F1a91hubezy9g36vh2sg0.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%2Fi%2F1a91hubezy9g36vh2sg0.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;TypeScript docs (&lt;a href="https://www.typescriptlang.org/docs" rel="noopener noreferrer"&gt;https://www.typescriptlang.org/docs&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Oracle docs (&lt;a href="https://docs.oracle.com/cd/E57471_01/bigData.100/extensions_bdd/src/cext_transform_typing.html" rel="noopener noreferrer"&gt;https://docs.oracle.com/cd/E57471_01/bigData.100/extensions_bdd/src/cext_transform_typing.html&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tutorialspoint.com&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sitepoint.com (&lt;a href="https://www.sitepoint.com/typing-versus-dynamic-typing/" rel="noopener noreferrer"&gt;https://www.sitepoint.com/typing-versus-dynamic-typing/&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wikipedia.com&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>typescript</category>
      <category>learning</category>
      <category>growth</category>
    </item>
    <item>
      <title>Which Route Leads To The Middle? </title>
      <dc:creator>James F. Thomas</dc:creator>
      <pubDate>Fri, 24 Jul 2020 13:33:41 +0000</pubDate>
      <link>https://forem.com/jamesfthomas/which-route-leads-to-the-middle-34d4</link>
      <guid>https://forem.com/jamesfthomas/which-route-leads-to-the-middle-34d4</guid>
      <description>&lt;p&gt;&lt;em&gt;Exploring routes and middleware functions&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;The internet is an environment built on the to and fro movement of words describing persons, places, or things in the world we live in. The seemingly simple task of listening for and responding to those words accordingly can be a daunting duty all by itself. Luckily, we have some key phrases, that move along predetermined paths, being listened for by particular entities, that respond to the keywords, making our online conversations flow smoothly without ever stopping our rants or raves. In this blog, we are going to introduce and explain some of the key players in that data exchange and how they allow us to keep the words flowing as seamlessly as possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  What path are my words walking?
&lt;/h2&gt;

&lt;p&gt;The path that I am referring to is generally known as a &lt;code&gt;route&lt;/code&gt; in computer science. The term ‘route’ basically refers to the entire pathway our words or data must travel in order to reach intended destinations in a network.  The ‘route’ encompasses all the stops and tolls along the way to that destination including all players such as a computer, router, server, or database. More specifically, &lt;code&gt;routing&lt;/code&gt; or determining the route data will travel and in what way it will be processed, is directly related to the choice of words included in your data package. Remember those keywords I mentioned before, well here they come again, but this time let’s title them requests, or, methods, or verbs. Even though all three choices are interchangeable for the sake of clarity in the remainder of this introduction they will be called verbs.        &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%2Flogrhythm.com%2Fwp-content%2Fuploads%2F2020%2F02%2Fmonitoring-ospf-routing-protocols-image-1.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%2Flogrhythm.com%2Fwp-content%2Fuploads%2F2020%2F02%2Fmonitoring-ospf-routing-protocols-image-1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What keywords should I listen for?
&lt;/h2&gt;

&lt;p&gt;The aforementioned verbs, or specifically HTTP Request methods, are one of the key parts of the requests for information that are sent out along these routes. The request and subsequent response have a particular format that consists of other key portions (headers, data types, data amounts, cookies, etc.) but is not in our focus for today. Each of these HTTP verbs will enact a certain protocol to be performed on the data being sent to an intermediate (player along the route) and then elicit a particular response back to the client or entity having sent the request. HTTP stands for Hypertext Transfer Protocol and the verbs that you choose to use will trigger one of these predetermined transfer protocols. I thought that was an interesting little tidbit for you to start making a full picture with because it helped me. Anyway, there is no limit to the number of verbs that can be defined, meaning in the future our intermediates can be programmed with far more response protocols, but there are 5 that are more commonly used than others: GET, POST, PUT, PATCH, &amp;amp; DELETE.           &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%2Fcomputing.dcu.ie%2F~humphrys%2FNotes%2FNetworks%2Ftanenbaum%2F7-41.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%2Fcomputing.dcu.ie%2F~humphrys%2FNotes%2FNetworks%2Ftanenbaum%2F7-41.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Who’s listening?
&lt;/h2&gt;

&lt;p&gt;Now that we know what words are being listened for, we need to highlight” who” is listening, and that brings us to those intermediates I mentioned along our route. The specific player I am going to refer to is middleware. Middleware is a bridge between the operating system (“OS” =&amp;gt; your computer’s software) your machine is using and the applications (the particular programs being run on the “OS”) that are being run on it. Middleware functions are hidden or abstracted from the user but allow for the coupling of multiple applications enabling control of information and transfer between them. The bridge created by middleware allows applications to receive services and have certain capabilities that are not offered by the operating system of your computer, such as messaging, API management, and authentication. All middleware performs a communicative role between applications and the OS but the specific type will be solely dependent upon the tasks you are performing and the information you want to receive. So let’s take a look at a few basic examples of middleware in action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we are utilizing middleware functionality provided by the express.js framework. The express framework is imported into our project via the require() function and a new instance of the express framework is then instantiated allowing us access to the frameworks methods. The .get() method from express.js allows us to handle requests for information to a certain endpoint, file or server location, etc. The point we are requesting information from is our first input parameter &lt;code&gt;’/’&lt;/code&gt; while the second is a callback giving our function access to the request and response objects that will contain our data. Res.send() is a method that allows for response in the form a string to sent to our application and displayed on the DOM. The final component of our example is the app.listen() method which binds our application, to listen for connections on a certain host port.  &lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Topic Recap:&lt;/em&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We defined a ‘route’&lt;/li&gt;
&lt;li&gt;We highlighted HTTP Request Methods&lt;/li&gt;
&lt;li&gt;We briefly introduced Middleware
&lt;/li&gt;
&lt;li&gt;We viewed a basic example of middleware in use &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that you have been exposed to what a route is, what is sent along with them, and who interacts with the packages sent along each, you can be better identify the bridging elements between your front and backend. For the newbie like me, the ability to read and identify particular parts of the code in projects and documentation will greatly enhance your ability to recognize implementation patterns and employ them on your own in the future.     &lt;/p&gt;

&lt;h4&gt;
  
  
  Happy Coding!!!
&lt;/h4&gt;

&lt;p&gt;Sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ComputerHope.com (&lt;a href="https://www.computerhope.com/jargon/r/route.htm" rel="noopener noreferrer"&gt;https://www.computerhope.com/jargon/r/route.htm&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Express Doc (&lt;a href="https://expressjs.com/en/starter/basic-routing.html" rel="noopener noreferrer"&gt;https://expressjs.com/en/starter/basic-routing.html&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Wikipedia (&lt;a href="https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Microsoft Azure (&lt;a href="https://azure.microsoft.com/en-in/overview/what-is-middleware/" rel="noopener noreferrer"&gt;https://azure.microsoft.com/en-in/overview/what-is-middleware/&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Show * Database MySQL</title>
      <dc:creator>James F. Thomas</dc:creator>
      <pubDate>Mon, 20 Jul 2020 05:07:51 +0000</pubDate>
      <link>https://forem.com/jamesfthomas/show-database-mysql-4cfh</link>
      <guid>https://forem.com/jamesfthomas/show-database-mysql-4cfh</guid>
      <description>&lt;p&gt;&lt;em&gt;A brief overview of Database Management Systems (DBMS) and proper syntax while interacting with MySQL.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Before we can get into the specific meat and bones of interacting with the MySQL database, we must first do a little bit of explaining what a database is and why you would ever need to use one. Once we have established our base understanding of what and why the specific ways to interact with the MySQL database will be far more relevant to the future success of your web design projects. So without any more waisted whitespace let’s &lt;code&gt;describe * databases&lt;/code&gt;.  &lt;/p&gt;

&lt;h3&gt;
  
  
  What is a database &amp;amp;&amp;amp; why use one?
&lt;/h3&gt;

&lt;p&gt;A database is simply a specifically organized assemblage of correlated electronic information that can range from a simple list of items, groups of photos, or even massive anthologies of customer-related information from a large corporation. Typically, the data or information housed within these electronic storage containers is arranged into tables, that then displayed in a column x row format, which makes interaction with these stored values far more economical. A Database is usually employed when multiple processes are accessing and altering data concurrently. The database serves to ensure that these simultaneous inquiries, do not overwrite the changes being performed on either the user or server-side. In the case of someone interacting with database information, this task is called performing a “query” and will require you to use particular verbiage to be successful in the appending or retrieving of desired data.   &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9TUuM1zZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcSDlKrDK6ipE7JFESpLnsf4CYU8C09c7rtZUg%26usqp%3DCAU" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9TUuM1zZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcSDlKrDK6ipE7JFESpLnsf4CYU8C09c7rtZUg%26usqp%3DCAU" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Are all databases the same?
&lt;/h3&gt;

&lt;p&gt;There are serval different types of databases available for use in your next project, but which one you employ is dependent upon not only some personal choices but also upon the project itself. Some considerations to help guide your database management system (DBMS) choice is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Data structure (how it looks and is to be presented)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data consistency (usability of stored data)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data protection (access and encryption of stored data) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessibility/integration (the ability of multiple parties to &lt;br&gt;
access and utilize data) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Interaction efficiency/ scalability (response time to &lt;br&gt;
interactions and ability to change data)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Usability (regards how easy it is to work with DBMS) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Service/implementation costs.    &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you have considered the factors that will guide your choice you can sit down and choose your DBMS type. Here is a list of some of the types of DBMS:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Centralized – database information is stored at a centralized &lt;br&gt;
location &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Distributed – database information is stored across varies sites &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Personal – data collected and stored on personal computers &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;End-user – shared data designed for end-users (i.e. different &lt;br&gt;
department managers)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Commercial – for huge data sets, subject-specific, paid access &lt;br&gt;
through commercial links   &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;NoSQL – for large sets of unstructured data &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Operational – information related to company operations &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Relational – data organized by tables (rows x columns), &lt;br&gt;
predefined category&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cloud – database built for a virtual environment &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Object-oriented – a mixture of object-oriented programing and &lt;br&gt;
relations database structures. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Graph – a type of NoSQL database that uses graph theory to store, &lt;br&gt;
map, and query data relationships &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With all these factors to consider and types of databases to choose from you can see just how complex this decision of which database to use in your next project can be. So, in that light, I have included links to this very influential information below in my sources section in the hope that you can utilize it to make the most appropriate choice for your project shortly. As for this blog project we will choose the relational database management system specifically MySQL and highlight how to interact with the tabled values stored within this database.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I interact with relationally stored data?
&lt;/h3&gt;

&lt;p&gt;Remember earlier we stated that the interaction with stored information was done via something called a query. Well that query, is simply a statement to the DBMS containing keywords along with instructions of how the program should alter data you want to target. Unlike other programing interaction verbiage, you may have encountered in the world of code, I find MySQL syntax extremely readable and more closely resembling human speech than JavaScript or syntax. &lt;/p&gt;

&lt;p&gt;Keywords such as &lt;code&gt;SHOW&lt;/code&gt;, &lt;code&gt;USE&lt;/code&gt;, &lt;code&gt;CREATE&lt;/code&gt;, &lt;code&gt;SELECT&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;, &lt;code&gt;DESCRIBE&lt;/code&gt;, &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;LOAD DATA&lt;/code&gt;, &lt;code&gt;FROM&lt;/code&gt;, &lt;code&gt;WHERE&lt;/code&gt;, &lt;code&gt;IN&lt;/code&gt;, &amp;amp; &lt;code&gt;ORDER BY&lt;/code&gt; are just some of the words and or terms used to interact in specific ways with stored data. Since this is our first encounter with MySQL syntax lets stick with some basic queries and get more advanced in the blogs to come. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a new database on server&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysql&amp;gt; CREATE DATABASE test;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;View existing databases on the server:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysql&amp;gt; SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql    |
| test     |
| tmp      |
+----------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create a new table&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysql&amp;gt; CREATE TABLE animal (name VARCHAR(20), owner VARCHAR(20),
       species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Display the new table you just made&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysql&amp;gt; DESCRIBE animal;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(20) | YES  |     | NULL    |       |
| owner   | varchar(20) | YES  |     | NULL    |       |
| species | varchar(20) | YES  |     | NULL    |       |
| sex     | char(1)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| death   | date        | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add a value to the table you just made&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysql&amp;gt; INSERT INTO pet
       VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These simple queries will get you started arranging information in your database by creating a place for and adding more values to your relational tables. We have only scratched the surface of our journey into databases, so I do hope these 4 examples show just how simple yet readable the MySQL query syntax is to understand and if you choose, put to use.  &lt;/p&gt;

&lt;h4&gt;
  
  
  Topic Recap:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;a database is an organized collection of electronic information &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;many factors influence the type of database you will employ in &lt;br&gt;
your project &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;interaction with stored data is called a query, queries are &lt;br&gt;
strings &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;MySQL query syntax is very human-readable &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Conclusion:
&lt;/h5&gt;

&lt;p&gt;The modern database has evolved into many different choices that will be affected by a multitude of different factors that should be considered before your project beginning. I suggest a little bit of research into some queries into the syntax used in each one before you make your choice. While your free to choose anyone you want, I like the syntax utilized in the RDBMS MySQL for its human readability and structured displays. These may change with greater experience but for now, MySQL is the base I am banking on. With the information highlighted in this blog, I hope to have armed you with all you need to choose the best DBMS for your next project no matter it is big or small.&lt;/p&gt;

&lt;p&gt;Happy Coding!!!     &lt;/p&gt;

&lt;p&gt;Sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;MySQL Docs (&lt;a href="https://dev.mysql.com/doc/refman/8.0/en/what-is-"&gt;https://dev.mysql.com/doc/refman/8.0/en/what-is-&lt;/a&gt; &lt;br&gt;
mysql.html) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Oracle Docs (&lt;a href="https://www.oracle.com/database/what-is-"&gt;https://www.oracle.com/database/what-is-&lt;/a&gt; &lt;br&gt;
database.html)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;“8 Key Considerations When Choosing a DBMS” &lt;br&gt;
(&lt;a href="https://blog.paessler.com/key-considerations-when-choosing-a-"&gt;https://blog.paessler.com/key-considerations-when-choosing-a-&lt;/a&gt; &lt;br&gt;
dbms)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;“Types of Databases” (&lt;a href="https://www.tutorialspoint.com/Types-of-"&gt;https://www.tutorialspoint.com/Types-of-&lt;/a&gt; &lt;br&gt;
databases)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Just How Do React.js Components Cycle Through Life?</title>
      <dc:creator>James F. Thomas</dc:creator>
      <pubDate>Mon, 13 Jul 2020 03:35:08 +0000</pubDate>
      <link>https://forem.com/jamesfthomas/just-how-do-react-js-components-cycle-through-life-6p1</link>
      <guid>https://forem.com/jamesfthomas/just-how-do-react-js-components-cycle-through-life-6p1</guid>
      <description>&lt;h1&gt;
  
  
  The Lifecycle of React.js components!
&lt;/h1&gt;

&lt;p&gt;A &lt;em&gt;“Lifecycle”&lt;/em&gt; in the React.js framework refers to the procedures which correlate to the creation, displaying, monitoring, and removing of a React component. This &lt;em&gt;“Lifecycle”&lt;/em&gt; can be arranged into three phases – Mounting, Updating, Unmounting – with various methods available on the React object that empower a developer to observe and alter the component as desired throughout each section of its life. So, let us take a quick peek at the birth, life, death, and the associated tools for each phase in the &lt;em&gt;lifecycle&lt;/em&gt; of a React component. &lt;/p&gt;

&lt;h2&gt;
  
  
  So how do I bring a component to life?
&lt;/h2&gt;

&lt;p&gt;The birthing of a React.js component comes about in the mounting phase of the lifecycle. While it does refer to the first stage of this component's life it can be broken down one step further as well to include different steps according to the type of component you want to bring to life. Before mounting your component, you must consider the desired tasks you want this component to perform on the page, which will influence your choice to initialize the component with or without state. State is an object that is given to your component via the constructor and allows you to set properties on the component that can be monitored for changes. &lt;br&gt;
The state object is a JS object so accessing and setting those properties is done via dot notation. A stateless component also referred to as presentational or dumb, is labeled this chiefly because these components lack a state object and cannot track changes in user information. The stateless components are used to present or display static (unchanging) information. Stateless components due to one thing hence the names presentational and dumb.            &lt;/p&gt;
&lt;h3&gt;
  
  
  Stateless Components
&lt;/h3&gt;
&lt;h5&gt;
  
  
  1
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Will display basic stateless React.js element

ReactDOM.render(
&amp;lt;h1&amp;gt; Completely Statless Yall &amp;lt;/h1&amp;gt;, 
document.getElementById("htmlLocation")
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h5&gt;
  
  
  2
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Stateless = () =&amp;gt; {
  return (
    &amp;lt;h1&amp;gt;All I Do Is show This Info!&amp;lt;/h1&amp;gt;
  )
 }

//Will display basic stateless functional React.js component 
ReactDOM.render(
&amp;lt;Stateless /&amp;gt;, 
document.getElementById("htmlLocation")
);

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

&lt;/div&gt;

&lt;h5&gt;
  
  
  3
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Stateless extends React.Component {
 constructor(props) {
   super(props)

  }
 render() {
   return(
     &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;{this.props.title}&amp;lt;/h1&amp;gt;
     &amp;lt;/div&amp;gt;
   )
 }
}

var info = {
  title: "I have no state"
} 

//Will display basic stateless React.js class component  
 ReactDOM.render(
  &amp;lt;Stateless title={info.title}/&amp;gt;, 
  document.getElementById("htmlLocation")
  );

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

&lt;/div&gt;

&lt;h3&gt;
  
  
  Statefull Component
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CodeSchools extends React.Component {
  constructor(props) {
    super(props);
    this.state = {school: "Operation Spark"};
  }
  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;Welcome To {this.state.school}&amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

//Will display stateful React.js class component
ReactDOM.render(
  &amp;lt;CodeSchools/&amp;gt;, 
  document.getElementById("htmlLocation")
  );


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

&lt;/div&gt;


&lt;p&gt;React.js elements evolve into components once they begin to accept props object regardless if they have state or not.  There is a guiding rule in react.js, that will affect how you interact with the props object that you pass into your stateful components, which all pertain to purity. No matter if the component you declare is of the functional or class designation it must never alter its own props. Remember that in JS a pure function does not alter its inputs and has the same return value for the same inputs, well the same rules apply to any component in the React.js framework.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// impure function example 

function impure(object, name) {
  object.owner = name;
  return object;
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Side-Information&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In React.js the act of placing your initialized component or element on the DOM is called Mounting and is the second part of the first stage of the lifecycle but is usually the term used to describe both actions. So, in the future, if you come across mounting remember you must first create something before it can be placed.  For the mounting Phase to be carried out React.js has multiple built-it methods that can be employed to get your component onto the screen but only one is absolutely necessary, and that is rendered (). The render method is the portion of your component that will actually output HTML elements to the DOM, it will examine the components state, and then return elements, fragments(a group of child elements like a list), portals (render components to different parent component), or basic JS data values (strings &amp;amp;&amp;amp; numbers). There are additional Mounting phase methods such as componentDidMount (allows you preset JS statements to alter the state of the object once it has mounted), but to best illustrate the key actions of this phase I will stick to highlighting the render() method only.   &lt;/p&gt;

&lt;h2&gt;
  
  
  So, what happens after my component is mounted?
&lt;/h2&gt;

&lt;p&gt;After the mandatory render method has placed your React.js component onto the DOM, you have officially entered the Updating phase of the component’s lifecycle. During the updating phase, the component will be rendered repeatedly according to either a change in the parent components props object or a change in the components local state. These changes represent interactions with the user interface or updating of information from a server that is being visually represented on the DOM. Methods like setState() allow us to interject event listeners into our components that will change the state properties in response to desired events. One common example would be changing DOM information in response to a user click.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CodeSchools extends React.Component {
  constructor(props) {
    super(props);
    this.state = {school: "Operation Spark"};
  }

/*
created new function which uses setState() method to 
change school name property of state object
*/
  changeSchool = () =&amp;gt; {
    this.setState({school: "Hack Reactor"})
  }

  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;Welcome To {this.state.school}&amp;lt;/h1&amp;gt;
&amp;lt;!--added HTML button &amp;amp; click listener responds to user actions--&amp;gt;
        &amp;lt;button type="button" onClick={this.changeSchool}&amp;gt;Change Your School&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

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

&lt;/div&gt;



&lt;p&gt;There are multiple methods that will allow you to monitor and subsequently change your component in response to that input but setSet() is very simple far simpler for a beginner to grasp in theory and implementation so I kept it simple in the above example. If you just need to know more about component updating you can go to &lt;a href="https://reactjs.org/"&gt;https://reactjs.org/&lt;/a&gt; and read up on the methods shouldComponentUpdate(),  ComponentWillUpdate(), and componentDidUpdate() to satisfy your curiosity.      &lt;/p&gt;

&lt;h2&gt;
  
  
  It’s up and updating, so now what?
&lt;/h2&gt;

&lt;p&gt;Once the component is up and responding to user interactions, we can move along into the final lifecycle phase which encompasses removing components from the DOM and so is appropriately named in direct opposition to our first phase. We are now in the Unmounting phase of the react.js component lifecycle which is home to one solitary built-in method, componentWillUnmount(). Following the appropriate naming of the phase, this method’s only job is to remove a component from the DOM in preparation for being destroyed. Once a components instance is taken off the DOM or unmounted it will never be rendered again. In light of this fact componentWillUnmount() should not utilize the updating phase method of setSet() because the component will never be used again and therefore will have no state to set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//This function will alert you right before the component is removed and destroyed 

componentWillUnmount() {
  console.log('will unmount');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ComponentWillUnmount() is the final function to be called in a chain of events prior to your component being removed from the DOM. It can be used to simply alert you of the component’s removal or be set to do away with or cancel any elements as well as timers set in the mounting phase by built-in method like CompentWillMount() or CompentDidMount().  Since we are keeping this exploration of the lifecycle phases as a basic overview, I feel simply alerting you to the existence of the method is enough to properly demonstrate each phase and some of the methods employed in each one.   &lt;/p&gt;

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

&lt;p&gt;The lifecycle of any component in React.js is split into 3 distinct phases which are all eloquently named for your ease of recall. If you think about them logically you will never miss one in the set when asked. First, create it and put it on the screen. Second, monitor and change accordingly. Third and final, remove and destroy when done.    &lt;/p&gt;

&lt;p&gt;Topic Recap: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Components are not components until the accept props&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Components need to be pure not altering inputs, returning the &lt;br&gt;
same value with same input&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The component Lifecycle is composed of three main parts: &lt;br&gt;
Mounting, Updating, Unmounting&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unmounting has only 1 built-in method for that lifecycle phase &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If this overview was not enough to elucidate the cycles of a react.js component’s life for you, check out the links below for a deeper dive into aspects and methods that are coupled with each stage. Until next time and topic, Happy coding!&lt;/p&gt;

&lt;p&gt;Sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;W3schools.com (&lt;a href="https://www.w3schools.com/react/react_lifecycle.asp"&gt;https://www.w3schools.com/react/react_lifecycle.asp&lt;/a&gt;) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Edmond Atto (&lt;a href="https://medium.com/the-andela-way/understanding-the-fundamentals-of-state-in-react-79c711be677f"&gt;https://medium.com/the-andela-way/understanding-the-fundamentals-of-state-in-react-79c711be677f&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React.js docs (&lt;a href="https://reactjs.org/docs/components-and-props.html"&gt;https://reactjs.org/docs/components-and-props.html&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;tutorialspoint.com (&lt;a href="https://www.tutorialspoint.com/react-js"&gt;https://www.tutorialspoint.com/react-js&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>introduction</category>
    </item>
    <item>
      <title>Your Gonna Need Some Scripts To Go With That JavaScript!</title>
      <dc:creator>James F. Thomas</dc:creator>
      <pubDate>Mon, 06 Jul 2020 01:53:06 +0000</pubDate>
      <link>https://forem.com/jamesfthomas/your-gonna-need-some-scripts-to-go-with-that-javascript-2i5a</link>
      <guid>https://forem.com/jamesfthomas/your-gonna-need-some-scripts-to-go-with-that-javascript-2i5a</guid>
      <description>&lt;p&gt;If you're new to programming like me, then you may only have minimal experience with the three pillars of web design, HTML, CSS, and JavaScript or JS. For your skills to advance to the next level you must understand how these pillars support the foundation on which your web sites and applications will be built. This blog will quickly highlight the connection between HTML and JS, by explaining the placement of the HTML element known as the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Why you want JS in your HTML?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--exGOLHlT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://heimdalsecurity.com/blog/wp-content/uploads/JavaScript-usage-in-websites-300x300.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--exGOLHlT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://heimdalsecurity.com/blog/wp-content/uploads/JavaScript-usage-in-websites-300x300.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JS is a scripting language that is used to provide interactivity to your formerly static webpages. This means that the page you build will have programmed actions or reactions to client-side interactions with page components. Even simpler, if it moves, updates, scrolls, toggles, turns, changes color, or appears out of nowhere chances are JS is behind the scenes getting work done. So here the takeaway is, JS makes your page and apps move.  &lt;/p&gt;

&lt;h2&gt;
  
  
  How do You get it in there?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G5tGX7Dg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.pngio.com/about-us-akron-fossils-science-center-2-branches-tree-png-530_322.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G5tGX7Dg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://img.pngio.com/about-us-akron-fossils-science-center-2-branches-tree-png-530_322.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although there are two branches there is only one root. No matter how much or exactly where you want to put the JS into your HTML file, it will always have to go inside of a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag. The script tag is a dedicated HTML element that is used to run executable code for client-side interactions. When you want your page to react-to or retrieve-from the person visiting the site or using your app, the code that will enable this ability will be embedded inside the script tag like so &lt;code&gt;&amp;lt;script&amp;gt;executable code &amp;lt;/script&amp;gt;&lt;/code&gt;.      &lt;/p&gt;

&lt;h4&gt;
  
  
  script tag example
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- This will display an alert pop up with the current date inside the string value --&amp;gt;
&amp;lt;script&amp;gt;
    let today = new Date();
    alert(`Today is ${today}, and everything is all good!`)
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  So, we just put the JS in the script tag?
&lt;/h2&gt;

&lt;p&gt;Well yes but no. The &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag is your link between the two languages (HTML &amp;amp;&amp;amp; JS) but there are two actual ways in which the code can be executed. You can either write the code you want to interact with HTML elements right into your code, or you can write all the code you want to interact with your page elements in a separate file and connect them to your HTML page via a link. These methods are knowns as &lt;em&gt;in-line&lt;/em&gt; vs &lt;em&gt;cached&lt;/em&gt;. Choosing the cached option, a separate file containing all your JS code would probably be the best if you plan to run your JS code on multiple pages, as it would save you from having to duplicate the inline scripts for each page that you create. &lt;/p&gt;

&lt;h4&gt;
  
  
  Inline
&lt;/h4&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 lang="en-US"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;Code Examples&amp;lt;/title&amp;gt;

  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt; 
  &amp;lt;!-- This inline script will create a &amp;lt;H1&amp;gt; element on top of page when loaded --&amp;gt;
  &amp;lt;script&amp;gt;
      document.write("&amp;lt;h1&amp;gt;Hello World!&amp;lt;/h1&amp;gt;")
  &amp;lt;/script&amp;gt;


&amp;lt;/body&amp;gt;  
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Cached
&lt;/h4&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 lang="en-US"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;Code Examples&amp;lt;/title&amp;gt;
    &amp;lt;!-- This is an example of linking HTML to extrnal file --&amp;gt;
  &amp;lt;script src="myJavascriptFile.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt; 


&amp;lt;/body&amp;gt;  
&amp;lt;/html&amp;gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Where is the best place to put my scripts and why?
&lt;/h2&gt;

&lt;p&gt;Making the choice of where to place your script tag is always up to the programmer but knowing some background information on how the code interacts with the page elements will help you make an informed decision. The most traditional placement of the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag is within the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; section of the HTML document which is at the top of the document preceding the body. Following our most traditional spot within the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; section, would be the placement of the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag as the last element in the body section prior to the closing &lt;code&gt;&amp;lt;/body&amp;gt;&lt;/code&gt; tag.  If your page is not intensely interactive or you wish to only add small dynamic features to some of its components, then adding your inline JS at the top or bottom of the page is not going to noticeably affect the speed of your page load.  &lt;/p&gt;

&lt;h4&gt;
  
  
  Script Tag at Top of File
&lt;/h4&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 lang="en-US"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;Code Examples&amp;lt;/title&amp;gt;
  &amp;lt;!-- Placing the script at top of your JS file--&amp;gt;
  &amp;lt;script src="myJavascriptFile.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt; 

&amp;lt;/body&amp;gt;  
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Script Tag at Bottom of File
&lt;/h4&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 lang="en-US"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;Code Examples&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt; 

    &amp;lt;!-- Placing the script at bottom of your JS file--&amp;gt;
    &amp;lt;script src="myJavascriptFile.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;  
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How JS affects the speed of your load is due to the fact that the DOM will pause loading HTML elements when it encounters JS code in the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag. It will then resume downloading the remaining page content once the JS code has been interpreted. Of course, if you have numerous elements on the page, each with their own &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;’s this will drastically slow the loading of the page and make for a dull user experience. At this point, it is best practice to use the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; to link your page to an external file containing all JS code intended for your site. Another small caveat to the placing the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; at the bottom of the file is to avoid loading errors. JS code can NOT interact with HTML elements if they are not loaded, so again placing your &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag at the end of the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; will help you avoid these communication breakdowns.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d1mvYr8Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.ogroup.com.au/wp-content/uploads/2016/04/communication-cartoon.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d1mvYr8Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.ogroup.com.au/wp-content/uploads/2016/04/communication-cartoon.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One Quick fix for this delay in page parsing, or loading of page elements, would be to add the &lt;strong&gt;defer&lt;/strong&gt; attribute to the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag. When the browser encounters a tag with this attribute it will wait to execute the code until the document has been fully loaded.     &lt;/p&gt;

&lt;h4&gt;
  
  
  Defer attribute
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- Add defer attribute to signal that script content shouldn't be run unitl page loded --&amp;gt;
    &amp;lt;script defer&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, remember: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;JS code can be entered into your projects directly =&amp;gt; &lt;em&gt;Inline&lt;/em&gt; or indirectly =&amp;gt; &lt;em&gt;Cached&lt;/em&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags are the route to including JS in your HTML files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; placement affects browser page parsing, so place mindfully&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When in doubt Cache the code out&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although it is a simple choice, it can be one that vastly affects user experience on your page so make sure to choose your placement wisely. Your goal should be to have as smooth and short of load time as possible to ensure those users continue to come back and tell all their friends about the wonderful thing you have created. Thank you so much for your time, I hope that you learned something, and now that you know where your JS should go, knowing is half the battle!!! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K9tluKRW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.pinimg.com/originals/78/c5/2f/78c52f879cb87f994e354d14ddfbbbb4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K9tluKRW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.pinimg.com/originals/78/c5/2f/78c52f879cb87f994e354d14ddfbbbb4.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Happy Coding!!!!
&lt;/h2&gt;

&lt;p&gt;Sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/"&gt;https://www.w3schools.com/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/"&gt;https://developer.mozilla.org/en-US/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/javascript/javascript_placement.htm"&gt;https://www.tutorialspoint.com/javascript/javascript_placement.htm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Javascript &amp;amp; Jquery interactive front-end web development by Jon Duckett&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>newbie</category>
    </item>
    <item>
      <title>Scopes &amp; Closures - A Metaphor </title>
      <dc:creator>James F. Thomas</dc:creator>
      <pubDate>Sun, 28 Jun 2020 19:25:19 +0000</pubDate>
      <link>https://forem.com/jamesfthomas/scopes-closures-a-metaphor-40mk</link>
      <guid>https://forem.com/jamesfthomas/scopes-closures-a-metaphor-40mk</guid>
      <description>&lt;p&gt;Introduction&lt;/p&gt;

&lt;p&gt;When I first began learning the ins and outs of basic coding, which I am still in the process of doing, the concept of “scopes &amp;amp; closures” was one that took me a minute to understand. I loved declaring variables, assigning values, and making things appear with console.log() statements. I truly felt like a wizard, creating my incantations, and watching them play out before my eyes. But once the tasks began to include loops and functions the concept of scopes appeared, and all the while threw me for a loop. So, I came up with a metaphor that helped me scope out the situation.     &lt;/p&gt;

&lt;p&gt;Scopes&lt;/p&gt;

&lt;p&gt;What exactly is a scope? &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qv4unIkF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcRWwv1ibbalTNTl6CeC6TbCqCs_QIq50YSBTQ%26usqp%3DCAU" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qv4unIkF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcRWwv1ibbalTNTl6CeC6TbCqCs_QIq50YSBTQ%26usqp%3DCAU" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More technical definitions of scope involve wording like, “the context of execution” (MDN) or “the level of accessibility” (W3Schools), but for me, the easiest way to explain scope to a newbie would be “the area of your code in which the variable or value was created”. I found this to be a simple and visually applicable definition that allowed me to start really grasping scopes and how they build upon or within one another. Being able to absorb the concept of scopes is the only way to move into closures, so let’s first expand a bit more on the types of scopes a newbie will need to comprehend.&lt;/p&gt;

&lt;p&gt;Scopes come in two varieties:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EafJPgvG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcTxrJMCsmnBRQDtV8VCcJwabpdB_2gtEOM3tw%26usqp%3DCAU" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EafJPgvG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcTxrJMCsmnBRQDtV8VCcJwabpdB_2gtEOM3tw%26usqp%3DCAU" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first and simplest to understand is the “global”. To the beginner “global scope” will encompass any variables or functions in your code that are not inside another function or conditional block. For my newbie’s let’s get visual, look at your code and if it’s not housed inside anything else, that would be the global scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// globally declared items 

let name = "James F. Thomas";

let age = 37;

let globalFunc = () =&amp;gt;{
  console.log("My First Blog Post")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, there are instances where an undeclared variable housed within a function will be automatically turned into a global variable, but that is beyond the basic overview of this blog. Remember we are keeping to the lowest level of abstraction for this one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//undeclared variable in function

let globalExample = () =&amp;gt;{
  message = "My First Blog Post";
  console.log(message);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second scope type would be known as “local”.  A “local scope” is generated when you initialize a variable or a function inside of another function, conditional statement, or loop code block. Placing the variable or value inside the curly braces of those previously listed code components will render them inaccessible or invisible to any components not in the same or local scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//local scoped variables

let outerFunc = () =&amp;gt; {
  let localVariable = "I'm local"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Locally scoped variables or values are considered children of the global scope in which they were created. It is with that relationship they will retain access to any globally located code components and be able to use them as desired. The technical term for this ability is “lexical scope”. This access continues with each nested function but is only one way in nature. So that means as we go from parent to child to grandchild each inner level can access outwardly but not vice versus. Kids access parent values, but not the other way around, and grandkids of course can take from both.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = "James F. Thomas";

let globalFunc = () =&amp;gt;{
  console.log(name)
}

globalFunc(); //prints "James F. Thomas"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This child-parent interaction or “lexical” relation between local and parent code components is just what we needed to understand to find closure. Trust me that will be funny in a few lines.    &lt;/p&gt;

&lt;p&gt;Closures:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eT-UeJAW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.pinimg.com/originals/c9/2e/51/c92e513c98715c55dd1c4523f96888ec.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eT-UeJAW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.pinimg.com/originals/c9/2e/51/c92e513c98715c55dd1c4523f96888ec.jpg" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A “closure” describes the access a child element has to values housed in its parent or “lexical” environment. A closure is created when a function is housed within another and refers to its parent variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let outerFunc = () =&amp;gt;{

  let message = "I'm local"

  return function childFunc(){
    console.log(message);
  }; 

};

outerFunc()(); //prints "I'm local"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok cool, so why is that important? Those variables and values not within the closure will only exist at runtime, so the closure or inner function will allow for future access even after the outer function has returned. So basically, a closure creates a type of barrier around the values it has accesses to and then allows you to store access to them even after the outer function has been run.   &lt;/p&gt;

&lt;p&gt;Conclusion:&lt;/p&gt;

&lt;p&gt;Even though that all makes sense to me NOW, I had to come up with a more relatable explanation for these concepts during my first encounter with this topic. &lt;/p&gt;

&lt;p&gt;So, in my head: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K9pfOJcg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcS10r1w4-jEHwygLYn2QkBInxywGqp-vw8_Uw%26usqp%3DCAU" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K9pfOJcg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcS10r1w4-jEHwygLYn2QkBInxywGqp-vw8_Uw%26usqp%3DCAU" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The global scope is an entire city, and any variable or function declared in it is a city service, and city services are available to all residents. Local scopes are gated communities within the city, with their own services, only available community to residents. Some subdivisions are huge and can have other smaller gated subdivisions inside of them, with their own services. Residents can of course leave their communities and access city services like the bus, but the bus does not run into any of these gated communities. For closures, I thought about the gated communities having their own service warehouses, that supplied goods and kept records, but the only way for non-residents to receive either was to make calls from the distribution center located in the city downtown. Lucky for us they are open 24 hours a day.  &lt;/p&gt;

&lt;p&gt;Yeah I know it’s silly but it worked for me, and maybe it will work for you. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A4bpOBVZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcSjBNZFAWv71kNfhY7mwf_tsOtItYbk7pPHwQ%26usqp%3DCAU" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A4bpOBVZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcSjBNZFAWv71kNfhY7mwf_tsOtItYbk7pPHwQ%26usqp%3DCAU" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy Coding!!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d06y5hFk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcSoUx80_emb8H7K1SXPrGIpTfzIK2YSyn3qBA%26usqp%3DCAU" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d06y5hFk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcSoUx80_emb8H7K1SXPrGIpTfzIK2YSyn3qBA%26usqp%3DCAU" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
