<?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>DEV Community: Szabi</title>
    <description>The latest articles on DEV Community by Szabi (@szabikr).</description>
    <link>https://dev.to/szabikr</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%2F271198%2F60a0a6fb-9ee0-445d-afac-aef1fc96c6d2.jpg</url>
      <title>DEV Community: Szabi</title>
      <link>https://dev.to/szabikr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/szabikr"/>
    <language>en</language>
    <item>
      <title>CORS is a Pain for Side Projects</title>
      <dc:creator>Szabi</dc:creator>
      <pubDate>Sat, 21 Mar 2020 16:05:17 +0000</pubDate>
      <link>https://dev.to/szabikr/cors-is-a-pain-for-side-projects-50fp</link>
      <guid>https://dev.to/szabikr/cors-is-a-pain-for-side-projects-50fp</guid>
      <description>&lt;p&gt;If you are even a slightly bit adventurous web developer you must have faced this problem before and maybe you solved it and got on with your development or maybe you were like me who just gave up at that point because anyway is just a side project and I could just start using a framework where the back-end &amp;amp; front-end has the same origin and I can skip this problem all together.&lt;/p&gt;

&lt;p&gt;This time I was a bit more serious about my side project, &lt;em&gt;My Habit Tracker&lt;/em&gt; and I decided to go all the way and fix this issue.&lt;/p&gt;

&lt;p&gt;Spoiler alert: &lt;em&gt;It's pretty simple actually.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We are going to create a browser based front-end web app that'll send a request to an end-point in the cloud. Technologies that are involved in this project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS API Gateway&lt;/li&gt;
&lt;li&gt;AWS Lambda&lt;/li&gt;
&lt;li&gt;React (w. create-react-app)&lt;/li&gt;
&lt;li&gt;Axios&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First step is to create a React app using &lt;code&gt;create-react-app&lt;/code&gt;. In order to achieve that run the following commands in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;npx create-react-app poc-cors-app
cd poc-cors-app
yarn start
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The app will be served in your browser on &lt;code&gt;http://localhost:3000/&lt;/code&gt;, this is going to be &lt;strong&gt;domain number 1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;After we've seen the react logo spinning around, let's edit the &lt;code&gt;App.js&lt;/code&gt; file to get rid of all unnecessary UI elements. Also we will have to use the &lt;code&gt;useState&lt;/code&gt; hook because the data what we want to show is going to come from the cloud and will be updated after our request had a successful response. So basically we are turning the &lt;code&gt;App&lt;/code&gt; component into a stateful component but thanks to react hooks we can keep the function syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./App.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setData&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CORS not yet enabled&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="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;App&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;header&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;App-header&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&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;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/header&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The command line will warn you about not using the &lt;code&gt;setData&lt;/code&gt; but don't worry, we are going to get back to this. Now let's create our back-end services.&lt;/p&gt;

&lt;p&gt;Login to AWS console and under Service find Lambda which will always be under the Compute section. When you navigate to Lambda you will be able to create a new function by clicking Create function on the top right corner of the page. Name the function &lt;code&gt;poc-cors-getData&lt;/code&gt; and hit the Create function button.&lt;/p&gt;

&lt;p&gt;After your lambda function has been created edit the function code to match the following line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CORS is enabled yeeey!&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="nx"&gt;response&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;Next up head to API Gateway which you'll find in Network and Content Delivery section and create a new REST API named &lt;code&gt;poc-cors-api&lt;/code&gt; which works with Lambda.&lt;br&gt;
By clicking the Actions dropdown add a new Resource to the api called &lt;code&gt;end-point&lt;/code&gt; and leave the rest of the form as default. Select the newly created Resource and add a GET Method to it by clicking the Actions dropdown again. Before you can save the method you need to attach the lambda function what we've created previously by providing the name of the function, &lt;code&gt;poc-cors-getData&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Okay, let's deploy our API (Actions dropdown), we can call the deployment stage &lt;code&gt;dev&lt;/code&gt; for the sake of this example. The &lt;code&gt;dev&lt;/code&gt; stage will provide you an Invoke URL which you can use to test your API in Postman i.e, or just simply copy it to your browser and append &lt;code&gt;/end-point&lt;/code&gt; to the end of the URL, and this is your &lt;strong&gt;domain number 2&lt;/strong&gt;. Hopefully you get back the response what the Lambda function has. If not, make sure all the names are identical.&lt;/p&gt;
&lt;h3&gt;
  
  
  Alright, back to the front-end
&lt;/h3&gt;

