<?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: Muhammad Adam</title>
    <description>The latest articles on DEV Community by Muhammad Adam (@bangadam).</description>
    <link>https://dev.to/bangadam</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%2F343964%2Fd6c3c57c-c14c-4f32-afe8-3863b7ce003e.png</url>
      <title>DEV Community: Muhammad Adam</title>
      <link>https://dev.to/bangadam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bangadam"/>
    <language>en</language>
    <item>
      <title>How to Build REST API using Lumen 8</title>
      <dc:creator>Muhammad Adam</dc:creator>
      <pubDate>Mon, 08 Aug 2022 06:39:00 +0000</pubDate>
      <link>https://dev.to/bangadam/how-to-build-rest-api-using-lumen-8-626</link>
      <guid>https://dev.to/bangadam/how-to-build-rest-api-using-lumen-8-626</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AAUdmnUs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2AGx3fX8bRkjUaYkNe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AAUdmnUs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2AGx3fX8bRkjUaYkNe.png" alt="" width="800" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;REST API is a mandatory skill that must be mastered by backend developers today, several well-known frameworks that help you develop it are Lumen. Lumen is a derivative project from Laravel that focuses on developing micro-frameworks, you could say this framework is smaller and faster. Therefore in this session, I will write for you how to create a REST API using the Lumen Framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation Lumen
&lt;/h2&gt;

&lt;p&gt;Before making a REST API of course we will first install lumen on local using composer, here is the command to install Lumen :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer create-project --prefer-dist laravel/lumen rest-api-lumen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;after the installation process is complete, it will automatically create a new folder called &lt;strong&gt;rest-api-lumen,&lt;/strong&gt; open the folder using your favorite text editor and then we will start configuring the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1 : Configuration
&lt;/h2&gt;

&lt;p&gt;Open the .env file, find the following key and adjust it as below:&lt;/p&gt;

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

DB_HOST=

DB_PORT=

DB_DATABASE=storage/database.sqlite

DB_USERNAME=

DB_PASSWORD=

CACHE_DRIVER=array

QUEUE_DRIVER=database

QUEUE_CONNECTION=sync
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;At this time we will use an &lt;strong&gt;SQLite&lt;/strong&gt; database as a place to store data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Database and Migrations
&lt;/h2&gt;

&lt;p&gt;After the configuration process is complete, we will first create the table using migration, run the command below to create a new migration file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:migration create_products_table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After that the new migration file is in the **database/migrations **folder, this time the example table that we will create is the product table which contains several attributes, namely name, price and description. follow the code below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8KcpikAj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2528/1%2ASFCB5fXvljC2Fxv6Hhu46Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8KcpikAj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2528/1%2ASFCB5fXvljC2Fxv6Hhu46Q.png" alt="" width="880" height="1106"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then follow the command below to run the process of creating the product table:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 3: Create Model
&lt;/h2&gt;

&lt;p&gt;The next step we will create a model for the product table by creating a new file called &lt;strong&gt;Product.php&lt;/strong&gt; in the &lt;strong&gt;app/Models&lt;/strong&gt; folder and then following the code below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jg9cnDjv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AfIhl1TEd4Kw5__RZlV5Czw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jg9cnDjv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AfIhl1TEd4Kw5__RZlV5Czw.png" alt="" width="880" height="820"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unlike laravel, our lumen framework must first configure when using Eloquent and Facades by opening the &lt;strong&gt;bootstrap/app.php&lt;/strong&gt; file, then uncommenting the following code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iTOzDoxh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AMXN1qsOmIyH4WUuoJzD2lw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iTOzDoxh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AMXN1qsOmIyH4WUuoJzD2lw.png" alt="" width="880" height="650"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 4: Create Controller
&lt;/h2&gt;

&lt;p&gt;The next step we will create a new controller file called &lt;strong&gt;ProductController.php&lt;/strong&gt;, put the file into the &lt;strong&gt;app/Http/Controller&lt;/strong&gt; folder, then follow the code below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wTvUIJWO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4624/1%2AVB-rlRxF1o6kcLKCZsjdEg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wTvUIJWO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4624/1%2AVB-rlRxF1o6kcLKCZsjdEg.png" alt="" width="880" height="1591"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 5: Setup Routes
&lt;/h2&gt;

