<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ana on Medium]]></title>
        <description><![CDATA[Stories by Ana on Medium]]></description>
        <link>https://medium.com/@ana_8826?source=rss-84b99b09944e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*iCEHdPnSwmYVXARm_9WltA.jpeg</url>
            <title>Stories by Ana on Medium</title>
            <link>https://medium.com/@ana_8826?source=rss-84b99b09944e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 04:39:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ana_8826/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Guide to Authentication using Passport-Local in React]]></title>
            <link>https://levelup.gitconnected.com/a-guide-to-authentication-using-passport-local-in-react-f5b3db06d4d0?source=rss-84b99b09944e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5b3db06d4d0</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <category><![CDATA[mern]]></category>
            <category><![CDATA[passport]]></category>
            <dc:creator><![CDATA[Ana]]></dc:creator>
            <pubDate>Mon, 15 Jun 2020 14:06:01 GMT</pubDate>
            <atom:updated>2020-06-21T13:39:10.156Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Guide to Authentication in React</h3><h4>How I learned authentication in React</h4><h4>Introduction</h4><p>I began this post with the intent to write a definitive guide on using Passport-Local in React because the resources out there were sparse on that specific topic. React is a library that builds user interfaces while authentication happens between the browser and the server (and a database in our case). <strong>So, lack of resources is due to the topics not being directly related.</strong></p><p>To understand the topic more deeply, I built my own Authentication method using cookies and bcrypt. I was confused on how the different elements of a MERN (MySQL, Express, React, NodeJS) application interacted with each other in the context of authentication. I addition to covering registration, login, hashing passwords, and using cookies, I review:</p><ul><li>How to make sure that CRUD takes into consideration the identity of the authenticated user so that they can only see, create, delete, update their own information.</li><li>How to make sure that only authenticated users have access to certain pages.</li></ul><h4>Welcome to A Guide on Authentication!</h4><p>Below is a summary of my authentication approach:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sOscIqnRarI1OCYRZwQX-g.png" /><figcaption>Summary of my authentication approach</figcaption></figure><p>In React, information can be shared across components using Global State and <a href="https://reactjs.org/docs/context.html#when-to-use-context">Context</a>. Either can be a valuable tool for determining what the user sees; however, we will depend on good ole fashioned cookies for authenticating users.</p><h3>Step by Step on Authenticating Users in MERN app</h3><p>Before we dive in, let’s clarify some definitions! Registration is essentially a request to create a new user record. Login is a request to be given access. Access is verified by checking if the user exists in the database and if the password provided matches the password in the record.</p><p>Think of registration as being added to the list to an exclusive club and login as showing up and having to show your id to prove that you are you.</p><h4>First, we need to build a few paths</h4><p>Start by creating the routes that the user will interact with directly — I chose to create two pages, one for login and one for registration or signup. In the App.js folder, I created the routes and referenced the pages I wanted to render.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*85VY8kXgV53iX9p5s4Z3cg.png" /><figcaption>Creating login and register routes in React</figcaption></figure><p>On the server side, I created one route <strong>/api/user</strong> to handle registration and login.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tYq-vayEj-PadBUoStAlOw.png" /><figcaption>API calls on Server side to actually login and register user</figcaption></figure><p>The two pieces above can all be created on a single server file or split out into multiple files as I have done so. The concepts are still the same.</p><h3>Registering A New User</h3><p>When a user registers, they fill out a form with information such as name, username/email and password. For our application, we will use email as the primary identifier for our users.</p><p>…but should the password be stored in some special way?</p><p>I’m glad you thought of that! Yes. One way to do that is to use <a href="https://www.npmjs.com/package/bcrypt"><strong>bcrypt</strong></a><strong>, </strong>a library that hashes passwords. In plain speak, bcrypt maps the password into something unrecognizable. In fact, I created over 20 users with the password “password” and none of the outputted text was similar in any recognizable way. So, server side, before actually creating the User record in your database, hash the password. That way, if anyone hacks your database, they won’t be able to get any actual password information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5QESohb1ZMJiKqF9urBqhw.png" /><figcaption>User Registration shown at the database level</figcaption></figure><p>An async function is used with bcrypt to ensure that the password is hashed before the User record is created. After creation, the User Id will be tied to a session record. Since the method is similar for registering and logging in, I’ll explain later.</p><h3>Logging in An Existing User</h3><p>When a user signs in, they fill out a form with their username/email and password. The values are bundled in the API request.</p><p>Server side, we are first checking whether the user exists and then checking if the password is valid. Note that the comparison isn’t a triple equal — The truth is that <em>we</em> can’t match them directly but the bcrypt compare function can.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*S4aDJMIuHNvhvVJQVl0U9g.png" /><figcaption>User login shows at the database level</figcaption></figure><p>Great, we’ve technically authenticated a user! Now, we need to make it <em>mean </em>something:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*iy7eklvBgmHnjvgO.gif" /></figure><h3>Using Cookies to Restrict Access</h3><h4>Restricting Access based on authenticated User information</h4><p>Let’s visit the database to understand how information interacts: User data (name, email, hashed password) is stored in a User Table. A second table stores User-Sessions by tracking Users and Active Session-Cookies relations. Every time we give a User a cookie, we store the cookie value and the User’s id in this User-Session table. Users have a one-to-many relationship with User-Sessions meaning that a single User could technically be authenticated on multiple browsers at the same time — think ipad, iphone and laptop.</p><h3>How We Will Use Cookies</h3><p>Now it’s cookie time! <a href="https://en.wikipedia.org/wiki/HTTP_cookie">Cookies</a> are a piece of data stored by a browser and sent along with every request.</p><p>When the user has been authenticated, a cookie will be created. Since cookies are included in every request, the server will be able to use Session Cookies to confirm the User’s identity. With every CRUD request, we will first check the Session Cookies, and use the User-Session table to match the cookie back to a User and returning the User’s identity if the relationship exists. Once the User signs out, the cookie is deleted and the User-Session record is deleted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*ScWBGarg_MVbreP_.gif" /><figcaption>Illustration of how Cookies are set</figcaption></figure><p>Piraveena beautifully outlined relationship the between browser and server <a href="https://medium.com/@piraveenaparalogarajah/sessions-and-cookies-2c0919552f29">in this article</a>. Check it out for your reference.</p><h3>Creating a Cookie is the Easiest Part</h3><p>It’s simple to create a cookie but it’s difficult to make it unique enough that it cannot be hacked. Welcome back bcrypt!</p><pre>let cookievalue = bcrypt.hash(&quot;secretword&quot;,10)</pre><p>To set the cookie, we just need to tie it to a response</p><pre>res.cookie(&quot;cookiename&quot;, cookievalue)</pre><p>In your app, you can check the application tab under Inspect to see if this worked correctly. Here is how you can tie session cookie creation to the login process:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*iRjdu2d_xlll2-5p3L8ppg.png" /></figure><p>You can take a similar approach for registration but rather than waiting to confirm that the password exists, you need to wait for the user to be created in the database. Grab the User Id from the response to create a User-Session record. Client side won’t receive a response until the User Session has been created.</p><h3>Deleting Cookies is just as simple</h3><p>When a user logs out, we remove the cookies in the browser and delete the User-Session relationship from our database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*83gTqgCsK6rg_UAZ.gif" /></figure><p>Cookies are sent with every request. To access the cookie that you specifically set you can use a parser. I chose to parse the cookie myself to better understand how a parse may work. Note the user of decodeURIComponent to undo the URL encoding that happened when the cookie was set.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*50kJGWG4I0VZ8sF2seX87g.png" /><figcaption>How to delete a user and parse out a cookie</figcaption></figure><h3>CRUD is the tricky part</h3><p>The most important thing we’ve learned about cookies is that they are appended to all requests. That means that on the client side, there aren’t additional pieces of information we need to append or provide in order to make API request.</p><p>The flow is as follows:</p><ul><li>parse the cookies to get your specific cookie</li><li>use the value of the cookie to make a request to the User-Session table</li><li>if the result is null, or no relationship exists, return nothing</li><li>if there is a match, return the User Id and then use that id to fulfill the request that the user originally made</li></ul><p>Every time the user makes a request, we are checking their credentials but doing it in a way so subtle that the client doesn’t know.</p><p>This method is effective because if the user logs out their authentication — the record on the user session table — will be wiped and they will be asked to log in again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*qXt1ulYnDauuDsVj.gif" /><figcaption>User Session Needs to Exist For User To Have Access</figcaption></figure><h3>What About Context?</h3><p>Since we are building a MERN app, Context is a tool available to share information across React components. Even if you chose to use User Context for tracking whether there is an authenticated user logged in, you still need to validate the user with cookies. Context will still be valuable to restrict what pages are viewable to the user at a given time. For example, set context that checks for cookies every time the page loads and if there is a cookie, you could show the sign out button because it means a user is logged in.</p><p>Since Context is on the Client side, we don’t want to depend on it as a method for confirming the user’s identity.</p><h3>What’s Next?</h3><p>Now that you have learned how authentication works, you can explore additional resources that teach you how to use Passport, indexedDB, cookie-parse, firebase-authentication, etc. The world is your oyster.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5b3db06d4d0" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/a-guide-to-authentication-using-passport-local-in-react-f5b3db06d4d0">A Guide to Authentication using Passport-Local in React</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Crash CORS: Cookies, Servers and Webpages]]></title>
            <link>https://medium.com/swlh/crash-cors-cookies-servers-and-webpages-a2435c623f95?source=rss-84b99b09944e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2435c623f95</guid>
            <category><![CDATA[cookies]]></category>
            <category><![CDATA[cors]]></category>
            <category><![CDATA[servers]]></category>
            <category><![CDATA[browsers]]></category>
            <dc:creator><![CDATA[Ana]]></dc:creator>
            <pubDate>Sun, 03 May 2020 00:47:40 GMT</pubDate>
            <atom:updated>2020-05-16T00:36:46.619Z</atom:updated>
            <content:encoded><![CDATA[<h3>Background</h3><p>I discovered Cross-Origin Resource Sharing (CORS) last week when I tried to make a request to the Github Jobs API from the client-side and got this error message:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hE4dABw6Karc5Bv6" /><figcaption>The error was replicated using google.com from my localhost:3000</figcaption></figure><h3>What is it?</h3><p>According to Mozilla(<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS">MDN</a>): “<strong>Cross-Origin Resource Sharing</strong> (<a href="https://developer.mozilla.org/en-US/docs/Glossary/CORS">CORS</a>) is a mechanism that uses additional<a href="https://developer.mozilla.org/en-US/docs/Glossary/HTTP"> HTTP</a> headers to tell browsers to give a web application running at one<a href="https://developer.mozilla.org/en-US/docs/Glossary/origin"> origin</a>, access to selected resources from a different origin. A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, or port) from its own.”</p><p>Meaning, if a webpage tries to make a request to another webpage, there are a set of instructions that must be followed in order for that request to yield the intended response. When I ran a request from localhost:3000 to access data on another webpage, my request failed because Github Jobs did not explicitly give me permission to make a request to their server. I moved my client side request to server side and this resolved my issue so I moved on with the project.</p><h4>…isn’t it strange that servers can make a request to another server but websites can’t?</h4><p>I certainly thought so!</p><p>First: a little house keeping. You can see the code that I used to bypass the CORS issue below and then we can move into learning more about cookies, servers and webpages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*73T-i7BolrzAmBHi" /><figcaption>Server side code to make a to another website</figcaption></figure><h3>A Few Resolved Misconceptions</h3><ul><li><strong>CORS relates to Browsers, not webpages in themselves:</strong> I thought that GitHub Jobs API was rejecting my request but GitHub never even got my request because it was my Browser that did the blocking.</li><li><strong>Webpages are making requests to other servers: </strong>I thought that the webpage was making a request to another webpage but it’s actually making a request to the server.</li><li><strong>There is a governing body setting the standards for the web called </strong><a href="https://www.w3.org/"><strong>W3C</strong></a>. One of the standards is CORS. W3C is different from <a href="https://www.ietf.org/about/">IETF</a>, which governs the internet. Often, web and internet are used interchangeably but should not be since the web is a subset of the internet</li></ul><h3>The Basics</h3><p>Let’s lay out a diagram to further clarify CORS. Let’s say that webpage 1 is mybank.com and when I access mybank.com and make requests, those go to the mybank server. That seems normal and makes sense. Now, let’s say there is a second website called myAirlinePoints.com with a server called myAirlinePoints server. From mybank.com, I cannot make requests to myAirlinePoints server. Not because myAirlinePoints Server is blocking it but because my browser won’t allow it. These kinds of requests are blocked by default! This is CORS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*M8zt5wjvTC1eTphO" /></figure><h3>Why does CORS exist?</h3><p>Because of cookies. <a href="https://en.wikipedia.org/wiki/HTTP_cookie">Cookies </a>are a piece of data stored by a browser that contain user’s information for a determinate or indeterminate amount of time. Cookies have a lot of valuable uses in the web, including authenticating users. Each user’s browser stores cookies for each webpage they are in. If you go to your cookies now: right click: “inspect” -&gt; application -&gt; cookies -&gt; Medium, you will see a lot of what seems like garbled nonsense. You see Medium cookies because you are in a medium page. If you move on to other pages, you will see the cookies for those pages.</p><p>As I update my information, or make requests to the server in Medium.com, the cookies are used for the different CRUD requests that I am making. When I request a change to the server, my browser will look for cookies to include in the request and will append those to my request. When my session ends, hopefully, the cookies that were authenticating me to the browser will be wiped. Then, when I come back for another session and login, a new set of authentication cookies will be generated, confirming my identity to the server.</p><p>To answer why CORS exists, let’s consider a world where webpages can make requests to other servers. If I am logged into mybank.com, the browser will have a cookie stored with some authentication values that are used when I view balance, request a transfer, etc. If on my second tab, I have sketchy-site.com, that site could technically make a request to mybank.com using my cookies. The bank server doesn’t know where requests come from — whether it’s other servers or a webpage so as long as the authentication code is appended to requests to create, Update, Delete and View, they could execute. To avoid this situation, CORS sets the default that websites cannot make requests to other servers without explicit permission.</p><h3>What does CORS look like in practice?</h3><p>There are many use cases for allowing one website to access data on another server. Take the example of an airline credit card issued through your bank. When I log into my airline, I want to be able to see how many points I have accumulated through my credit card purchases that are now available for my use on the credit card. With the use of head request, my bank will tell the browser that the airline has permission to make a request to the browser and will allow the airline to make the request. Websites explicitly tell the browsers which external webpages can make requests.</p><h3>Conclusion</h3><p>Finally to answer the question: Why is it that servers can make requests to other servers but websites can’t? It seems to have more to do with keeping other websites from making requests on your behalf when you don’t even know it’s happening. This must mean that servers have a different way of authenticating access to data when the request is being made from another server.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2435c623f95" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/crash-cors-cookies-servers-and-webpages-a2435c623f95">Crash CORS: Cookies, Servers and Webpages</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Firebase Realtime Database Setup for Beginners]]></title>
            <link>https://medium.com/@ana_8826/firebase-realtime-database-setup-for-beginners-58da8dd0b8a2?source=rss-84b99b09944e------2</link>
            <guid isPermaLink="false">https://medium.com/p/58da8dd0b8a2</guid>
            <category><![CDATA[firebase-database]]></category>
            <dc:creator><![CDATA[Ana]]></dc:creator>
            <pubDate>Mon, 06 Apr 2020 03:53:42 GMT</pubDate>
            <atom:updated>2020-04-06T05:12:10.636Z</atom:updated>
            <content:encoded><![CDATA[<p>Last week, I taught myself how to use Firebase Realtime Database. Being able to generate data realtime and have that data available to other users is a huge step up from storing data in local storage. This is probably where I should share that I’m on my 4th week of a Full Stack Developer Program at the UC Berkeley Extension school.</p><p>A little background about Firebase before we start: Firebase is a mobile and web development platform that was founded in 2011 and later acquired by Google. When you go to their website, you’ll notice 19+ products available to users. I was referred to Firebase Realtime Database less than a week ago. I am not a master in Firebase but I have acquired the skills necessary to store information and that’s what I will share with you to encourage you to try and build your own database.</p><h3>Step-by-Step</h3><ol><li>Create a project in Firebase, let’s call it Medium Post. Ignore the request to enable Google Analytics. You are learning one big skill today, no need for the extra distractions.</li><li>Create an application under the Medium Post Project. This step can be a bit confusing because the “Add an app” section looks like a logo. Select “Web Application” and create an application. Note that you’ll have access to all Firebase products.</li><li>Copy the Firebase SDK snippet and copy it to your own code. You’ll notice a comment TODO to add SDKs for firebase products that you want to use. Below is the SDK for the firebase database product that we will be using.</li></ol><pre>&lt;!-- The core Firebase JS SDK is always required and must be listed first --&gt;</pre><pre>&lt;script src=&quot;https://www.gstatic.com/firebasejs/7.13.1/firebase-app.js&quot;&gt;&lt;/script&gt;</pre><pre>&lt;!-- TODO: Add SDKs for Firebase products that you want to use<br>https://firebase.google.com/docs/web/setup#available-libraries --&gt;</pre><pre>&lt;script src=&quot;https://www.gstatic.com/firebasejs/7.13.1/firebase-database.js&quot;&gt;&lt;/script&gt;</pre><p>4. The remaining code elements do not need to be changed:</p><ul><li>firebaseConfig is an object that houses your configuration values including API keys, database url etc.</li><li>The initializeApp method will take the firebaseConfig object as a parameter to initialize the app.</li></ul><p>5. To initialize your database, you need to use the database method and store the response in a variable:</p><pre>var database = firebase.database()</pre><p>You now have a connection between Firebase and your program and the critical next question is how you want to organize your data. As a data person by trade, this is a question close to my heart since your data strategy will determine how you can use data in the future. As a builder, now I see that it has implications on how you create data elements on Firebase and at what level you retrieve information.</p><p>More on that in the next post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58da8dd0b8a2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>