<?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: Alex McKean</title>
    <description>The latest articles on DEV Community by Alex McKean (@amckean12).</description>
    <link>https://dev.to/amckean12</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%2F102814%2Fb489d17b-bbed-49de-bf3e-71f3978377e0.png</url>
      <title>DEV Community: Alex McKean</title>
      <link>https://dev.to/amckean12</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amckean12"/>
    <language>en</language>
    <item>
      <title>Designing a Relational Database for a Cookbook</title>
      <dc:creator>Alex McKean</dc:creator>
      <pubDate>Tue, 02 Jul 2019 14:48:09 +0000</pubDate>
      <link>https://dev.to/amckean12/designing-a-relational-database-for-a-cookbook-4nj6</link>
      <guid>https://dev.to/amckean12/designing-a-relational-database-for-a-cookbook-4nj6</guid>
      <description>&lt;p&gt;When I'm not programming I'm cooking. I love looking through cookbooks and finding new and interesting recipes. Having browsed through my fair share of books I started to notice a pattern in the types of recipe attributes displayed. I thought it might be a good exercise to design a relational database based on cookbook recipes. &lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;So what makes up a relational database? Relations! In a relational database everything is connected. With all things being connected we could just create one large table with hundreds of attributes types, but we don't. Why is that? Data Integrity! Data integrity is the overall completeness, accuracy and consistency of our data. In order for our database to have Data integrity it needs to have these three features. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No Repeating Values&lt;/li&gt;