&lt;p&gt;The next step we will customize the routes file, open the &lt;strong&gt;web.php&lt;/strong&gt; file in the &lt;strong&gt;routes&lt;/strong&gt; folder and follow the code below :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--igYExQOP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3144/1%2AYF4Fkh2B26-gAbAYZKHwIg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--igYExQOP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3144/1%2AYF4Fkh2B26-gAbAYZKHwIg.png" alt="" width="880" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the picture above we create a route url prefix with the name &lt;strong&gt;api/v1&lt;/strong&gt; then we create another prefix group for our api &lt;strong&gt;products&lt;/strong&gt;, after everything is done, it’s time for us to start testing the API to make sure it runs smoothly.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 6: Testing API
&lt;/h2&gt;

&lt;p&gt;Pada langkah terakhir ini kita akan mencoba api yang sudah kita buat sebelumnya, untuk test apinya aku memakai extensinon Thunder Client pada Visual Studi Code atau bisa memakai Postman, Sebelum test API kita jalankan terlebih dahulu lumen menggunakan perintah dibawah ini :&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php -S 0.0.0.0:8000 -t public/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;kemudian bisa dilanjutkan untuk test APInya, berikut adalah hasil dari test API :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GET All Products&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vdJPr99h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2A4VOMPc4weD3_1forF5C4Dg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vdJPr99h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2A4VOMPc4weD3_1forF5C4Dg.png" alt="" width="880" height="550"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get Product By ID&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z1Gem1nQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2Ai7P8KuMHr8QCZMKSa5D_eQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z1Gem1nQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2Ai7P8KuMHr8QCZMKSa5D_eQ.png" alt="" width="880" height="550"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create Product&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_4nfoPN---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2A01LUO595N8EsvhkLOxZTwQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_4nfoPN---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2A01LUO595N8EsvhkLOxZTwQ.png" alt="" width="880" height="550"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update Product&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---FI-YrP8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2Aitxi3qWP135ZPI1Wbe4f9g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---FI-YrP8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2Aitxi3qWP135ZPI1Wbe4f9g.png" alt="" width="880" height="550"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Delete Product&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hC-LhIli--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2AY7QO6COD_VDHhgIHxKLYWw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hC-LhIli--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2AY7QO6COD_VDHhgIHxKLYWw.png" alt="" width="880" height="550"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;We have been able to create a REST API for a product using the Lumen framework, of course, this is not enough because basically making a REST API requires other components such as Authorization, Rate Limiting, etc. You can see the source code of this article on my github &lt;a href="http://github.com/bangadam/rest-api-lumen"&gt;**account&lt;/a&gt;,** don’t forget to give a star and share this article if it is useful for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks For Reading!
&lt;/h2&gt;

&lt;p&gt;Available for a new project! Let’s have a talk : &lt;br&gt;
email : &lt;a href="//mailto:bangadam.dev@gmail.com"&gt;bangadam.dev@gmail.com&lt;/a&gt;&lt;br&gt;
LinkedIn : &lt;a href="https://www.linkedin.com/in/bangadam/"&gt;https://www.linkedin.com/in/bangadam&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>restapi</category>
      <category>lume</category>
    </item>
    <item>
      <title>Do You Need A Implementation Jwt In Go (Golang)?</title>
      <dc:creator>Muhammad Adam</dc:creator>
      <pubDate>Mon, 08 Aug 2022 06:37:55 +0000</pubDate>
      <link>https://dev.to/bangadam/do-you-need-a-implementation-jwt-in-go-golang-5cfm</link>
      <guid>https://dev.to/bangadam/do-you-need-a-implementation-jwt-in-go-golang-5cfm</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BHcLUE25--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2Ai2V-3hISf9coeGGI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BHcLUE25--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2Ai2V-3hISf9coeGGI.png" alt="" width="799" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go is becoming very popular for backend development, and JWT’s are one of the most popular ways to handle authentication on API requests. In this article, we are going to go over the basics of JWT’s and how to implement a secure authentication strategy in Go! Let’s go&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a JWT?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;JSON Web Token&lt;/strong&gt; (&lt;strong&gt;JWT&lt;/strong&gt;, pronounced &lt;a href="https://en.wikipedia.org/wiki/Help:IPA/English"&gt;/dʒɒt/&lt;/a&gt;, same as the word “jot”&lt;a href="https://en.wikipedia.org/wiki/JSON_Web_Token#cite_note-rfc7519-1"&gt;[1]&lt;/a&gt;) is a &lt;a href="https://en.wikipedia.org/wiki/Internet_Standard#Proposed_Standard"&gt;proposed Internet standard&lt;/a&gt; for creating data with optional &lt;a href="https://en.wikipedia.org/wiki/Signature_(cryptography)"&gt;signature&lt;/a&gt; and/or optional &lt;a href="https://en.wikipedia.org/wiki/Encryption"&gt;encryption&lt;/a&gt; whose &lt;a href="https://en.wikipedia.org/wiki/Payload_(computing)"&gt;payload&lt;/a&gt; holds &lt;a href="https://en.wikipedia.org/wiki/JSON"&gt;JSON&lt;/a&gt; that asserts some number of &lt;a href="https://en.wikipedia.org/wiki/Claims-based_identity"&gt;claims&lt;/a&gt;. The tokens are signed either using a private secret or a &lt;a href="https://en.wikipedia.org/wiki/Public-key_cryptography"&gt;public/private key&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;JWTs have essentially encoded JSON objects that have been signed by the server, proving their authenticity.&lt;/p&gt;

