<?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: James Giosmas</title>
    <description>The latest articles on DEV Community by James Giosmas (@giosmasj).</description>
    <link>https://dev.to/giosmasj</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%2F461775%2Fcd06a963-c2d4-4c1e-a4a6-ab7fabb29950.png</url>
      <title>DEV Community: James Giosmas</title>
      <link>https://dev.to/giosmasj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/giosmasj"/>
    <language>en</language>
    <item>
      <title>Auth in Rails Part 3</title>
      <dc:creator>James Giosmas</dc:creator>
      <pubDate>Sun, 27 Sep 2020 23:07:44 +0000</pubDate>
      <link>https://dev.to/giosmasj/auth-in-rails-part-3-3l4d</link>
      <guid>https://dev.to/giosmasj/auth-in-rails-part-3-3l4d</guid>
      <description>&lt;p&gt;Hooray we've made it to part 3 in a 3 part series on basic authentication in Ruby on Rails! Checkout &lt;a href="https://dev.to/giosmasj/creating-new-users-in-rails-1909"&gt;Part 1&lt;/a&gt; and &lt;a href="https://dev.to/giosmasj/auth-in-rails-part-2-1p8e"&gt;Part 2&lt;/a&gt; if you haven't already. For part 3 the goal will be to verify and protect resources with tokens.&lt;/p&gt;

&lt;p&gt;In the previous posts we've created a Ruby on Rails API for a clothing store that has &lt;code&gt;users&lt;/code&gt;. Now we're going to make it so only logged in &lt;code&gt;users&lt;/code&gt; can create &lt;code&gt;items&lt;/code&gt;. Let's generate this new resource &lt;code&gt;item&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In your terminal:&lt;br&gt;
&lt;code&gt;rails g controller items&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rails g model item&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we setup the route and the migration, &lt;code&gt;items&lt;/code&gt; will have a name and a price.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fs83dr61hzy9iydh9a6xf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fs83dr61hzy9iydh9a6xf.png" alt="Alt Text" width="762" height="224"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu7bmncrs7o5wlkinwqll.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu7bmncrs7o5wlkinwqll.png" alt="Alt Text" width="800" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we'll setup a basic &lt;code&gt;ItemsController&lt;/code&gt; where anyone can create an &lt;code&gt;item&lt;/code&gt; and index all of the &lt;code&gt;items&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F30nftjq6jcd3sigd1zdv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F30nftjq6jcd3sigd1zdv.png" alt="Alt Text" width="780" height="616"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In your terminal run &lt;code&gt;rails db:migrate&lt;/code&gt; and start your server &lt;code&gt;rails s&lt;/code&gt; so we can test this in Postman. A &lt;code&gt;get&lt;/code&gt; request to &lt;code&gt;localhost:3000/items&lt;/code&gt; should show an empty list of &lt;code&gt;items&lt;/code&gt;. So let's make sure we can create an &lt;code&gt;item&lt;/code&gt; with a &lt;code&gt;post&lt;/code&gt; request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzegddvda76ttzjqdlfij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzegddvda76ttzjqdlfij.png" alt="Alt Text" width="800" height="258"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fasallmvmujums1emrfw0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fasallmvmujums1emrfw0.png" alt="Alt Text" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's make it so only a logged in &lt;code&gt;user&lt;/code&gt; with a valid token can create an &lt;code&gt;item&lt;/code&gt;. Let's update the &lt;code&gt;create&lt;/code&gt; method in the &lt;code&gt;ItemsController&lt;/code&gt;. When making HTTP requests the token is stored in the &lt;code&gt;Authorization&lt;/code&gt; header so we will want access to that. If the authorization header is absent from the request we can end the conversation right there and present an error. Else, we'll want to split the token from the authorization header at " " since the header includes "Bearer " prior to the token itself. Now we want to DECODE the token (that we encoded in the &lt;code&gt;AuthorizationController&lt;/code&gt;) so we can use it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6fw7nb6lfgvqmozpbef3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6fw7nb6lfgvqmozpbef3.png" alt="Alt Text" width="800" height="806"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's try to add a shirt with no token present&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2lihz26psca1hsxgkdpr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2lihz26psca1hsxgkdpr.png" alt="Alt Text" width="800" height="266"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Femf37fsajp0oedo1qnwi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Femf37fsajp0oedo1qnwi.png" alt="Alt Text" width="800" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's try it with my token, the token can be obtained by making a &lt;code&gt;post&lt;/code&gt; request to the &lt;code&gt;login&lt;/code&gt; route with a valid username and password. Enter your token under the "Authorization" header and make sure to select "Bearer Token"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3b3v285f4i9sz8m8i8mx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3b3v285f4i9sz8m8i8mx.png" alt="Alt Text" width="800" height="85"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdn7uw8140t6u3v8jsndu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdn7uw8140t6u3v8jsndu.png" alt="Alt Text" width="800" height="272"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkcikzzwtxh9hjk7jywut.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkcikzzwtxh9hjk7jywut.png" alt="Alt Text" width="800" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And lastly let's tamper with this token and make sure it only works with a valid token. I've added some "xxxxx" to the end of my token and will try to add a sweater to our items list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc4le1f4ivrfahiju4o5q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc4le1f4ivrfahiju4o5q.png" alt="Alt Text" width="800" height="43"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2z9kacv6aecoip2ze497.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2z9kacv6aecoip2ze497.png" alt="Alt Text" width="800" height="273"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz8uhkob2wuto8e72kko1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz8uhkob2wuto8e72kko1.png" alt="Alt Text" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright fam we've made it! The &lt;code&gt;item&lt;/code&gt; resource has been successfully protected! Thanks again for reading and as always please let me know if you have any questions, comments, suggestions, etc. because as a developer I'm always learning so there's always more to know!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>auth</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Auth in Rails Part 2</title>
      <dc:creator>James Giosmas</dc:creator>
      <pubDate>Sat, 19 Sep 2020 20:39:28 +0000</pubDate>
      <link>https://dev.to/giosmasj/auth-in-rails-part-2-1p8e</link>
      <guid>https://dev.to/giosmasj/auth-in-rails-part-2-1p8e</guid>
      <description>&lt;p&gt;Alrighty, been a busy few weeks so if you've been waiting for  part 2 in my auth series I thank you for your patience! Today's goals will be to verify the identity of a user and issue signed tokens to logged in users.&lt;/p&gt;

