<?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 Austin Rhoads on Medium]]></title>
        <description><![CDATA[Stories by Austin Rhoads on Medium]]></description>
        <link>https://medium.com/@austinrhoads?source=rss-11e116b029d8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*swKDAZ_jZLs2GhPTR0d31g.jpeg</url>
            <title>Stories by Austin Rhoads on Medium</title>
            <link>https://medium.com/@austinrhoads?source=rss-11e116b029d8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 21:21:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@austinrhoads/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[Using PostgreSQL in Rails]]></title>
            <link>https://levelup.gitconnected.com/using-postgresql-in-rails-e85f44e039e1?source=rss-11e116b029d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/e85f44e039e1</guid>
            <category><![CDATA[rails-api]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[postgresql-tutorials]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Austin Rhoads]]></dc:creator>
            <pubDate>Tue, 07 Dec 2021 03:09:39 GMT</pubDate>
            <atom:updated>2021-12-14T04:54:42.308Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we will implement <strong>PostgreSQL</strong> into our Rails application. By the end, your rails application will have been configured to create its databases using the PostgreSQL adapter. This article is for anyone who has some experience with Ruby on Rails.</p><p>We will complete the following tasks:</p><p>— Create a PostgreSQL user and password in the PostgreSQL shell.</p><p>— Create a Rails application (or API) that is setup for PostgreSQL.</p><p>— Configure the connections that attach your databases to your development, testing, and production environments.</p><p>— Finally we will create those databases.</p><p>If you haven’t already, you can download postgresql by following the instructions <a href="https://www.postgresql.org/download/">here</a>.</p><h3><strong>From SQLite3 to PostgreSQL</strong></h3><p><strong>PostgreSQL</strong> is an open-source <strong>relational database management system</strong> (RDBMS) that uses <strong>SQL</strong> as well as it’s own language to manage your database. The built in database management system for Rails is <strong>SQLite3.</strong> SQLite3 is great for development in your local environment but it doesn’t work for production because it only allows one user to write to the database at a time. With PostgreSQL we can build a better database that can handle production. To do that the first thing we need to do is create a PostgreSQL user and password.</p><h4><strong>PostgreSQL User and Password</strong></h4><p>First we enter the <strong>PostgreSQL </strong>shell which is called <strong>psql:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*hPOwoIwI9LlvzGaudJ1bIQ.png" /></figure><p>Next we create a user and password. Note that the password must be in single quotes and the command ends with a semi-colon.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/829/1*ItIyqvDnXrv8XhHyh1DvKw.png" /></figure><p>And that’s it. We can make sure we have created a user by printing out a list of all users. In the psql shell run:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*-ENNxXPuuosuHQ8OnX3nPA.png" /></figure><p>And there it is. Noice! There is a whole lot more you could do here including creating and editing a database but we’ll do all that stuff inside our rails application.</p><h3><strong>Creating the Rails API</strong></h3><p>When first creating the rails API we specify that our database will be built using the PostgreSQL system. We do this with the — <strong>database</strong> command option. I do not use the — <strong>api </strong>command option because this will leave out several features that I would just add back in later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/1*k1Mx7peaP5s1CuN4o8b1Mg.png" /></figure><p>Once your application is created go ahead and check the <strong>config/database.yml</strong> file to make sure we are set up for correctly. Under default we should see the adapter is set to postgresql.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*5qn6fAWpcDKX3geBswv-Lw.png" /></figure><p>Awesome! We are good to go. Now we just have to fix this file up a bit.</p><h4><strong>Connecting Environments to Databases</strong></h4><p>We are going to set the host, port, user, and password key for each environment. This is how we let Rails know how connect to our databases.</p><p>In Yaml files, we can define our default key values and call those in each environment. Then if we want to override a default value we simply redefine that key with a new value within that environment.</p><p>Note that we want a different database for testing and development because the test database is automatically erased and regenerated each time it is run.</p><p>Of course we could store all of these values as environment variables in your <strong>.env</strong> file but for simplicity sake I’ll just plug them in here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tUQseSZp8xayjVdA5WVUlA.png" /></figure><p>Now we can run either <strong>rake db:create</strong> or <strong>rails db:create</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*NOs85j5RFa0iWT1grONXhw.png" /></figure><p>You can see the development and test databases have been created. From here you can go ahead and create some models and migrations. If you decide to you can easily create a production database with Heroku or another service like RDS and connect it in this <strong>config/database.yml </strong>file.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e85f44e039e1" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/using-postgresql-in-rails-e85f44e039e1">Using PostgreSQL in Rails</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[HOW TO USE GOOGLE-MAPS-REACT — Part Two]]></title>
            <link>https://medium.com/@austinrhoads/how-to-use-google-maps-react-part-two-4a57ba1b5922?source=rss-11e116b029d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a57ba1b5922</guid>
            <dc:creator><![CDATA[Austin Rhoads]]></dc:creator>
            <pubDate>Sat, 04 Sep 2021 01:45:03 GMT</pubDate>
            <atom:updated>2021-09-04T01:45:03.833Z</atom:updated>
            <content:encoded><![CDATA[<h3>HOW TO USE GOOGLE-MAPS-REACT — Part Two</h3><p>In part two of this series we take the map component we made in the <a href="https://medium.com/@austinrhoads/how-to-use-google-maps-react-57ba97ca897b"><strong>previous article</strong></a> and give it some custom marker components. Then we will create an info bubble that displays when hovering over the marker. Let’s get into it.</p><h4><strong>Create a Marker Component</strong></h4><p>First, we’ll create another marker component specifically to label bait shops on the map. For now it’s exactly the same as the myLocationMarker only it renders green. Remember, to be able to render the component on the map all we have to do is pass in <strong>lat</strong> and <strong>lng</strong> properties.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B0Vz1nlUYfmmjLwFPUBGtQ.png" /></figure><p>We can then import it into our map component and write a function that returns all the locations as a markers while passing in the <strong>lat</strong> and <strong>lng</strong> properties. Obviously here you would fetch whatever places you have in your database but for this example I’ve just created an array of objects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tou355Xh4wileX0FRTdSkA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eC-aIhApfZr4XKnawB39Ag.png" /></figure><p>Then just pass this function into your <strong>GoogleMapReact</strong> component and it renders the list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q_3DeqOnprEnNBXHVCZXYQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a57ba1b5922" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[2D Collision Detection]]></title>
            <link>https://levelup.gitconnected.com/2d-collision-detection-8e50b6b8b5c0?source=rss-11e116b029d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e50b6b8b5c0</guid>
            <category><![CDATA[2d-game-design]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[2d-game-development]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[collision-detection]]></category>
            <dc:creator><![CDATA[Austin Rhoads]]></dc:creator>
            <pubDate>Mon, 16 Aug 2021 00:23:33 GMT</pubDate>
            <atom:updated>2021-09-03T14:03:33.263Z</atom:updated>
            <content:encoded><![CDATA[<p>I wanted to make a game that would operate and feel like Super Mario World out of JavaScript. The physics and movement of the characters on the screen in these games are simple and elegant. The most basic way to approach this type of 2d collision detection is to utilize a concept known as<strong> Axis-aligned bounding box</strong>.</p><p><strong>Axis-aligned bounding box</strong>, or<strong> AABB</strong>, detects if two non-rotational polygons are overlapping each other. Typically <strong>AABB</strong> algorithms run something like this:</p><p>We have two polygons, each with an X (left), and a Y (top) position, as well as width and height properties.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*yLbbs2q9vWbZpD-GkcXJsw.png" /></figure><p>We then compare X and Y positions along with width and height to see if they are overlapping.</p><p>We test four conditional statements, if all are true then we know we are overlapping. Lets break each conditional on it’s own.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*g-W506cjZogra51PFEyqSQ.png" /></figure><p>line 2 — A’s left side is to the left of B’s right side.</p><p>line 3 — A’s right side is to the right of B’s left side.</p><p>line 4 — A’s top is above B’s bottom.</p><p>line 5 — A’s bottom is lower than B’s top.</p><p>Let’s plug in the real values for Mario and his coin, represented by the yellow and red polygons. The X and Y positions of each polygon comes from the distance in pixels from the left and top edges of the viewport.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*NGKFPz5x-F4r_XH4DyuYzg.png" /></figure><p>This algorithm has a robust range of applications. It can be used to detect if your player is over a collectible item, touching a bad guy, at the door at the end of the game or just standing on a solid object.</p><h3>Game Components</h3><p>My project was a simple tile-based 2d platform game consisting of the player object, a 32 by 32 pixel red square named “Cubio”, three types of “baddies” and the tile map of the game’s world.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*2Gs5KWJGQXZGRNcZM9sViA.png" /></figure><h3>Tiles</h3><p>The entire map is made up of just four tiles types. 80 by 80 pixels in dimension, each has it’s own numerical value and specified sets of rules that determine how the player interacts with them upon collision.</p><h4><strong>0: sky</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/80/0*9RIIGlYaBo0bvt1E.png" /></figure><p>The player moves freely through this tile.</p><h4>1: earth</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/80/0*IT-hmrI9iJk-6tC5.png" /></figure><p>If the player lands on top of the tile, the player’s vertical movement stops and it rests on top of the tile, otherwise it can go through this tile in every other direction.</p><h4>2: crate</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/80/0*hiqkDfi1Lxuo5YZ7.png" /></figure><p>If the player collides with any side of this tile it’s position stays outside of this tile.</p><h4>3: sky_island</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/80/1*LWYyaU1rcUibgw_D1rNnzQ.png" /></figure><p>If the player lands on top of the tile, the player’s vertical movement stops and it rests on top of the tile, otherwise it can go through this tile in every other direction.</p><h3>Map</h3><p>Without going into too much detail here the tile map is represented by an array of tile types in their assigned column and row, such that the array:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cyo85mQTL5BeaNl9aPSMMw.png" /></figure><p>…appears as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*Nij-kCkhte7izbeV.png" /></figure><h3>Interaction and Behavior</h3><p>Whether its the sky or earth, my player is always colliding with at least one tile in every single frame so there is no need to run the aabb() function. My collision_detection() function first locates the position of each corner of the player. Then, it retrieves the type of tile that corner is colliding with. Then it passes in the tile’s type and the corner’s x and y position into the collide() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*4Zjl188aE8y0BGVn.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*Us4GaNkK-_8rMZGf.png" /></figure><p>The collide() function uses a switch statement to select the appropriate block of code to run depending on the tile type. As you can see type “0” (sky) has no effect and type “2” (crate) collides with all four sides. In case your are wondering why I’ve defined these points twice in collision_detection(), it’s because each time I call collide() it has the potential to move the player. So every point has to be redefined each time after I call collide().</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*9WKjucFte_JuIHmN.png" /></figure><p>In these different tile collide functions you can see the older position of the player is compared to the tile as well. This allows you to establish the direction the player is going.</p><p>The function needs to know what direction the player is going in order to have the correct response. Otherwise if the player collided with the bottom of the crate tile, it’s top is now above the crate’s bottom. Then when we run <strong>collide()</strong> the <strong>collide_top()</strong> fires first and places the player on top of the tile. To the user it would appear as if the player went straight through the bottom of the crate and landed on top. This negative effect is known as “tunneling”. Therefore, establishing player direction gives us a better representation of what’s happening.</p><p>You can see how that when writing a <strong>baddy_collision_detection()</strong> function it would be very similar to this only we can modify <strong>collide_top()</strong> to kill the baddy and all other collides to kill the player on contact.</p><pre>function baddyCollideTopMortal(player, bad){</pre><pre>    if (player.getBottom() &gt; bad.getTop() &amp;&amp; player.getOldBottom() &lt;= bad.getTop()){<br>      <br>        player.setBottom(bad.getTop() - 0.01);<br>        <br>        player.y_velocity = -27;<br>        player.jumping = false;<br>        let dead_index = currentGame.alive_baddies.indexOf(bad);<br>        currentGame.alive_baddies.splice(dead_index, 1);<br>        return true;<br>    } return false;<br>  }<br>  <br>  function baddyCollideLeft(player, bad){<br>  <br>    if (player.getRight() &gt; bad.getLeft() &amp;&amp; player.getOldRight() &lt;= bad.getLeft()){<br>        player.setRight(bad.getLeft() - 1);<br>        player.x_velocity = 0;<br>        player_dead = true;<br>        return true;<br>    } return false;<br>    <br>  }</pre><h3>Efficiency with AABB, Coins, and Baddies</h3><p>Once all of you tiles, coins and baddies are interacting with you player correctly you’ll notice that running a collision test on every coin and every baddy at every cycle of you engine loop is very costly and unnecessary.</p><p>This is where you separate the process of test for collision into two phases, broad and narrow.</p><h3>Broad</h3><p>In the broad phase, create an array of near entities(coins, baddies). Iterate through the full list of entities, checking the object’s x position to see if it’s close to the player and then pushing that entity into a near list. This is a lot less costly than running the entire collision test on each entity.</p><h3>Narrow</h3><p>Once a much smaller list of near objects is created we then iterate over each testing for collision with the <strong>aabb()</strong> function and/or run appropriate blocks of code.</p><p>Collision detection is the mechanism that brings your game to life. Each step is easy to understand and can be re-imagined many different ways, but once implemented is powerful. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e50b6b8b5c0" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/2d-collision-detection-8e50b6b8b5c0">2D Collision Detection</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[How to use google-maps-react: Part One]]></title>
            <link>https://levelup.gitconnected.com/how-to-use-google-maps-react-57ba97ca897b?source=rss-11e116b029d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/57ba97ca897b</guid>
            <category><![CDATA[google-map-api]]></category>
            <category><![CDATA[google-map-react]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Austin Rhoads]]></dc:creator>
            <pubDate>Tue, 10 Aug 2021 22:28:12 GMT</pubDate>
            <atom:updated>2021-09-03T14:12:29.064Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0auUWDNxcxpkp6eu_iNR7w.jpeg" /></figure><p>When I decided to make a React fishing app I knew it was crucial to allow users to save locations because one of the main differences between standing on the shore like an idiot and catching fish is knowing <em>where</em> to fish.</p><p>Whether you want to drop a pin on that new fishing spot where you caught a monster trout, provide directions to a local business or display search results for a realty website, adding a map to your application can be <strong>very powerful</strong>.</p><h3>What To Expect From This Article</h3><p>This is the first part in a series I am doing on building a high functioning dynamic map using <strong>GoogleMapReact</strong> in your React application. Assuming you are familiar with React by the end of this series you should be able to create your own map component and give it customized functionality based on your own needs. By the end of this article you should have a map up and running and be able to populate it with custom markers.</p><p><strong>GoogleMapReact</strong> gives you access to the functionality of the Google Maps API but also allows you to render custom React components inside the map. In fact it will render any component onto the map that has <strong>lat</strong> and <strong>lng</strong> properties. Which is super awesome.</p><h3><strong>Google API</strong></h3><p>To get started you’ll need to create a Google API project. You can do so <a href="https://developers.google.com/workspace/guides/create-project">here</a>. Once you’ve created your project enable the <strong>Maps JavaScript API</strong> and get an <strong>API key</strong>. There are many resources for how to do this and the Google documentation for this can be found <a href="https://developers.google.com/maps/documentation/javascript/get-api-key">here</a>. Once you have your API key, hang on to it we’ll need it later.</p><h3><strong>The NPM Package</strong></h3><p>There are many different packages with similar names so make sure you’re using the <strong>google-map-react</strong> (NOT react-google-maps or google-maps-react). The correct package can be found <a href="https://www.npmjs.com/package/google-map-react">here</a>.</p><p>Install the package in your react application by running:</p><p>npm i google-map-react</p><h3><strong>Getting Up and Running</strong></h3><p>Next we are going to create our own map component to render in our app. We’ll import <strong>GoogleMapReact</strong> inside our <strong>MapExample</strong> component. Also, there are a few things required initially so the map will display and function correctly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8rt7bRwGwkA6k0p-XA3z0Q.png" /></figure><h3><strong>Set Inline Dimensions</strong></h3><p>Set the width and height of the MapExample component. As a requirement of all Google Maps, the imported map will fill the dimensions of it’s parent component but if there are no dimensions set, it will collapse down to zero. I set the dimensions with inline CSS to ensure it always loads with set dimensions.</p><h3><strong>Default Map Props</strong></h3><p>Before it loads, Google Maps looks for it’s <strong>defaultCenter, center</strong> and <strong>defaultZoom</strong> properties. d<strong>efaultCenter</strong> and<strong> center</strong> should return an object that has <strong>lat</strong> and <strong>lng</strong> properties, and <strong>defaultZoom</strong> is an integer. I start with 12. The higher the zoom integer the more zoomed in it is.</p><p>In the MapExample I have the default <strong>lat</strong> and <strong>lng</strong> set to the spot where I caught all of these speckled trout. :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FxF12I2YJXEpoBDlcOLtow.jpeg" /></figure><p>The <strong>center</strong> is set to <strong>this.state.center</strong> which for now is the same as the <strong>defaultCenter.</strong> You do not want the defaultCenter set to the state because Google Maps doesn’t like it when you change the default props and as you use the map you will change the state’s center property continuously.</p><h3><strong>Google API Key</strong></h3><p>In the GoogleMapReact object pass in your API key in the <strong>bootstrapURLKeys</strong>. Pass in an object that has a key property set to your API Key in quotations.</p><h3><strong>OnChange State Update</strong></h3><p>We want our state to always reflect the current position of the map center, which can be changed by dragging the map with the grabber tool. Luckily, built into the onChange prop is access to current <strong>center</strong> and <strong>zoom</strong> properties. Within this event handler we can update the state to wherever the map is currently pointing every time it changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*P0OFaqesmyMkpexMQCgPbQ.png" /></figure><p>Now that we have our map up and running there are some basic functions you might want for your application.</p><h3><strong>Getting the User’s Location</strong></h3><p>Let’s go ahead and put a button in the MapExample component that when clicked grabs the user’s location and re-centers the map to that location.</p><p>First we grab the user’s location in the App component. Then we pass it down through Props to the MapExample component. I also save it to localStor</p><p>age for reasons which I’ll get into later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bu9wK3DlQuHpLIF06Yh7XA.png" /></figure><p>Inside the MapExample component we make a button which calls the <strong>centerMyLocation</strong> function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2wCnvIl5PW9kjpbOtUrYow.png" /></figure><p>This function inside the MapExample component then centers the map over the user’s location by updating the state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v0mcsAwX8WRcN5YJMp-6xw.png" /></figure><h3><strong>Placing Markers</strong></h3><p>As I said before, one of the coolest things about GoogleMapReact is that it will render any component onto the map that has <strong>lat</strong> and <strong>lng</strong> props. You can also create that component with functions that suit your applications specific needs. So let’s make some markers!</p><h3><strong>My Location Marker</strong></h3><p>First we’ll create a simple MyLocationMarker component that automatically displays the user’s location when the map loads. I’ve provided some inline CSS that will make the div a blue dot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XIKC_lsrHDDRLRQV7RBoDw.png" /></figure><p>From here all we have to do is import the marker to the MapExample and pass in the user’s lat and lng as props.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X4ZusL3jLAKk_pzwsZ-K-g.png" /></figure><p>And Boom! We have our user’s location…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*9oWfgwIdMIrfGhJ5zdx_TQ.png" /></figure><p>To get the map to automatically center on the user’s location when it loads there are several options. For this example I simply save the user’s location to localStorage and retrieve that in the MapExample component. Then I set it to the state with a logical OR operator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M2ox7h8J-M8637adba2U1g.png" /></figure><h3><strong>Re-Cap</strong></h3><p>So far in the first part of the series we’ve learned how to:</p><ul><li>Import <strong>google-map-react</strong> into a component.</li><li>Set the default props.</li><li>Access map properties from the <strong>onChange</strong> event handler.</li><li>Get the user’s Location.</li><li>Place a marker component set to the user’s location inside the map.</li></ul><p>I hope this helps you feel more oriented towards using google-map-react. In the next part of the series I’ll go over how to create more functional marker components with info bubbles. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57ba97ca897b" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/how-to-use-google-maps-react-57ba97ca897b">How to use google-maps-react: Part One</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[Session Store With React/Rails API]]></title>
            <link>https://medium.com/@austinrhoads/session-store-with-react-rails-api-116a9efe3c30?source=rss-11e116b029d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/116a9efe3c30</guid>
            <dc:creator><![CDATA[Austin Rhoads]]></dc:creator>
            <pubDate>Tue, 20 Jul 2021 02:16:18 GMT</pubDate>
            <atom:updated>2021-09-10T17:01:15.246Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/259/1*JZ9PMeOnM_YE8iK5w3rmqA.jpeg" /></figure><p>While recently building a React/Rails API application, invalid signature and CORS errors quickly surfaced when attempting to store sessions. I found some code <a href="https://dwinniford.github.io/setting_up_session_cookies_in_a_rails_api_with_javascript_front_end">here</a> that fixed these issues but I wanted to know why they were fixing them and what was the fundamental issue. I learned that I had to add middleware to my application and reconfigure how I made fetch requests. The subject of cookies is very complicated under the surface but this is how it works as far as I understand it.</p><p>Your application has all of these end points that create, read, update or delete data in your database through HTTP requests (GET, POST, PUT, DELETE). When you sign into a website, you set a cookie in the browser. This cookie is data package that is sent from that website to your computer. Before any request you send is acted upon, from a form or an AJAX request, that cookie is checked to make sure the request is legit. Request can come from any site, so its useful if say ‘JoeShmo.com’ sends a request to your bank app saying, transfer all funds to ‘JoeShmo.com’.</p><p>Most pre-made frameworks such as Ruby on Rails communicate within themselves very securely because they have built in CSRF. CSRF or Cross Site Request Forgery is a token that is generated when the page is loaded. Then when you send a form or make a request the token goes along for the ride to verify the requests validity. If the token is invalid the request gets denied. This is very useful however, with my React/Rails API application I need to apply some middleware to make this work.</p><p>In config/application add these two lines:</p><p>config.middleware.use ActionDispatch::Cookies</p><pre>config.middleware.use ActionDispatch::Session::CookieStore</pre><p>In your application controller you set your CSRF cookie as a before action.</p><pre>class ApplicationController &lt; ActionController::API<br>    before_action :set_csrf_cookie<br>    include ActionController::Cookies<br>    include ActionController::RequestForgeryProtection<br>  <br>    protect_from_forgery with: :exception <br>                <br>    def cookie <br>        &quot;ok&quot;<br>    end<br>                <br>    private <br>                <br>    def set_csrf_cookie<br>       cookies[&quot;CSRF-TOKEN&quot;] = {<br>            value: form_authenticity_token,<br>            domain: :all <br>        }<br>    end<br>end</pre><p>in routes.rb</p><pre>root to: &quot;application#cookie&quot;</pre><p>Make sure in your initializers/cors.eb file to have credentials set to true.</p><pre>Rails.application.config.action_controller.forgery_protection_origin_check = false</pre><pre>Rails.application.config.middleware.insert_before 0, Rack::Cors do<br>   allow do<br>     origins &#39;localhost:3000&#39; <br>     resource &#39;*&#39;,<br>       headers: :any,<br>       methods: [:get, :post, :put, :patch, :delete, :options, :head], <br>       credentials: true <br>   end<br>end</pre><p>Now in all your fetch requests that aren’t strictly GET requests you’ll have to send the CSRF token in the headers.</p><p>Something like this in React:</p><pre>function getCSRFToken() {<br>    return unescape(document.cookie.split(&#39;=&#39;)[1])<br>  }</pre><pre>const configObject = {<br>                method: &quot;POST&quot;,<br>                credentials: &#39;include&#39;,<br>                headers: {<br>                    &#39;X-CSRF-Token&#39;: getCSRFToken(),<br>                   <br>                },<br>                body: JSON.stringify(data)<br>            }</pre><p>Now when you make requests a CSRF-Token is sent in the headers and everything is routed through session#cookies which sets the cookie. And that’s all she wrote! The sessions controller from here acts as it normally would. Yay another wall stepped over :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=116a9efe3c30" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>