&lt;p&gt;For example, when a user login into a website secured via JWTs, the flow should look something like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The user sends a username and password to the server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server verifies username and password are correct&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server creates a JSON object that looks like this: &lt;br&gt;
{“username”: “adam”}&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server encodes and signs the JSON object, creating a JWT:&lt;br&gt;
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The user’s web client saves the JWT for later use&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the user makes a request to a protected endpoint, the JWT is passed along in an HTTP header&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server checks the signature on the JWT to make sure the JWT was originally created by the same server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server reads the claims and gives permission to the request to operate as “adam”&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Create a JWT&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before we create a JWT, we are going to use a library for dealing with JSON Web Token in go using &lt;a href="https://github.com/golang-jwt/jwt"&gt;golang-jwt&lt;/a&gt;. Make sure you have the code cloned in your local by running the below command :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go get github.com/golang-jwt/jwt/v4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This implies that we presume the server that generates the JWTs will also be the sole server that verifies them.&lt;/p&gt;

&lt;p&gt;First, define a struct that will be used to represent claims or identity for users data :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Joshjp9t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AFLbx_OSz6KTJ0DKt305zRg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Joshjp9t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AFLbx_OSz6KTJ0DKt305zRg.png" alt="" width="489" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;jwt.StandardClaims&lt;/em&gt; struct contains useful fields like &lt;em&gt;expiry, issuer name, Subject and etc&lt;/em&gt;. Now we will create some actual claims for the user that just logged in :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5v-ZXxGx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AzdzPyQXcCHoauLjoR5YgAQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5v-ZXxGx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AzdzPyQXcCHoauLjoR5YgAQ.png" alt="" width="495" height="261"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, Create an unsigned token from the claims :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Sign the token using a secure private key. Make sure you use a real private key, preferably at least 256 bits in length:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;signedToken, err := token.SignedString([]byte(“secureSecretText”))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now the signed token can be sent back to the client.&lt;/p&gt;

&lt;h2&gt;
  
  
  Validating a JWT
&lt;/h2&gt;

&lt;p&gt;When a client requests a secured endpoint, we may use the following steps to validate the JWT.&lt;/p&gt;