&lt;p&gt;Step by step for verifying a user's identity in Rails will be to create a Login &lt;code&gt;route&lt;/code&gt;, generate an &lt;code&gt;authentication_controller&lt;/code&gt;, create a &lt;code&gt;login action&lt;/code&gt;, look up the user, authenticate that user, create a token, then sign it, and lastly send that signed token to the user.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqql5ecv8ywa5af48oa5z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqql5ecv8ywa5af48oa5z.png" alt="Alt Text" width="750" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above I added line 3 of code which created the Login &lt;code&gt;route&lt;/code&gt; that basically says any &lt;code&gt;post request&lt;/code&gt; to the &lt;code&gt;login path&lt;/code&gt; should go to the &lt;code&gt;authentication_controller&lt;/code&gt;'s &lt;code&gt;login action&lt;/code&gt;. Next we'll create the &lt;code&gt;authentication_controller&lt;/code&gt; with the following command in our terminal:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails g controller authentication&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now inside the &lt;code&gt;authentication_controller&lt;/code&gt; we can create the &lt;code&gt;login action&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyotgshkckddxgpqu1kws.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyotgshkckddxgpqu1kws.png" alt="Alt Text" width="800" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is essentially saying if the username exists and the password is correct you will be successfully logged in and receive a 200 OK status with a success message; otherwise you will receive an error message for what is incorrect and a 401 unauthorized status. Here it is in Postman:&lt;/p&gt;

