<?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 Eth3rnit3 on Medium]]></title>
        <description><![CDATA[Stories by Eth3rnit3 on Medium]]></description>
        <link>https://medium.com/@eth3rnit3?source=rss-1e4ae1f5813b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ee-iwBWyGQyDsduKQ1iDjQ.jpeg</url>
            <title>Stories by Eth3rnit3 on Medium</title>
            <link>https://medium.com/@eth3rnit3?source=rss-1e4ae1f5813b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 02:40:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eth3rnit3/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[The better Ruby on rails template]]></title>
            <link>https://medium.com/@eth3rnit3/the-better-ruby-on-rails-template-38af86c4fd3e?source=rss-1e4ae1f5813b------2</link>
            <guid isPermaLink="false">https://medium.com/p/38af86c4fd3e</guid>
            <category><![CDATA[ruby-on-rails-development]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Eth3rnit3]]></dc:creator>
            <pubDate>Thu, 06 Oct 2022 14:26:48 GMT</pubDate>
            <atom:updated>2022-10-06T14:26:48.602Z</atom:updated>
            <content:encoded><![CDATA[<p>After years of development with ruby on rails, I have realized dozens of personal and professional projects. With time, I got used to it, I always use the same gems at the beginning of an application, most of them needing some configuration.</p><p>I’ve known about rails templates for a long time but I always thought that it wasn’t very useful, that in view of the evolution of things, maintaining the template would be as costly as taking a little time to reconfigure an app from scratch.</p><p>That’s where I was wrong, because one of the strengths of Ruby on Rails is the fact that many developers are conscientious and try to follow and keep the conventions of the framework. What makes that in spite of the evolution, the configuration and the use of the gems vary only very rarely.<br>It is thus following this observation that I finally decided to create “The template” rails-api which allows me to generate a pre-configured app in a few seconds only.</p><p>I also made sure that the template generates all the necessary docker config. Gathered in a docker-compose, the backend (api), a database server (only <strong>postgres</strong> for the moment), a redis server and a worker which only remains to connect to the techno of my choice for the asynchronous tasks.<br>For the moment, the scalable configuration is quite limited, but all doors are open to go much further. If any reader of this article is interested in contributing to the project, I would be very happy.</p><p><a href="https://github.com/Eth3rnit3/rails_api_template">GitHub - Eth3rnit3/rails_api_template: Rails api template it make ready to code</a></p><p>happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38af86c4fd3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sequelize relationships — Ultimate guide]]></title>
            <link>https://medium.com/@eth3rnit3/sequelize-relationships-ultimate-guide-f26801a75554?source=rss-1e4ae1f5813b------2</link>
            <guid isPermaLink="false">https://medium.com/p/f26801a75554</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[sequelize]]></category>
            <dc:creator><![CDATA[Eth3rnit3]]></dc:creator>
            <pubDate>Wed, 28 Nov 2018 16:54:29 GMT</pubDate>
            <atom:updated>2023-11-23T22:09:30.365Z</atom:updated>
            <content:encoded><![CDATA[<h3>Sequelize relationships — Ultimate guide</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*fygqfizATFjt6ALek2W1jg.png" /><figcaption>source <a href="https://hackernoon.com/integrating-rabbitmq-with-nodejs-3f3u32cx">https://hackernoon.com/integrating-rabbitmq-with-nodejs-3f3u32cx</a></figcaption></figure><p>A few weeks ago I started developing web applications with Node.JS and Express. This framework if you can call it that and very disconcerting at first, especially for a developer like me who has been studying with Ruby on Rails. But after several weeks of use, I realized that Node.JS is Express are wonderful.</p><p>However, I find that despite the popularity of Node.JS it is still difficult to find examples of structured applications. Thanks to the great flexibility of Node.JS we find lots of ways to do it but very few of them have a clean and maintainable code.</p><p>The biggest difficulty I have encountered is the management of database relationships. While sequelize presents itself as an essential ORM of Node.JS, the documentation and resources that can be found often leave much to be desired. That’s why after spending a lot of time managing this relationship, I decided to write this article in order to detail the implementation of all types of relationships possible thanks to Sequelize</p><h3>The 3 types of relationship</h3><p>We will take as an example 3 models. <br>- Company<br>- Employee<br>- Working day<br>Employe has one company (belongsTo) 1:1<br>Company has many employees (hasMany) 1:n<br>Employe has many WorkingDay and WorkingDay has many employees (manyToMany) n:n</p><p>To create many to many relationship, we will need a joining table that we will call WorkingDaysEmploye.</p><h3>Express Generator and Sequelize CLI</h3><p>I know that some people criticize the express generator or the sequelize cli. Personally I find that it allows both of us to start a project by giving a very suitable starting structure.<br>If not already done, globally install express-generator and sequelize-cli like this</p><pre>npm install express-generator -g <br>npm install -g sequelize-cli</pre><p>Then we will generate a new app thanks to express and install the necessary libraries</p><pre>express relationship-app-demo --no-view<br>cd relationship-app-demo<br>npm install --save sequelize mysql2<br>sequelize init</pre><pre>The last command will create 3 folders (Models, Migrations and Seeder) and a `config.json` file in the `/config` directory</pre><p>We need to modify this file with your credentials to the local mysql server like this</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8bf05a1152b9351f8b8d8e8298b87dde/href">https://medium.com/media/8bf05a1152b9351f8b8d8e8298b87dde/href</a></iframe><p>after configuration you can run <br>`sequelize db:create` to create a new database named `nodejs_relationship_demo`</p><h3>Models generation</h3><p>now, we’re going to create all the models we’re going to need</p><pre>sequelize model:generate --name Company --attributes name:STRING</pre><pre>sequelize model:generate --name User --attributes email:STRING,firstName:STRING,lastName:STRING,companyId:INTEGER</pre><pre>sequelize model:generate --name WorkingDay --attributes weekDay:STRING,workingDate:DATE,isWorking:BOOLEAN</pre><pre>sequelize model:generate --name UsersWorkingDay --attributes userId:INTEGER,workingDayId:INTEGER</pre><p>UsersWorkingDay will be used to join User and WorkingDay (many to many relationship).<br>Now that we have generated the necessary templates, we will have to indicate the relationships in the migration files by adding a reference key on all foreign keys.<br>I also added for each foreign key an AllowNull false to prevent creation if the model is not linked</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c496f74f684363c6f458327865163619/href">https://medium.com/media/c496f74f684363c6f458327865163619/href</a></iframe><p>You can then check with any database explorer that the tables are linked to each other.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ssd1pTPfoS7hbZT6HeL9mw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YkTCy483-pw-qNSnI6-63A.png" /></figure><p>The relationships being well defined on the mysql side, we will now have to explain to sequelize what the relationships are between our models</p><h3>Models associations</h3><p>1:1</p><p>The first relationship is a User belongs to a Company, open the `models/user.js` and add the association line 10 like this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f832ce53bf66b3a6f3ed7e68f4868d72/href">https://medium.com/media/f832ce53bf66b3a6f3ed7e68f4868d72/href</a></iframe><p>1:N</p><p>Knowing that a company has several Users we can add the 1:n relationship in the Company model. I rename the user model using an alias (employes) for more clarity, I put this alias in the plural because the relationship is hasMany</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98936d1b6e43264a3302d4d1332125b8/href">https://medium.com/media/98936d1b6e43264a3302d4d1332125b8/href</a></iframe><p>N:N</p><p>Finally, there is only one relationship left to manage and probably the most difficult. For this, we will need to describe the association in the 3 files concerned. (user.js, workingday.js, usersworkingday.js)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fd24426a745a78d3e250e723a04088e/href">https://medium.com/media/3fd24426a745a78d3e250e723a04088e/href</a></iframe><p>And that’s it, we configured the database and the sequelize models with the 3 types of relationships. It may seem simple after all, but before that, long hours of research were needed.</p><h3>How to test relationships</h3><p>The last thing to do once the configuration is finished, is to test the relationships between them to make sure that everything works as you want it to.<br>For that, I create a new test file that I will delete later</p><pre>touch relationshipTester.js</pre><p>First, import all the models we need</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9867c782f5062205a19855130ec0014/href">https://medium.com/media/c9867c782f5062205a19855130ec0014/href</a></iframe><p>The first registration we will need is Company, so let’s start by creating a new company.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0cf8465e4cd02a805f0c9515ce91093/href">https://medium.com/media/d0cf8465e4cd02a805f0c9515ce91093/href</a></iframe><p>Now, we will create 3 Users (employees) linked to this company</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10f276bcfeeec3beae14c81aade44118/href">https://medium.com/media/10f276bcfeeec3beae14c81aade44118/href</a></iframe><p>Let’s see now if we can recover a company from an User and Users from a Company</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4605e6e439ea5c74c822290504f87714/href">https://medium.com/media/4605e6e439ea5c74c822290504f87714/href</a></iframe><p>Everything here is perfect! All that remains is to test the many to many relationship in this way.<br>Let’s start by creating 3 working days</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9d4f4669493fd606cda2e2383af7404/href">https://medium.com/media/d9d4f4669493fd606cda2e2383af7404/href</a></iframe><p>Finally, we want to be able to retrieve the list of employees for a given work day, but also to know the work days of a given employee.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ed5b989cecd124252f9e66713002b5a/href">https://medium.com/media/6ed5b989cecd124252f9e66713002b5a/href</a></iframe><h3>Conclusion</h3><p>First I would like to apologize for the horror mistake that is in the code, I realized when writing this article that in English employee to take two ‘e’. (sorry, I’m French)</p><p>I would then say that what we need to remember is that when associating in models, it is a good practice to use aliases (as:) because sequelize pluralizes the name of models and it can sometimes bring surprises, by using aliases, you rename the associations which then allows you to use this alias in include. Sequelize also create functions for each association, we also use aliases in singular or plural to call these functions. The list is available <a href="http://docs.sequelizejs.com/manual/tutorial/associations.html">here in the documentation</a>.</p><p>Finally, during an belongsToMany association it is not mandatory but specifying the foreign key ensures a good connection. It also happened to me that Sequelize does not associate the model with the right table if it happens to you, you can specify a classname in the second parameter of the model definition.</p><p>Eth3rnit3</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f26801a75554" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CRUD React-Native & RoR backend with Devise Auth Token]]></title>
            <link>https://medium.com/@eth3rnit3/crud-react-native-ror-backend-with-devise-auth-token-4407cac3aa0b?source=rss-1e4ae1f5813b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4407cac3aa0b</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[devise-auth-token]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Eth3rnit3]]></dc:creator>
            <pubDate>Fri, 11 May 2018 23:22:26 GMT</pubDate>
            <atom:updated>2018-05-11T23:22:26.982Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X02pP9P6XmFdLU5S0qT52w.jpeg" /></figure><p>I will write this tutorial in 2 parts, the first will concern the Ruby on Rails backend and the second will concern the mobile application with React-Native.</p><p>This tutorial aims to show how to manage the identification with <a href="https://github.com/lynndylanhurley/devise_token_auth">Devise Auth Token</a> gem and the realization of a CRUD on the backend that is manipulable from the mobile application.</p><h3>1st Part — Backend</h3><p>I started from <a href="https://github.com/lewagon/rails-templates">Le Wagon template</a> to save myself a bit of configuration and act as if I wanted to implement an API in an existing webapp.</p><p>First, I create an Appointment model that will serve as a base for this tutorial, I assign a title and a body.<br>I create a 1:n relationship between User and Appointment so that a user can have multiple appointments. I open the console and create some test data to check that everything works as it should</p><p>It is now time to move to the creation of the API to have a CRUD on our Appointment model</p><pre>mkdir -p app/controllers/api/v1<br>touch app/controllers/api/v1/base_controller.rb <br>touch app/controllers/api/v1/appointments_controller.rb<br>mkdir -p app/views/api/v1/appointments<br>touch app/views/api/v1/appointments/index</pre><pre># base_controller.rb<br>class Api::V1::BaseController &lt; ActionController::Base<br>  respond_to :json<br>end</pre><pre># appointments_controller.rb<br>class Api::V1::AppointmentsController &lt; Api::V1::BaseController<br>  def index<br>    @appointments = Appointment.all<br>  end<br>end</pre><pre># index.json.jbuilder (using jbuilder gem)<br>json.array! @appointments do |appointment|<br>  json.extract! appointment, :id, :title, :body<br>end</pre><p>For now, don’t forget to inherit ActionController::Base to escape devise if you have a webapp that already uses devise. Then make a curl to see if you receive the appointments well.</p><pre>curl -s <a href="http://localhost:3000/api/v1/appointments">http://localhost:3000/api/v1/appointments</a> | jq</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pkwVIe1vOmHrmNlENpIdMg.png" /></figure><p>Now we don’t have to do the same thing but for the create show update and destroy action, I let you see the following link to see the <a href="https://github.com/Eth3rnit3/Rails_React_Native_Demo/blob/master/app/controllers/api/v1/appointments_controller.rb">full controller</a>, <a href="https://github.com/Eth3rnit3/Rails_React_Native_Demo/blob/master/config/routes.rb">routes</a> and <a href="https://github.com/Eth3rnit3/Rails_React_Native_Demo/tree/master/app/views/api/v1/appointments">views</a>.<br>To test the other routes I use postman to facilitate the creation of parameters and headers.<br>When the CRUD works, we can move to the implementation Devise Auth Token. <br>add gem to the Gemfile</p><pre>gem &#39;devise_token_auth&#39;</pre><pre>bundle install</pre><pre>rails g devise_token_auth:install User auth</pre><p>The <a href="https://maicolben.gitbooks.io/devise-token-auth/content/docs/config/">documentation</a> being extremely well detailed, I will not dwell on the subject but <strong>User</strong> is the model to which we will link devise and <strong>Auth</strong> will be the namespace of the route to manage identification via api.<br>Be careful if you add the gem to an existing application, you should modify the migration file generated by the install and review the devise modules included in the <em>User.rb </em>as needed. <br>Also be careful because Devise Auth Token depends on Devise Confirmable and only requires a user to be confirmed so that the token can be assigned correctly when sign_in. If you do not use this module, you can disable the check like <a href="http://Be careful if you add the gem to an existing application, you should modify the migration file generated by the install and review the currency modules included in the User.rb file as needed. Also be careful because Auth Token Currency depends on Confirmable Currency and only requires a user to be confirmed so that the token can be assigned correctly when sign_in. if you do not use this module, you can disable the check like this otherwise during sign_in an error will occur and no token will be assigned">this</a> otherwise during sign_in an error will occur and no token will be assigned</p><p>It is now necessary to modify the base controller so that it uses devise.<br>escape the CSRF check if the request is in json format and override the <a href="https://github.com/Eth3rnit3/Rails_React_Native_Demo/blob/master/app/controllers/api/v1/devise_token_auth/sessions_controller.rb">devise session controller</a> on the api route so that it does not execute authenticate_user! on new create and validate_token. <br>Modify the <a href="https://github.com/Eth3rnit3/Rails_React_Native_Demo/blob/master/config/routes.rb">routes file</a>.</p><pre># appointments_controller.rb<br>class Api::V1::BaseController &lt;  ActionController::Base  <br>  respond_to :json<br>  include DeviseTokenAuth::Concerns::SetUserByToken</pre><pre>  protect_from_forgery with: :null_session, only: Proc.new { |c| c.request.format.json? }  <br>  before_action :authenticate_user!  <br>end</pre><p>With postman’s help, we can now test our sign_in on <em>GET 127.0.0.1:3000/api/v1/auth/sign_in</em> with email and password params.<br>If the configuration is good, you will receive a status 200 with user data in the body so due params (access-token, client, uid, token-type and expiry) in headers like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XIYK3zzrTTOz1fGI-U-_mQ.png" /><figcaption>send email and password to sign_in</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/1*GHKFw1bTT219IjpZcPTtxA.png" /><figcaption>Headers response after sign_in ok</figcaption></figure><p>This token must then be validated on the POST url 127.0.0.1:3000/api/v1/auth/validate_token by sending the data received previously. like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4waSPIKlBIZqqZaHMHMLVQ.png" /><figcaption>send token received in sign_in</figcaption></figure><p>As you can see, you will receive a true status with once again the user data and a new access-token in the headers. this one will change after each new request which returns a status 200 it is therefore necessary well thought for each request to use the access-token of the previous response</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mPREU2gkBwoI6VUizE-5gg.png" /><figcaption>Token valided</figcaption></figure><p>If you are here is that everything is ok then the continuation should not pose any problem, you can start again the tests on your CRUD in order to check that everything is ok then pass to the creation of the mobile part.</p><h3>2nd Part — Mobile App</h3><p>For the realization of the mobile app, I used <a href="https://github.com/react-community/create-react-native-app">create-react-native-app</a> that depends on the <a href="https://github.com/expo/expo">Expo</a> library is that does not deal with ios and android versions because in most cases <a href="https://github.com/expo/expo">Expo</a> does for us.<br>So we find ourselves with a basic structure to start our application and an <em>App.js</em> file that is the root of the application.</p><pre>npm install --save &quot;axios&quot; &quot;native-base&quot; &quot;react-native-form-builder&quot; &quot;react-redux&quot; &quot;redux&quot; &quot;redux-actions&quot; &quot;redux-logger&quot; &quot;redux-thunk&quot;</pre><pre># list of added dependencies<br> <br>axios =&gt; For API resquest</pre><pre>native-base =&gt; To have the basic android and ios graphics components</pre><pre>react-native-cards =&gt; To quickly create cards in a scrollview</pre><pre>react-native-form-builder =&gt; To quickly create forms and retrieve entered data</pre><pre>react-navigation =&gt; Basic navigation for react-native, which allows navigation by Tabs, Stack and Drawer</pre><pre>react-redux =&gt; In order to have general state shared throughout the application</pre><pre>redux =&gt; idem</pre><pre>redux-actions =&gt; In order to define actions that will modify the states</pre><pre>redux-logger =&gt; Allows to have returns in the developer console when action is called</pre><pre>redux-persist =&gt; Enables state persistence in AsyncStorage for offline processing and automatic reconnection</pre><pre>redux-thunk =&gt; Middleware but I confess that I didn’t really understand the utility</pre><p>Let’s move on to the creation of the structure for the files concerning the login logic.<br>I first created a <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/tree/master/config/routes"><em>config/routes</em></a> folder in which I created <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/config/routes/Router.js"><em>Router.js</em></a><em> </em><a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/config/routes/AppStack.js"><em>AppStack.js</em></a><em> </em>and <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/config/routes/AuthStack.js"><em>AuthStack.js</em></a>, you will have understood, the first is the basic router that will contains all the others. <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/config/routes/AuthStack.js"><em>AuthStack.js</em></a> will be the router used for pages visible by users not logged in. <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/config/routes/AppStack.js"><em>AppStack.js</em></a>, on the other hand, will contain all the pages or even other routers, reserved for the connected user.<br>Then I created an <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/tree/master/App/views"><em>App/view</em></a> folder in which I created two <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/tree/master/App/views/connected"><em>App/view/connected</em></a> <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/tree/master/App/views/unconnected"><em>App/view/unconnected</em></a> folders, sorry I know it didn’t go great but at the time I couldn’t find anything better.<br>I chose to create a view with selection tabs for offline users. A <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/views/unconnected/LoginScreen.js">tab</a> that will contain a <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/models/forms/LoginForm.js"><em>nested login form</em></a> and another for an <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/views/unconnected/AboutScreen.js"><em>About page</em></a>.<br>I then create a <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/views/connected/RootScreen.js"><em>RootScreen</em></a> which will be the entry point when a user successfully connects.<br>Here you go :-)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*poT0QtvNpvSWfUwsEGXmow.png" /><figcaption>LoginScreen.js</figcaption></figure><p>I do some testing to see that the first router (<a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/config/routes/AuthStack.js"><em>AuthStack</em></a>) works well and that the data entered in the form is well recovered</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*4mp8BJKGQN2wf5tkHgB9PQ.png" /><figcaption>console.log(‘FORM VALUES’, formValues);</figcaption></figure><p>We are now going to integrate redux and I must say that for a beginner it is really not a part of pleasure even if in the end it is not that complicated, there is a lot of new thing at once when we do not know.<br>If I chose to integrate redux so early it’s because being given that the token will be modified at each request, I might as well build all this logic around redux. Moreover we will probably need to do api request everywhere in the application and we will need the token.</p><p>I will not get into the details because a lot of explanation exists on <a href="https://redux.js.org/basics">redux</a> and <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend">the code I wrote</a> is the simplest it is so with the <a href="https://redux.js.org/basics">documentation</a> and the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend">source code</a> you should easily understand but know that you have to :<br> 1 — To have a <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App.js#L10"><em>Provider</em></a> who is the parent component of your application, this provider contains the redux <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/store.js"><em>store</em></a> in property.<br>2 — Create a <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/store.js"><em>store</em></a> which will be the property of the provider and which will contain all your redux <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/reducers/index.js"><em>configuration</em></a><em>.</em><br>3 — To have <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/actions/auth.js"><em>Actions</em></a> that are in fact functions that can be imported and called everywhere from the application in order to be executed. An action only returns a string and an object to the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/reducers/auth.js"><em>reducer</em></a> which performs a real action with it.<br>4- To build a <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/reducers/auth.js"><em>reducer</em></a> which will be with the listening of the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/actions/auth.js"><em>actions</em></a> and according to the received message will execute a modification of the state using the received object.</p><p>I was able to read that good development practice and thanks to a little tutorial that I could find, I could easily pass all the properties and change the state of the navigation by redux.<br>By using a reducer that runs automatically at each change of navigation state, that’s it.</p><p>In order to quickly test that redux is well implemented, I create an action that will return “user_login_successfull” as well as formValues to the reducer so that it assigns the data entered as state. I would call this action from <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/commit/1f1832b0f6cf908568359b018813a3a80121066f#diff-e31982a901896856733bfe32ba27549cR39"><em>LoginScreen.js</em></a> in the login() function which is called when pressing the login button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*zLvxfAj9cqUxlXyiy90AzQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*1M1tA58U5DCvG6hd2hLvbw.png" /><figcaption>thanks redux-logger</figcaption></figure><p>It’s okay, redux is implanted and it works. It’s not bad enough, is it?<br>Let’s now go to the real login and validation of the token that follows.</p><p>Finally the logic is pretty simple, with the help of axios to perform the requests I create a <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/models/forms/LoginForm.js#L46"><em>newSession function</em></a> and <em>a </em><a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/models/forms/LoginForm.js#L62"><em>validateToken</em></a> function that will be called if the first is successful. <br>Then I create an <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/actions/auth.js#L3"><em>authSuccess action</em></a> which will be called if the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/models/forms/LoginForm.js#L74"><em>validation</em> <em>returns an OK status</em></a> and that will send at the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/reducers/auth.js#L11"><em>Auth reducer</em></a> the response containing the user datas and token for the next request.</p><p>As we can see between the previous state and the next state, all the data necessary for the identification of the user was well recovered and assigned in the state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lO9H64w3mjf6aJV2A3bSRg.png" /><figcaption>sign_in =&gt; ok, validate_token =&gt; ok, redux =&gt; ok</figcaption></figure><p>The user is now redirected to the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/views/connected/RootScreen.js"><em>RootScreen</em></a> page of the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/config/routes/AppStack.js"><em>AppStack</em></a> router. The <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/views/connected/RootScreen.js">RootScreen</a> class has simply been decorated by the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/views/connected/RootScreen.js#L36"><em>connect function</em></a> of react-redux in order to connect to the redux store and have access to the global state. A <em>console.log(this.props)</em> in the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/views/connected/RootScreen.js"><em>RootScreen</em></a> constructor proves that one has good access to the store.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*tHHwzVRaWiF-wQE1jboHlA.png" /></figure><p>The biggest is done, we have an entry point on our backend rails with an identified user. The doors are now open to go further and create any application you want. I chose to show how to interact with the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/tree/master/App/views/appointments"><em>CRUD of the Appointment model</em></a> it is not very designed and really very basic but the objective was only to show a way of doing that I think can be understood by beginners. Everything about CRUD is <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/tree/master/App/views/appointments"><em>here</em></a>, organized like Rails :-) I also use a <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/config/routes/AppointmentStack.js"><em>different router</em></a> that only concerns this part of the application.</p><p>I want to note that during my tests, I could notice that Devise Auth Token changes the token only on a request different from the previous one, example if I refreshed the index page a first time, I receive a new token, I go on a page not making an API call then return on the index, the call is well done but the token does not change and is even returned empty. so I added a small condition on each answer to verify its presence.<br>If it is well present I call the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/actions/auth.js#L10"><em>action updateAccessToken</em></a> which sends the new token to the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/reducers/auth.js#L24"><em>concerned reducer</em></a> so that redux modifies the token in memory.</p><p>Finally the last thing we need is to persist the information so that the user does not have to reconnect every time, to do this we will use <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/redux/store.js#L20"><em>redux-persist coupled to the AsyncStorage and the store</em></a> so whenever the state redux will be modified, it will be stored locally. <br>Then we can change the entry point of the app for a <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/App/views/unconnected/AuthLoadingScreen.js">new screen</a> in the <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend/blob/master/config/routes/Router.js#L17">router</a>. This will check whether the user is logged in or has deleted his session.</p><p>Souce app mobile : <a href="https://github.com/Eth3rnit3/React-native-app-to-rails-backend">https://github.com/Eth3rnit3/React-native-app-to-rails-backend</a><br>Source backend Ruby sur Rails : <a href="https://github.com/Eth3rnit3/Rails_React_Native_Demo">https://github.com/Eth3rnit3/Rails_React_Native_Demo</a></p><p>I hope I’ve done myself well and that you’ll enjoy this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4407cac3aa0b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>