&lt;li&gt;No Incorrect Values &lt;/li&gt;
&lt;li&gt;No Broken Relationships between tables&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So how can we achieve that? We do so by breaking up the data into multiple, manageable, and unique tables. We then connect these tables through relations. When it comes to relations there are only three types we can use (really two, but we'll get to that). These types are: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;One to One - (ex: One person has one Passport and one Passport has one Person)&lt;/li&gt;
&lt;li&gt;One to Many (ex: One Person can have many credit cards but many credit cards can only have one person)&lt;/li&gt;
&lt;li&gt;Many to Many (ex: Many classes have many students while many students have many classes) &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That last one seemed hard to read and understand right? It is. Plus it doesn't work. The reason it doesn't work has to do with a concept called Parent-Child relationships. Lets look at the "one-to-many" example and try to figure out who is the parent and who is the child. Out of the Person and the Credit card which can't exist without the other? The credit card! There are no magical limitless credit cards floating around that aren't assigned to someone. So, in this scenario, the parent is the person and the card is the child. Now lets look at the "many-to-many" relationship. Whose the parent and child there? On one case a student can't be a student without attending a class. On the other case is a class really a class without students? Here lies why many-to-many relationships don't work. We have no clear parent. &lt;/p&gt;

&lt;p&gt;We solve this problem by breaking up the "many-to-many" relationship into two "one-to-many" relationships. This way we have a clear parent and child. So if we were to model the students and classes tables we'd have something like this. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedn5v20d9oquat2xf454.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedn5v20d9oquat2xf454.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Where the "Student-Classes" table would be a child to both the student table and the class table. &lt;/p&gt;

&lt;h2&gt;
  
  
  DB Design
&lt;/h2&gt;

&lt;p&gt;Okay, so armed with this knowledge let's build out a database for cookbook recipes! Let's take a look at a recipe and pull out the relevant information. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Recipe Title: Simple Cheese Pizza
Recipe Description: A great pizza that's really easy to make. 
Recipe Ingredients:
2.5 cups All Purpose Flour
1 Packet Yeast 
1.5 teaspoons Sugar
0.75 teaspoons salt
2 tablespoons Olive Oil
0.75 cups warm water
1.25 cups cheese
1 cup tomato sauce


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

&lt;/div&gt;

&lt;p&gt;This seems like enough information for us to start with. Looking at the recipe above what data is truly dependent on a single recipe? I would argue the "Recipe Title" and the "Recipe Description", so let's put that into a table. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2tx5lrut47sep8u8wjzt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2tx5lrut47sep8u8wjzt.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay so now let's look at the ingredients section. First, we know that a recipe has many ingredients, but also an ingredient has many recipes. I can use 2.5 cups of flour in another recipe, so there is no need to repeat that value again in my database. We can also argue that I can use 2.5 cups of some other ingredient in my database, like 2.5 cups of brown sugar. So is there a need to continuously repeat 2.5 cups in a database? I'd argue not. &lt;/p&gt;

&lt;p&gt;Let's work with an individual ingredient and expand that to all ingredients for a recipe. When looking at an ingredient we notice some key features, we have: measurement quantity, measurement unit, and ingredient. Below show this with "1 cup tomato sauce".&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

1 = measurement qty
cup = measurement unit
tomato sauce = ingredient


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

&lt;/div&gt;

&lt;p&gt;We could then create three tables based on these three features. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfuhqkxyncqd25xld9re.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfuhqkxyncqd25xld9re.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, so we have all our information represented, but how do we connect that recipe table with the three ingredient tables. We use a join table! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcu3abww8k6uupzc2g22f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcu3abww8k6uupzc2g22f.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What does this database allow us to do? Well, let's take a look at two pizza recipes. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Recipe Title: Simple Cheese Pizza
Recipe Description: A great pizza that's really easy to make. 
Recipe Ingredients:
2.5 cups All Purpose Flour
1 Packet Walmart Yeast 
1.5 teaspoons Sugar
0.75 teaspoons salt
2 tablespoons Olive Oil
0.75 cups warm water
1.25 cups cheese
1 cup tomato sauce


Recipe Title: Simple Cheese Pizza with Pepperoni
Recipe Description: A great pizza with pepperoni that really easy to make. 
Recipe Ingredients:
2.5 cups All Purpose Flour
1 Packet Walmart Yeast 
1.5 teaspoons Sugar
0.75 teaspoons salt
2 tablespoons Olive Oil
0.75 cups warm water
1.25 cups cheese
1 cup tomato sauce
1 bag pepperoni



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

&lt;/div&gt;

&lt;p&gt;We know that our recipe title and recipe description are different hence we need to store both those values, but look at the ingredients. Nothing really changes except for the "1 bag pepperoni" at the end. Do we really need to duplicate all those values? Well what would happen if we did? Let's look at "1 Packet Walmart Yeast". If Walmart went out of business odds are Walmart Yeast would cease to exist. So we'd have to find a substitute for that yeast and then change not one, but two records. That doesn't seem that hard, but now imagine that we had 10,000 recipes using Walmart Yeast. That's a lot of changing. With the above database we'd only have to change one table value instead of 10,000! Thank you relations!&lt;/p&gt;

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

&lt;p&gt;There is no single right answer to database design, but there are a lot of wrong answers. Database design is equal parts art and science. It requires having a deep understanding of the relationships between all the data you are trying to connect. I have found when I don't focus enough time on proper database design my apps become inflexible and inefficient. At the beginning the concept was tricky for me to grasp, especially modeling many-to-many relationships. I'm curious on your thoughts on database design and if its worth spending a large amount of time on. Let me know!&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>database</category>
    </item>
    <item>
      <title>Building a FullStack App in a Month. Here are some lessons I learned. </title>
      <dc:creator>Alex McKean</dc:creator>
      <pubDate>Wed, 19 Jun 2019 19:14:53 +0000</pubDate>
      <link>https://dev.to/amckean12/building-a-fullstack-app-in-a-month-here-are-some-lessons-i-learned-bo</link>
      <guid>https://dev.to/amckean12/building-a-fullstack-app-in-a-month-here-are-some-lessons-i-learned-bo</guid>
      <description>&lt;p&gt;Over the past month I really challenged my self to take an idea in my head and build out a product. I wanted to build something that not only could be shown off to potential employers, but something I could eventually use.  The app is a cookbook where a user can: search for recipes, add searched recipes to a menu, then generate a shopping list based on those recipes. To build this app the main tools I used were React, Redux, Rails, and PostgreSQL. I learned a lot going through this process, but there are three main lessons that stood out. &lt;/p&gt;

&lt;h1&gt;
  
  
  Don’t be afraid to Break Things
&lt;/h1&gt;

&lt;p&gt;You will crash your app. When this happens don’t panic, take the time and work through the problem. Some problems will take an hour to solve, some will take weeks. Don’t let the fear of tackling a problem for a week stop you from implementing something new. Initially, I let this happen. I took out features from my app because they lacked a clear defined path for implementation. My app was losing the core things that made it cool because I didn’t know how to build them. When you strip away all the features that make your app cool what you're left with is a glorified todo list, and no one wants to spend a month building that. &lt;/p&gt;

&lt;p&gt;There's no harm in making a new branch and experimenting/testing some ideas. I learned more trying and breaking things than I ever did on set lesson plans. Worst case scenario switch back over to your stable branch with no harm done. &lt;/p&gt;

&lt;h1&gt;
  
  
  Read Up on the Libraries Your Implementing into Your App
&lt;/h1&gt;

&lt;p&gt;Whenever you are implementing an external library in your app, read up on it. I spent many hours trying to solve problems that could have been fixed with a quick glance at a repo’s issues page. Quality of an external library’s docs was a big factor, for me, on whether I'd use it or not.  This was my process in researching certain libraries. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read the official documentation for the library &lt;/li&gt;
&lt;li&gt;Check out the repos Issues&lt;/li&gt;
&lt;li&gt;Look up questions related to the library on stack overflow&lt;/li&gt;
&lt;li&gt;Read articles about the library&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Initially, I felt this process was excessive. It felt I was wasting time reading instead of actually trying to implement with code. What I found was that I had a better understanding of the library, and when it came time to actually use the library I knew potential problems that could arise and where to look to solve them. &lt;/p&gt;

&lt;h1&gt;
  
  
  Design, Design, Design!!!
&lt;/h1&gt;

&lt;p&gt;Please, if there is one thing to take away from this post it is this, spend time designing. My design process was: sketch out what the app would look like, then jump into code. I suffered. Without proper design you can really lose control of your apps. Here are some problems I had that were attributed to poor design. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Duplicate components&lt;/li&gt;
&lt;li&gt;Too many class based components the could have been functional&lt;/li&gt;
&lt;li&gt;Unnecessary CSS&lt;/li&gt;
&lt;li&gt;Duplicate code&lt;/li&gt;
&lt;li&gt;A lot of unnecessary local state&lt;/li&gt;
&lt;li&gt;A way too complicated file tree&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As of today my app is functioning and works, but maintaining it would definitely be difficult. For me, I think a good exercise would be a rewrite with an emphasis on design and maintainability using tools like esLint and standards like airBNB style guide.&lt;/p&gt;

&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;Overall, I really enjoyed the process. It really is satisfying taking an idea in your head and transforming it into a product. Even more satisfying were the lessons I learned along the way. If you have any questions or want to share things you've learned feel free to reach out! &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>career</category>
      <category>productivity</category>
    </item>
    <item>
      <title>User Authentication for a Rails API using Knock</title>
      <dc:creator>Alex McKean</dc:creator>
      <pubDate>Tue, 11 Jun 2019 17:28:24 +0000</pubDate>
      <link>https://dev.to/amckean12/user-authentication-for-a-rails-api-and-a-react-client-part-1-server-side-3fej</link>
      <guid>https://dev.to/amckean12/user-authentication-for-a-rails-api-and-a-react-client-part-1-server-side-3fej</guid>
      <description>&lt;p&gt;The goal of this post is to set up user authentication using a JSON web token (JWT) for a Rails API. &lt;/p&gt;

&lt;p&gt;Normally, when I am working with rails I use a session based authentication. This means I let my server side do all the heavy lifting. With token based authentication I'm not gonna store anything on the server, but I will create a unique encoded token that will be needed anytime I'm accessing the backend. This token will be placed in the header of my requests (as a credential) and be used anytime I need to authenticate a user. To begin there are really two routes I can take. I can build my own token based authentication solution in rails (reinventing wheels), or I can see what open source solutions are available. We're gonna see what's available.&lt;/p&gt;

&lt;p&gt;A quick Google search of "rails api JWT authentication gem" should lead us to Knock. &lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/nsarno"&gt;
        nsarno
      &lt;/a&gt; / &lt;a href="https://github.com/nsarno/knock"&gt;
        knock
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Seamless JWT authentication for Rails API
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
knock&lt;/h1&gt;
&lt;p&gt;&lt;a href="http://badge.fury.io/rb/knock" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/cba6fd7ecf999f31e72b687c4f27e45ed8f3afe0/68747470733a2f2f62616467652e667572792e696f2f72622f6b6e6f636b2e737667" alt="Gem Version"&gt;&lt;/a&gt;
&lt;a href="https://travis-ci.org/nsarno/knock" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/15d3c190c2db70796d86cf8b3e619a6d3f03f898/68747470733a2f2f7472617669732d63692e6f72672f6e7361726e6f2f6b6e6f636b2e737667" alt="Build Status"&gt;&lt;/a&gt;
&lt;a href="https://codeclimate.com/github/nsarno/knock" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/ede78e4c1fb5a1167be904d5f3f8f2ecc12de86f/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6e7361726e6f2f6b6e6f636b2f6261646765732f6770612e737667" alt="Code Climate"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Seamless JWT authentication for Rails API&lt;/p&gt;
&lt;h2&gt;
Description&lt;/h2&gt;
&lt;p&gt;Knock is an authentication solution for Rails API-only application based on JSON Web Tokens.&lt;/p&gt;
&lt;h3&gt;
Why should I use this?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;It's lightweight.&lt;/li&gt;
&lt;li&gt;It's tailored for Rails API-only application.&lt;/li&gt;
&lt;li&gt;It's &lt;a href="https://en.wikipedia.org/wiki/Representational_state_transfer#Statelessness" rel="nofollow"&gt;stateless&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;It works out of the box with services like &lt;a href="http://auth0.com/" rel="nofollow"&gt;Auth0&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
Is this being maintained?&lt;/h3&gt;
&lt;p&gt;&lt;del&gt;Yes&lt;/del&gt; &lt;del&gt;No&lt;/del&gt; Yes!&lt;/p&gt;
&lt;p&gt;Currently being maintained with the help of @andrerpbts ❤️&lt;/p&gt;
&lt;p&gt;Slowly moving through the backlog of issues &amp;amp; PRs and starting to build a roadmap for the next version.&lt;/p&gt;
&lt;h2&gt;
Getting Started&lt;/h2&gt;
&lt;h3&gt;
Installation&lt;/h3&gt;
&lt;p&gt;Add this line to your application's Gemfile:&lt;/p&gt;
&lt;div class="highlight highlight-source-ruby"&gt;&lt;pre&gt;&lt;span class="pl-en"&gt;gem&lt;/span&gt; &lt;span class="pl-s"&gt;'knock'&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then execute:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ bundle install
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;
Requirements&lt;/h3&gt;
&lt;p&gt;Knock makes one assumption about your user model:&lt;/p&gt;
&lt;p&gt;It must have an &lt;code&gt;authenticate&lt;/code&gt; method, similar to the one added by &lt;a href="http://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html#method-i-has_secure_password" rel="nofollow"&gt;has_secure_password&lt;/a&gt;.&lt;/p&gt;
&lt;div class="highlight highlight-source-ruby"&gt;&lt;pre&gt;&lt;span class="pl-k"&gt;class&lt;/span&gt; &lt;span class="pl-v"&gt;User&lt;/span&gt; &amp;lt; &lt;span class="pl-v"&gt;ActiveRecord&lt;/span&gt;::&lt;span class="pl-v"&gt;Base&lt;/span&gt;
  &lt;span class="pl-en"&gt;has_secure_password&lt;/span&gt;
&lt;span class="pl-k"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using &lt;code&gt;has_secure_password&lt;/code&gt; is recommended, but you don't have to as long as your user model implements an &lt;code&gt;authenticate&lt;/code&gt; instance…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/nsarno/knock"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Knock was built solely to provide an authentication solution for Rails APIs using JWTs, exactly what we want. Knock has a very good Readme so to begin we're just gonna follow that. &lt;/p&gt;

&lt;p&gt;First thing we're gonna do is add the necessary gems to our gemfile. These gems are... &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;gem 'knock' # Authentication Solution&lt;/li&gt;
&lt;li&gt;gem 'bcrypt', '~&amp;gt; 3.1.7' # To hash our passwords&lt;/li&gt;
&lt;li&gt;gem 'active_model_serializers' #Used to serialize our data (Note: As of this writing AMS is undergoing large development changes, if looking for another solution I'd recommend fast_jsonapi)&lt;/li&gt;
&lt;li&gt;gem 'jwt' #Ruby implementation of RFC 7519 OAuth JWT standard&lt;/li&gt;
&lt;li&gt;gem 'rack-cors' #this is a necessary for communication with our client&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next lets run bundle install. Now we are ready to start implementing Knock. First thing we are going to do is create a knock initializer, this will contain all the information about our configuration options in relation to knock. &lt;/p&gt;

&lt;p&gt;$ rails generate knock:install&lt;/p&gt;

&lt;p&gt;The initializer file will look as so in its simplest form... &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8awXvSb4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ey872mythxunuy5yq99o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8awXvSb4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ey872mythxunuy5yq99o.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: When generating this initializer we are given a bunch of configuration options. These options are a good idea to implement, but for the sake of this post we are gonna keep it simple. &lt;/p&gt;

&lt;p&gt;Next thing we need to do is implement a Token Controller. This will provide a way for our users to sign in. &lt;/p&gt;

&lt;p&gt;$ rails generate knock:token_controller user&lt;/p&gt;

&lt;p&gt;After generating this controller we will also need to add a login route to our routes file. This would be a good time to talk about namespacing. A really good way to keep your code clean and organized is to name space it. For this project we are gonna prepend all of our routes with /api/. To accomplish this, we want to use namespace :api in our routes file as well as nest all of our controllers under an api folder. The route file at this point is shown below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EqO4uPId--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/zt6ycmeblp7b2fqurdhy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EqO4uPId--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/zt6ycmeblp7b2fqurdhy.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While our user token controller at controllers/api/user_token_controller.rb will look like this. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0pNLVtwY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/x9ftdfipcnkfhb43ly41.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0pNLVtwY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/x9ftdfipcnkfhb43ly41.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: For this project we are using Rails 5.2 we need to include the skip_before_action :verify_authenticity_token. If we do not include that in the UserTokenController we will get a 422 response from our server when we try to log in. I found this out looking at the issues on github. It is always a good idea to look at those before implementing. &lt;/p&gt;

&lt;p&gt;Now the next steps are to implement the model, controller, serializer, and accompanying migration for a user. To do this I used a scaffold generator. &lt;/p&gt;

&lt;p&gt;For this application our user will have the following data... &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o9Pj0dLp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/t2hcwnmi3sapnxaenxd6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o9Pj0dLp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/t2hcwnmi3sapnxaenxd6.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The model looks as follows. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j-wGo1Cu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/uev4igvk71ssnmq1smzh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j-wGo1Cu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/uev4igvk71ssnmq1smzh.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we have above are validations for the presence of all parameters, as well as a unique email, and a password of minimum length of 7. We also have the method has_secure_password. This is critical for knock to work. According to the documentation Knock makes one assumption about our user model. It must have an authenticate method, similar to has_secure_password. We are able to call this method due to our bcrypt gem. It is recommended to just use has_secure_password, but we don't have to as long as our user model implements an authenticate method with the same behavior. &lt;/p&gt;

&lt;p&gt;We also need user routes in our route file. For now, we will have all the user routes available to us. Our routes file will now look like this. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MemwB567--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ugomfiih4z2ylmja4581.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MemwB567--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ugomfiih4z2ylmja4581.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next we need to work on the controller. Since we used a scaffold generator a lot of our method are created. We will, however, need to make two methods. The methods are&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;find #A method that we will use to find a user based on an email&lt;/li&gt;
&lt;li&gt;set_user #A method that will set the user. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These methods are shown below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jYj_Gl3V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0pu3jlxezo45gumz3fre.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jYj_Gl3V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0pu3jlxezo45gumz3fre.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FlU_IPvd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/p7ejb54oztmzjzcamwve.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FlU_IPvd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/p7ejb54oztmzjzcamwve.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will also need to add a find_user route to our routes file. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jIX-B8Nk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5cai8siwvqgwysd4dkuy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jIX-B8Nk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5cai8siwvqgwysd4dkuy.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By this point we should have what we need to log in a user. So let's create a user and use Postman to log them in. &lt;/p&gt;

&lt;p&gt;In my seeds file &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qsY2HZaL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mpqhjxmss2whwew0wemz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qsY2HZaL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mpqhjxmss2whwew0wemz.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then in postman...&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Headers &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mR7xwvMb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/w33gvqvh7nv1hb3yxf19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mR7xwvMb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/w33gvqvh7nv1hb3yxf19.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Body&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vZvJo1fB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/aj8rqiwl7rpu83yx8fxi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vZvJo1fB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/aj8rqiwl7rpu83yx8fxi.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And what we will get back will be our token. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V0ksvFpV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7xngi3i0j9bn7aks7kc3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V0ksvFpV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7xngi3i0j9bn7aks7kc3.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we would then do is place that token in the header of our request as a credential, and every time we want to access a resource the controller in charge of that resource would authenticate the user. An example of this is shown below in a controller for one of my projects. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rt6HwnHZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/en9sk5pfaa30mq87yl8y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rt6HwnHZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/en9sk5pfaa30mq87yl8y.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What I hope to do next is take the inputs we did in postman and replicate that in React. I plan on writing that post up soon! Thanks for reading and if you have any questions please let me know!&lt;/p&gt;

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