&lt;p&gt;Correct Username and Password (Status 200 OK):&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb6ttm3o6xhh35nsyrw4r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb6ttm3o6xhh35nsyrw4r.png" alt="Alt Text" width="800" height="261"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff2xqr0uy7b3ch70e6gbb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff2xqr0uy7b3ch70e6gbb.png" alt="Alt Text" width="800" height="184"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Incorrect Username (Status: 401 Unauthorized):&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0ygbzt9hau3npxmc1vzp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0ygbzt9hau3npxmc1vzp.png" alt="Alt Text" width="800" height="260"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fop6msopqsjtgve61o02a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fop6msopqsjtgve61o02a.png" alt="Alt Text" width="800" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Incorrect Password (Status: 401 Unauthorized):&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu18l0kc9jumvoqdco1xy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu18l0kc9jumvoqdco1xy.png" alt="Alt Text" width="800" height="265"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9wexanecldf5uacv97ss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9wexanecldf5uacv97ss.png" alt="Alt Text" width="800" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we've made it to the step where we are going to create a token. There are a few different ways to handle login and today we'll be handling login with token based authentication. How this works is the client gives the username and password to the server and in return for a correct username and password the server sends back a token. Now the token will be sent with all future requests instead of asking for login info. One way to think about this is when entering an event you show your ID (&lt;code&gt;username&lt;/code&gt;) and pay a cover charge (&lt;code&gt;password&lt;/code&gt;) in exchange for a wristband (&lt;code&gt;token&lt;/code&gt;) so you can come and go as you please without having to show ID and pay (login) again. For this token based authentication we need token encoding and this will be handled by a gem called &lt;code&gt;JWT&lt;/code&gt; (JSON Web Token, pronounced jot). The token consists of 3 parts: a header, a payload and a signature. The header and payload are encoded which means they can be reversed easily therefore the signature is the header and payload hashed which cannot be easily reversed. Now a signed token is created that cannot be tampered with or used to impersonate another user. Now let's install the JWT gem (which is the specific token encoding we'll be using) in the &lt;code&gt;Gemfile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7ofskrj0k7xozzzzaia0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7ofskrj0k7xozzzzaia0.png" alt="Alt Text" width="626" height="126"&gt;&lt;/a&gt;&lt;br&gt;
Simple as that and now in your terminal run &lt;code&gt;bundle install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let's set it up and call it a day! In the &lt;code&gt;authentication_controller&lt;/code&gt; we'll be replacing the last &lt;code&gt;else&lt;/code&gt; statement (line 10)  that had the success message about being logged in. That wasn't actually doing anything so let's change that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnshqysbtpb6qkowfo72n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnshqysbtpb6qkowfo72n.png" alt="Alt Text" width="800" height="518"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, now instead of a congratulatory message we receive a signed token. We setup a &lt;code&gt;payload&lt;/code&gt; which is some sort of evidence, it is what the server looks up the user by to prove they're who they say they are and have permission to do what they're trying to do. Later, you'll see why it isn't a good idea to include the &lt;code&gt;password&lt;/code&gt; as part of the &lt;code&gt;payload&lt;/code&gt;. Then, we also setup the &lt;code&gt;secret&lt;/code&gt;, which can be any string. I used the method that is supplied by Rails, but again could literally be any string you want. Then encode the token and lastly send it back (render it) with a signature on it.&lt;/p&gt;

&lt;p&gt;Alright, now let's see what this gives us in Postman&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdmsh96ut9eqprrtj3d4x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdmsh96ut9eqprrtj3d4x.png" alt="Alt Text" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We got the token! So let's see what it decodes to on JWT.io&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkjdicfmycojgjicxkw6j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkjdicfmycojgjicxkw6j.png" alt="Alt Text" width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Great now we see that is indeed the correct &lt;code&gt;user_id&lt;/code&gt; and &lt;code&gt;username&lt;/code&gt; so we know this signed token is doing exactly what it should be. Also, you can see if we included the &lt;code&gt;password&lt;/code&gt; as part of the &lt;code&gt;payload&lt;/code&gt; it could easily be exposed right here. Thanks for reading and as always please let me know if you have any questions, comments, suggestions, etc. because as a developer I'm always learning so there's always more to know!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>auth</category>
    </item>
    <item>
      <title>Creating New Users in Rails (Auth Part 1)</title>
      <dc:creator>James Giosmas</dc:creator>
      <pubDate>Wed, 02 Sep 2020 18:40:18 +0000</pubDate>
      <link>https://dev.to/giosmasj/creating-new-users-in-rails-1909</link>
      <guid>https://dev.to/giosmasj/creating-new-users-in-rails-1909</guid>
      <description>&lt;p&gt;As a routine, while on the job search, I will be aiming to upload a weekly blog post, in attempts to explain something technical that I've learned or grown from. I will be starting off with a three part series on authentication (auth), not to be confused with authorization.&lt;/p&gt;

&lt;p&gt;The goal in the first part of this series will be to create new users in Ruby on Rails and securely store their password. We will have to create a &lt;code&gt;User&lt;/code&gt; resource and store a hashed version of the password that &lt;code&gt;User&lt;/code&gt; created.&lt;/p&gt;

&lt;p&gt;This will be pretty straight-forward, the biggest difference between creating users and other resources will be storing a &lt;em&gt;hashed&lt;/em&gt; version of the password rather than the &lt;em&gt;actual&lt;/em&gt; password. Hashing is one-way encryption. The term hashing originates from making hashbrowns. When you start with each whole raw potato it's easier to tell which pieces came from which potato, but the more you cut up, stir in, and cook, it becomes impossible to tell which piece came from which potato and even more impossible to turn them back into whole raw potatoes.&lt;/p&gt;

&lt;p&gt;So we're going to start off by making a rails api and going from there. For this example we'll make an API for a clothing store that will have &lt;code&gt;Users&lt;/code&gt;. To create a &lt;code&gt;clothing-store&lt;/code&gt; API we'll simply run the following command in our terminal inside of the directory we would like it to be saved in.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails new clothing-store --api&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Once Rails works its magic, we can open our boiler-plate API. The first thing we will do is open the &lt;code&gt;Gemfile&lt;/code&gt; and un-comment line 17 (or whichever line says something along the lines of &lt;code&gt;gem 'bcrypt', '~&amp;gt; 3.1.7'&lt;/code&gt;). This is a very simple example but if we were adding any other gems this would be a good time to do so. Now in the terminal run &lt;code&gt;bundle install&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bcrypt&lt;/code&gt; is the gem that will be hashing our passwords for us. Once again showing that Rails really does give you so much power right out of the box. &lt;/p&gt;

