<?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: Emirhakan Tanhan</title>
    <description>The latest articles on DEV Community by Emirhakan Tanhan (@emirhakantanhan).</description>
    <link>https://dev.to/emirhakantanhan</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%2F639385%2F3b2955ba-e387-4c75-bed5-83dea1e81c9e.jpeg</url>
      <title>DEV Community: Emirhakan Tanhan</title>
      <link>https://dev.to/emirhakantanhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emirhakantanhan"/>
    <language>en</language>
    <item>
      <title>Figuring out a verification system via email link</title>
      <dc:creator>Emirhakan Tanhan</dc:creator>
      <pubDate>Sun, 30 May 2021 21:57:02 +0000</pubDate>
      <link>https://dev.to/emirhakantanhan/figuring-out-a-verification-system-via-email-link-59p1</link>
      <guid>https://dev.to/emirhakantanhan/figuring-out-a-verification-system-via-email-link-59p1</guid>
      <description>&lt;p&gt;Hi everyone,&lt;/p&gt;

&lt;p&gt;Today i'm doing a verification system for the important changes in our site, like changing your email or resetting your password. For this article lets simulate an email change.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why send email?
&lt;/h4&gt;

&lt;p&gt;Obviously, we need to send an email to the new email address, which will indicate that the new email address is &lt;strong&gt;in use&lt;/strong&gt; and &lt;strong&gt;reachable&lt;/strong&gt;, for the cases like if the user forgets his/her password. I wont be showing how i send emails to users but if you're curious, i'm using PHPMailer.&lt;/p&gt;

&lt;h4&gt;
  
  
  Keep the changes until confirmed
&lt;/h4&gt;

&lt;p&gt;User entered his/her new email address, but the changes won't be applying until the user goes to the verification link, which we sent to the new email address. So, when the user goes into the link, and it's time to apply the changes, how do we know what changes the user made? For that, we're going to store the changes in our database. &lt;/p&gt;

&lt;h4&gt;
  
  
  Database table for verification
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CpqDJ_Gk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfbddt3j5k01equovcqy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CpqDJ_Gk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfbddt3j5k01equovcqy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZXDeoxfu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3kjrc1dkrspd43a1nn7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZXDeoxfu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3kjrc1dkrspd43a1nn7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When a user wants to make a change that requires email verification, a row is created in &lt;em&gt;verification&lt;/em&gt; table. This table contains:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;status&lt;/code&gt;: Helps us to track if this verification link is used and the changes applied.&lt;br&gt;
 &lt;code&gt;email&lt;/code&gt;: Email address to send the verification link.&lt;br&gt;
 &lt;code&gt;title&lt;/code&gt;: Title of the email.&lt;br&gt;
 &lt;code&gt;description&lt;/code&gt;: Context of the email.&lt;br&gt;
 &lt;code&gt;link&lt;/code&gt;: Url with a key that directs to our site.&lt;br&gt;
 &lt;code&gt;changes&lt;/code&gt;: Changes that we will be applying once the user goes to the link that we have sent.&lt;br&gt;
 &lt;code&gt;user/admin id&lt;/code&gt;: Refers to the user/admin and lets us know whose changes we are applying.&lt;/p&gt;

&lt;p&gt;I will be talking in detail about 2 columns in this row: &lt;code&gt;link&lt;/code&gt; and &lt;code&gt;changes&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Link&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Link is a url that we will be using for both direction and finding the row related to that link in the &lt;em&gt;verification&lt;/em&gt; table.&lt;/p&gt;

&lt;p&gt;Example of a &lt;code&gt;link&lt;/code&gt;:&lt;br&gt;
&lt;code&gt;http://satis.emirhakan.com/panel/verification/ADOI8U7oCXOJoJJxBDMF3e&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's examine the &lt;code&gt;key&lt;/code&gt; part of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;| Ver. Type |  Random Gen. String | Hex. Id |
|-----------|---------------------|---------|
|    "A"    |"DOI8U7oCXOJoJJxBDMF"|   "3e"  |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Verification Type&lt;/code&gt;: First character of a &lt;code&gt;key&lt;/code&gt;. Lets us know what type of a verification this is, for example: "A" is for confirming email address, and "B" is for resetting password. This allows us to generate different styled pages on different verification types.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Random Generated String&lt;/code&gt;: Next 19 characters of a &lt;code&gt;key&lt;/code&gt;. This random string allows us to compare the &lt;strong&gt;url&lt;/strong&gt; in the address bar  with the &lt;strong&gt;url&lt;/strong&gt; in the &lt;code&gt;link&lt;/code&gt; column of a &lt;em&gt;verification&lt;/em&gt; row. In short this part distinguishes a &lt;code&gt;link&lt;/code&gt; from others.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Hexadecimal Id&lt;/code&gt;: Last couple characters after the 20th character of a &lt;code&gt;key&lt;/code&gt;. It's the id of the &lt;em&gt;verification&lt;/em&gt; row but transformed to hexadecimal. In this case "3e" in hexadecimal base is equal to  "62" in decimal base. We check the related &lt;em&gt;Verification&lt;/em&gt; row based on this id number.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Changes&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Changes are the pending changes due to their importance. They will be applied once the user goes to the link related to those changes. After they are applied, &lt;code&gt;status&lt;/code&gt; of that &lt;em&gt;verification&lt;/em&gt; row will be set to 1 to make it unusable, preventing any conflict and duplication in the database.&lt;/p&gt;