&lt;p&gt;Now we have to make a request to this API from our React app and in order to do this we are going to use &lt;a href="https://github.com/axios/axios"&gt;axios&lt;/a&gt;, add the package to the project using &lt;code&gt;yarn&lt;/code&gt; in your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add axios
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In your &lt;code&gt;App.js&lt;/code&gt; file make sure that you additionally import &lt;code&gt;axios&lt;/code&gt; and &lt;code&gt;useEffects&lt;/code&gt; hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;axios&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;Let's make the request by inserting the following piece of code after you call the useState and before you return the UI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;useEffect&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&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;https://YOUR_INVOKE_URL_ID.execute-api.eu-west-1.amazonaws.com/dev/end-point&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;fetchData&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;blockquote&gt;
&lt;p&gt;If you are not familiar with React Hooks I would recommend going through the compact and informative docs &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When you serve the web app now you'll find that the &lt;code&gt;CORS not yet enabled&lt;/code&gt; text is visible in the middle of the page and if you open up developer tools and navigate to the Console you'll see an error indicating that your request has been blocked by CORS (Cross Origin Resource Sharing). This happens because your front-end and cloud service are on two different domains and your browser blocks the request by default. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to dive into the details I can recommend &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"&gt;this&lt;/a&gt; article which has a bunch of information on the topic.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Luckily AWS made it very easy for us developers to Enable CORS on different API Gateway resources. In order to set this up we have to go back to AWS Console.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cool, switch to the back-end
&lt;/h3&gt;

&lt;p&gt;Select your Resource that you want to enable CORS on, &lt;code&gt;/end-point&lt;/code&gt; in our situation and open the Actions dropdown where you'll find the Enable CORS Resource Action, click on it. Leave all the settings as default and tap on Enable CORS and replace existing CORS headers button. This will create an OPTIONS Method which will have all the necessary Response Headers for letting your request from the browser to get a successful response.&lt;/p&gt;

&lt;p&gt;Finally, deploy the API (Actions dropdown). Refresh your browser and hopefully see the string that is returned in the response body from the Lambda function.&lt;/p&gt;




&lt;p&gt;Hope this short guide on how to start local web development using AWS services and React was useful. Please let me know in the comments if you got stuck at a certain step and I'll try my best to help you figure out what went wrong.&lt;/p&gt;

&lt;p&gt;Also, I would appreciate some feedback on how easy it was to follow this guide without any AWS Console screenshots?&lt;/p&gt;

&lt;p&gt;Thanks a lot for reading! 📖 Happy coding everyone ✌🏻&lt;/p&gt;

</description>
      <category>cors</category>
      <category>aws</category>
      <category>react</category>
      <category>sideprojects</category>
    </item>
    <item>
      <title>AWS - Admin User</title>
      <dc:creator>Szabi</dc:creator>
      <pubDate>Thu, 05 Mar 2020 16:50:06 +0000</pubDate>
      <link>https://dev.to/szabikr/aws-admin-user-5e8k</link>
      <guid>https://dev.to/szabikr/aws-admin-user-5e8k</guid>
      <description>&lt;p&gt;Started to use AWS in order to create back-end services for &lt;code&gt;My Habit Tracker&lt;/code&gt; application. An informative article described that it's not a good practice to use your &lt;code&gt;Root User&lt;/code&gt; to execute daily tasks in &lt;code&gt;AWS Console&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So I created an &lt;code&gt;IAM User&lt;/code&gt;, which I'm going to login with to work on my cloud solution for the application. &lt;/p&gt;

&lt;p&gt;First I wanted to create a &lt;code&gt;Role&lt;/code&gt; which has &lt;code&gt;AdministratorAccess&lt;/code&gt; permissions and make my &lt;code&gt;IAM User&lt;/code&gt; to assume that role, but it seemed a bit overkill as it's in the same account. I ended up creating a &lt;code&gt;IAM Group&lt;/code&gt; called &lt;code&gt;AdminGroup&lt;/code&gt; and attached the &lt;code&gt;AdministratorAccess&lt;/code&gt; policy, then added my previously created &lt;code&gt;IAM User&lt;/code&gt; to the group.&lt;/p&gt;

&lt;p&gt;Finally I just needed to add an &lt;code&gt;Account Alias&lt;/code&gt; so that I don't need to remember my &lt;code&gt;12 digit Account ID&lt;/code&gt; all the time. An account alias is also useful to make your login link a bit user friendly.&lt;/p&gt;

</description>
      <category>aws</category>
    </item>
    <item>
      <title>My Habit Tracker</title>
      <dc:creator>Szabi</dc:creator>
      <pubDate>Tue, 25 Feb 2020 11:50:14 +0000</pubDate>
      <link>https://dev.to/szabikr/my-habit-tracker-3o9h</link>
      <guid>https://dev.to/szabikr/my-habit-tracker-3o9h</guid>
      <description>&lt;p&gt;Started working on this habit tracker application which will help me and my friend (who is a UI/UX designer &amp;amp; user researcher) to track our habits in an extremely simple way. And hopefully extract useful information out of the data.&lt;/p&gt;

&lt;p&gt;The MVP is very simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User is able the add any number of habits and categorise them&lt;/li&gt;
&lt;li&gt;User is able to perform any of those habits at any time&lt;/li&gt;
&lt;li&gt;User will see the list of performed habits&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Starting with a website approach at the beginning and looking into transforming it into a PWA for offline accessibility and app like experience.&lt;/p&gt;

&lt;p&gt;Technologies that I use is create-react-app which helps me build a React app with Hooks. Hosting the site in an AWS S3 bucket to start with and will use AWS Lambda for handling back-end business logic.&lt;/p&gt;

&lt;p&gt;Planning on using MongoDB in order to store the data that we capture. A NoSQL database will do the job in our situation because we want to use the app as soon as possible to capture data as early as we can, but the data structure will change and because of this flexibility is key.&lt;/p&gt;

</description>
      <category>react</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