&lt;p&gt;Note: To use the Authorization HTTP header is idiomatic:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Authorization: Bearer {jwt}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After obtaining the JWT, use the same private key to validate the claims and verify the signature.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;token, err := jwt.ParseWithClaims(
    jwtFromHeader,
    &amp;amp;customClaims{},
    func(token *jwt.Token) (interface{}, error) {
        return []byte("secureSecretText"), nil 
    },
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The err variable will not be null if the claims have been tampered with. Parse the following claims from the token:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;claims, ok := token.Claims.(*customClaims)
if !ok {
    return errors.New("Couldn't parse claims")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Check if the token is expired:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if claims.ExpiresAt &amp;lt; time.Now().UTC().Unix() {
    return errors.New("JWT is expired")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You now know the username of the authenticated user!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;username := claims.Username
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For full examples take a look at this &lt;a href="https://pkg.go.dev/github.com/golang-jwt/jwt#example-Parse-Hmac"&gt;example&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks For Reading!
&lt;/h2&gt;

&lt;p&gt;Available for a new project! Let’s have a talk &lt;a href="//mailto:bangadam.dev@gmail.com"&gt;bangadam.dev@gmail.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>auth</category>
      <category>jwt</category>
    </item>
    <item>
      <title>Writing Unit Test in Golang Easily</title>
      <dc:creator>Muhammad Adam</dc:creator>
      <pubDate>Thu, 04 Aug 2022 00:58:00 +0000</pubDate>
      <link>https://dev.to/bangadam/writing-unit-test-in-golang-easily-lgm</link>
      <guid>https://dev.to/bangadam/writing-unit-test-in-golang-easily-lgm</guid>
      <description>&lt;h2&gt;
  
  
  Writing Unit Test in Golang Easily
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wGh0bYQL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2400/0%2A_WcCH-_9q9Jy92xy.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wGh0bYQL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2400/0%2A_WcCH-_9q9Jy92xy.jpeg" alt="" width="880" height="615"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unit testing is the most important thing in developing an application, which aims to minimize the occurrence of errors, therefore software developers are obliged to master it. In this article, we will write unit tests in the Golang programming language using the unit testing package provided by Golang.&lt;/p&gt;

&lt;p&gt;Let’s get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Unit Tests?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In &lt;a href="https://en.wikipedia.org/wiki/Computer_programming"&gt;computer programming&lt;/a&gt;, **unit testing **is a &lt;a href="https://en.wikipedia.org/wiki/Software_testing"&gt;software testing&lt;/a&gt; method by which individual units of &lt;a href="https://en.wikipedia.org/wiki/Source_code"&gt;source code&lt;/a&gt; — sets of one or more computer program &lt;a href="https://en.wikipedia.org/wiki/Modular_programming"&gt;modules&lt;/a&gt; together with associated control data, usage &lt;a href="https://en.wikipedia.org/wiki/Procedure_(computer_science)"&gt;procedures&lt;/a&gt;, and operating procedures — are tested to determine whether they are fit for use&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Developers use unit testing to validate individual functions, methods, modules, and packages. Unit testing aids in the discovery and elimination of issues early in the development cycle, as well as the prevention of regressions during refactoring. A decent unit test may also act as documentation for new developers on the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Goals
&lt;/h2&gt;

&lt;p&gt;In this article, it is hoped that readers will get an insight into how to do basic testing on the Golang programming language using the standard package, namely “&lt;strong&gt;testing&lt;/strong&gt;”.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: project structure
&lt;/h2&gt;

&lt;p&gt;In a project belonging to someone else, surely you have encountered a file that has the prefix &lt;strong&gt;_test.go&lt;/strong&gt;, right? and always in the same directory as the main file to be tested. At this time we will first create the project structure as below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/
 - main.go
 - main_test.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;don’t forget to run go mod init to initialize the project on Golang.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go mod init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 2: make simple func
&lt;/h2&gt;

&lt;p&gt;We will create a simple function that we will test later, the name of the function is &lt;strong&gt;Calculate()&lt;/strong&gt;. This &lt;strong&gt;Calculate()&lt;/strong&gt; function has the aim of adding a number and adding 2, it’s quite easy isn’t it, let’s make the code as below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Create a test file
&lt;/h2&gt;

&lt;p&gt;after we create the &lt;strong&gt;Calculate()&lt;/strong&gt; function, the next step is that we will create a test file for the function. Create a new file called &lt;strong&gt;main_test.go&lt;/strong&gt; and put it in the same directory, follow the code below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Step 4: Running Tests
&lt;/h2&gt;

&lt;p&gt;After we have created the test file, it’s time for us to try to run the testing command by calling the go test command on the terminal or CMD, as below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;If the process is successful then the display of the test process will be as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DL2qEinq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ADtjUV5cXq44-mIfutASvRA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DL2qEinq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ADtjUV5cXq44-mIfutASvRA.png" alt="testing pass" width="600" height="45"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;easy isn’t it? This is more or less how to make a test in the Golang programming language.&lt;/p&gt;
&lt;h2&gt;
  
  
  Table Driven Testing
&lt;/h2&gt;

&lt;p&gt;After we succeeded in creating a test file in the previous &lt;strong&gt;Calculate()&lt;/strong&gt; function, there is actually another way to make the test code better and dynamic, namely by creating an array model. You can see an example of this code below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
With the above code, we can declare the input and expected values ​​for each test case in the data array above. After you add it, try running the test one more time:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1BE3W-OO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ARJ6T5mChUOxo9UhWZ0dGBA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1BE3W-OO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ARJ6T5mChUOxo9UhWZ0dGBA.png" alt="testing pass" width="613" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;in this article, it can be concluded that writing unit tests is very important for the development process so that it can help carry out test code that has been made according to expectations or not, in the next stage maybe I will discuss code coverage on unit tests that we have made previously. If you need the source code above, just check my GitHub here &lt;a href="https://github.com/bangadam/go-test-introduction"&gt;https://github.com/bangadam/go-test-introduction&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks For Reading!
&lt;/h2&gt;

&lt;p&gt;Available for a new project! Let’s have a talk :&lt;br&gt;
Email: &lt;a href="//mailto:bangadam.dev@gmail.com"&gt;bangadam.dev@gmail.com&lt;/a&gt;&lt;br&gt;
Linkedin: &lt;a href="https://www.linkedin.com/in/bangadam/"&gt;https://www.linkedin.com/in/bangadam&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>unittest</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