&lt;p&gt;Example of a &lt;code&gt;changes&lt;/code&gt; column:&lt;br&gt;
&lt;code&gt;{"Admin":{"email":"me@emirhakan.com"}}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's examine parts of a &lt;code&gt;changes&lt;/code&gt; column:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             |        Change Object         |
             |------------------------------|
| Table Name | Col. Name |       Value      |
|------------|-----------|------------------|
|   "Admin"  |  "email"  |"me@emirhakan.com"|
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Table Name&lt;/code&gt;: The table where changes will be made. Changes can be made in 1 table at a time.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Change Object&lt;/code&gt;: Overall, the object that contains the change with its key. There can be multiple objects in a &lt;code&gt;changes&lt;/code&gt; column. e.g.&lt;br&gt;
&lt;code&gt;{"User_credit":{"credit_num":"4111111111111111"},{"exp_date":"02/22"}}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Column Name&lt;/code&gt;: The column that is going to be updated.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Value&lt;/code&gt;: The value that will be put in the column.&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementing this idea to our site
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Creating and sending verification link via email
&lt;/h5&gt;

&lt;p&gt;A &lt;em&gt;Verification&lt;/em&gt; row is created when an important change is submitted.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sV4vC2Be--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/evjt9rf3qbkl6u51cg5n.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sV4vC2Be--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/evjt9rf3qbkl6u51cg5n.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our case, we're only changing our email. A submitted form goes through different processes, but eventually it will come to the &lt;strong&gt;api&lt;/strong&gt;, where server-side operations are made.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PMnWAq8g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g61fu2qizy2ms0u0qbw3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PMnWAq8g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g61fu2qizy2ms0u0qbw3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;em&gt;Update admin email&lt;/em&gt; section, we're preparing all the necessary variables such as &lt;code&gt;link&lt;/code&gt; and &lt;code&gt;changes&lt;/code&gt;. &lt;br&gt;
Below that, in the &lt;em&gt;Create verification data&lt;/em&gt; section we're inserting &lt;em&gt;Verification&lt;/em&gt; row in our database.&lt;/p&gt;

&lt;p&gt;Finally, in the &lt;em&gt;Update link&lt;/em&gt; section, we're adding the hexadecimal id of the &lt;em&gt;Verification&lt;/em&gt; row in the end of the &lt;code&gt;link&lt;/code&gt;. Then, as you can see, we're running the &lt;code&gt;SendMail&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;At this point, we have created a &lt;em&gt;Verification&lt;/em&gt; row with the &lt;code&gt;link&lt;/code&gt; and the &lt;code&gt;changes&lt;/code&gt; inside, then we have sent an email to the user with the same &lt;code&gt;link&lt;/code&gt; attached.&lt;/p&gt;

&lt;h5&gt;
  
  
  Using the email link and confirming our changes
&lt;/h5&gt;

&lt;p&gt;Lets use our email link.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UO3WXKXj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/poz82i1o13jg660l9izi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UO3WXKXj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/poz82i1o13jg660l9izi.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When a url with a &lt;strong&gt;verification&lt;/strong&gt; link is entered in the address bar, it will go to the &lt;strong&gt;api&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QicJtp_D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ry6zn230spsvy8men57.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QicJtp_D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ry6zn230spsvy8men57.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right below the &lt;code&gt;case 'vercheck'&lt;/code&gt;, we are splitting the &lt;code&gt;link&lt;/code&gt; and preparing variables. In the first 2 cases of the &lt;code&gt;switch(true)&lt;/code&gt;, we're checking the &lt;code&gt;link&lt;/code&gt; and comparing it to it's match in the database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TqSup4Nx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cr82ilc3vnv3v1eblpkb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TqSup4Nx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cr82ilc3vnv3v1eblpkb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;default:&lt;/code&gt;, now we know the &lt;code&gt;link&lt;/code&gt; is correct and it belongs to a user and we are ready to apply the changes. We fetch the &lt;code&gt;changes&lt;/code&gt; column to disassemble it at the &lt;code&gt;foreach&lt;/code&gt; block. Through this process we also apply the changes.&lt;/p&gt;

&lt;p&gt;After finishing applying the changes, we are ready to update the related &lt;em&gt;Verification&lt;/em&gt; row and change its &lt;code&gt;status&lt;/code&gt; to 1.&lt;/p&gt;

&lt;p&gt;At the end, we return the status and finally, we have completed the verification process.&lt;/p&gt;

&lt;h4&gt;
  
  
  We have accomplished a verification system that works via email links
&lt;/h4&gt;

&lt;p&gt;I'm a beginner in development, my logic and code might not be so effective and correct, but i wanted to share my thoughts and process of developing a verification system. &lt;/p&gt;

&lt;p&gt;Thank you all for reading.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>php</category>
      <category>mysql</category>
    </item>
  </channel>
</rss>