&lt;p&gt;Now we can setup our &lt;code&gt;routes&lt;/code&gt;. For this example we &lt;em&gt;only&lt;/em&gt; want to be able to create &lt;code&gt;Users&lt;/code&gt; so that is the only route I will be creating. More developed applications may include other resources or routes such as, &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;show&lt;/code&gt;, &lt;code&gt;new&lt;/code&gt;, &lt;code&gt;edit&lt;/code&gt;, &lt;code&gt;update&lt;/code&gt;, and &lt;code&gt;delete&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzzbn1qnx5vtt41hmpzkx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzzbn1qnx5vtt41hmpzkx.png" alt="Alt Text" width="646" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now in our terminal we have to generate a &lt;code&gt;users&lt;/code&gt; controller by running &lt;code&gt;rails g controller users&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In our &lt;code&gt;users_controller.rb&lt;/code&gt; we need a &lt;code&gt;create&lt;/code&gt; method. In this example that is the &lt;em&gt;only&lt;/em&gt; method we will need. Just like &lt;code&gt;routes&lt;/code&gt; more developed projects may include other methods such as, &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;show&lt;/code&gt;, &lt;code&gt;new&lt;/code&gt;, &lt;code&gt;edit&lt;/code&gt;, &lt;code&gt;update&lt;/code&gt;, and &lt;code&gt;delete&lt;/code&gt;. Our &lt;code&gt;users_controller.rb&lt;/code&gt; should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw3y52qjva45pu5lb05y9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw3y52qjva45pu5lb05y9.png" alt="Alt Text" width="800" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;create&lt;/code&gt; method is pretty simple all it is saying is when creating a new instance of a &lt;code&gt;user&lt;/code&gt; in the &lt;code&gt;User&lt;/code&gt; class the &lt;code&gt;params&lt;/code&gt; taken in as the &lt;code&gt;username&lt;/code&gt; will be the &lt;code&gt;username&lt;/code&gt; and same for the &lt;code&gt;password&lt;/code&gt;. Then the instance of that &lt;code&gt;user&lt;/code&gt; will be rendered in JSON and have a &lt;code&gt;status&lt;/code&gt; of &lt;code&gt;created&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next is generating the &lt;code&gt;user&lt;/code&gt; model. Remember the controller is always &lt;em&gt;plural&lt;/em&gt; and the model is &lt;em&gt;singular&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;rails g model user&lt;/code&gt; in the terminal&lt;/p&gt;

&lt;p&gt;The only thing we have to do in there is tell it the &lt;code&gt;User&lt;/code&gt; class has a secure password by simply:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnsf4pjzqy3jnyqptbk0v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnsf4pjzqy3jnyqptbk0v.png" alt="Alt Text" width="558" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now in our migration file we must add &lt;code&gt;t.string :username&lt;/code&gt; and &lt;code&gt;t.string :password_digest&lt;/code&gt;. &lt;code&gt;password_digest&lt;/code&gt; is &lt;code&gt;bcrypt&lt;/code&gt; magic and what hashes the &lt;code&gt;password&lt;/code&gt;. Essentially, all we are doing here is saying the &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;password&lt;/code&gt; will be strings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqkrsv0ei4fwug9htsooi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqkrsv0ei4fwug9htsooi.png" alt="Alt Text" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Last step is to migrate the file by running &lt;code&gt;rails db:migrate&lt;/code&gt; in the terminal and we should be able to create a User and store a hashed password. Run &lt;code&gt;rails s&lt;/code&gt; in the terminal to start your server and let's try it out in Postman.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwwemc3dim2mibs36g0xu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwwemc3dim2mibs36g0xu.png" alt="Alt Text" width="800" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8mu1emqvw5pl15qo17qu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8mu1emqvw5pl15qo17qu.png" alt="Alt Text" width="800" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above, we see that I have created a &lt;code&gt;User&lt;/code&gt; with &lt;code&gt;username&lt;/code&gt; "jonas" and &lt;code&gt;password&lt;/code&gt; "jonas1!" and after the creation of this &lt;code&gt;User&lt;/code&gt; we can see that it would be extremely hard to decipher the &lt;code&gt;password_digest&lt;/code&gt; back to "jonas1!".&lt;/p&gt;

&lt;p&gt;And there you have it, creating users and a hashed password! Thanks for reading &amp;amp; as always, I'd appreciate any input, questions or comments.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
