<?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>Forem: Maulana</title>
    <description>The latest articles on Forem by Maulana (@yth_maulana).</description>
    <link>https://forem.com/yth_maulana</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%2F566085%2F65da429e-00ea-4d83-8f11-1f8156fd0555.png</url>
      <title>Forem: Maulana</title>
      <link>https://forem.com/yth_maulana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/yth_maulana"/>
    <language>en</language>
    <item>
      <title>Encrypted JWT and how to activate Auth::user()</title>
      <dc:creator>Maulana</dc:creator>
      <pubDate>Sun, 19 Nov 2023 14:19:57 +0000</pubDate>
      <link>https://forem.com/yth_maulana/encrypted-jwt-and-how-to-activate-authuser-1eai</link>
      <guid>https://forem.com/yth_maulana/encrypted-jwt-and-how-to-activate-authuser-1eai</guid>
      <description>&lt;p&gt;I just had an experience that I found interesting&lt;/p&gt;

&lt;p&gt;So the boss asked me to encrypt the JWT token sent to the frontend. However, the problem is that when the token is encrypted, the controller cannot use &lt;code&gt;Auth()-&amp;gt;user()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What I did was create middleware to decrypt the token sent from the frontend then set to user so that &lt;code&gt;Auth()-&amp;gt;user()&lt;/code&gt; can be used in all controllers in the middleware&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;str_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Bearer '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Authorization'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nv"&gt;$token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Crypt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;JWTAuth&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;setToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$token&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;JWTAuth&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;toUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maybe there are more suggestions for friends who are more experienced. Thanks 🌟&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
