<?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: Niyaz</title>
    <description>The latest articles on DEV Community by Niyaz (@jniyaz).</description>
    <link>https://dev.to/jniyaz</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%2F58969%2Fe7b124a1-cf05-48f0-85b7-2a196ad9ac33.jpg</url>
      <title>DEV Community: Niyaz</title>
      <link>https://dev.to/jniyaz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jniyaz"/>
    <language>en</language>
    <item>
      <title>Alpine.js, exploring in real world applications</title>
      <dc:creator>Niyaz</dc:creator>
      <pubDate>Wed, 25 Aug 2021 10:21:36 +0000</pubDate>
      <link>https://dev.to/jniyaz/alpine-js-exploring-in-real-world-applications-cgo</link>
      <guid>https://dev.to/jniyaz/alpine-js-exploring-in-real-world-applications-cgo</guid>
      <description>&lt;p&gt;Alpine is a minimal javascript library for composing behavior directly in HTML markup. &lt;/p&gt;

&lt;p&gt;Alpine was known as “project-x”, a nod to it’s past.&lt;br&gt;
Creator Caleb Porzio (Creator of Alpine.js, Laravel Livewire) has kept much of the syntax is like Vue.js.&lt;/p&gt;

&lt;p&gt;Okay, Lets start..&lt;/p&gt;
&lt;h3&gt;
  
  
  To use:
&lt;/h3&gt;
&lt;h5&gt;
  
  
  From a script tag into head tag:
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h5&gt;
  
  
  As a module:
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`npm install alpinejs` or `yarn alpinejs`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Alpine from 'alpinejs'
window.Alpine = Alpine
Alpine.start()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Alpine has collection of 15 attributes, 6 properties, and 2 methods.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;x-data, x-bind, x-on, x-text, x-if, x-for, x-ref, x-html...&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Please check the docs here, &lt;a href="https://alpinejs.dev" rel="noopener noreferrer"&gt;https://alpinejs.dev&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Creating simple component (x-data);
&lt;/h5&gt;

&lt;p&gt;All we need to do is add the x-data attribute to any element like below, Thats all.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div x-data="{ title: 'Hello Alpine..' }"&amp;gt;
&amp;lt;p x-text="title"&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;button @click="title='Alpine rocks'"&amp;gt;Change Title&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  On Modular approach:
&lt;/h5&gt;

&lt;h6&gt;
  
  
  On main.js
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Alpine from 'alpinejs';
window.Alpine = Alpine

import pageSidebar from './components/PageSidebar.js';
window.sidebarApp = pageSidebar;

Alpine.start()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  On components/PageSidebar.js
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default () =&amp;gt; {
  return { 
      data: null,
      init() {
          console.log('sidebar component');
      },
      // methods
      // ......
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  on Html Markup:
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div x-data="sidebarApp()"&amp;gt;
......
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;explore more details here : &lt;a href="https://alpinejs.dev/start-here" rel="noopener noreferrer"&gt;https://alpinejs.dev/start-here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thats All. Hope you get idea. Thanks for reading.&lt;/p&gt;

</description>
      <category>alpinejs</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How do I manage States in React Apps?</title>
      <dc:creator>Niyaz</dc:creator>
      <pubDate>Wed, 11 Aug 2021 11:30:11 +0000</pubDate>
      <link>https://dev.to/jniyaz/how-do-i-manage-states-in-react-apps-10d</link>
      <guid>https://dev.to/jniyaz/how-do-i-manage-states-in-react-apps-10d</guid>
      <description>&lt;p&gt;In this article, Let's go through, how do i manage local, global states When building react applications.&lt;/p&gt;

&lt;p&gt;I try to follow up with, keep the states as low as possible, &lt;/p&gt;

&lt;p&gt;App Level State:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Local State &lt;/li&gt;
&lt;li&gt;Lift State&lt;/li&gt;
&lt;li&gt;Global State&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Local State:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
    const [theme, setTheme] = useState("light");

    const pickTheme = theme === 'light' ? 'dark' : 'light';

    const toggleTheme = () =&amp;gt; {
        setTheme(pickTheme)
    }

    return (
        &amp;lt;div&amp;gt;
            &amp;lt;h4&amp;gt;Theme: {theme}&amp;lt;/h4&amp;gt;
            &amp;lt;button onClick={toggleTheme}&amp;gt;Change&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Lift State:
&lt;/h2&gt;

&lt;p&gt;when components grows like 2, 3 levels deeper, it is fine.. &lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
    const [theme, setTheme] = useState("light");
    const pickTheme = theme === 'light' ? 'dark' : 'light';
    const toggleTheme = () =&amp;gt; {
        setTheme(pickTheme)
    }

    return &amp;lt;DirectChild theme={theme} toggleTheme={toggleTheme}  /&amp;gt;
}

function DirectChild({ theme, toggleTheme }) {
    return &amp;lt;div&amp;gt;
        &amp;lt;div&amp;gt;Hello Child&amp;lt;/div&amp;gt;
        &amp;lt;DeeperChild theme={theme} toggleTheme={toggleTheme} /&amp;gt;
    &amp;lt;/div&amp;gt;
}

function DeeperChild({ theme, toggleTheme }) {
    return &amp;lt;div&amp;gt;
        &amp;lt;h2&amp;gt;Hello Child&amp;lt;/h2&amp;gt;
        &amp;lt;h4&amp;gt;Theme: {theme}&amp;lt;/h4&amp;gt;
        &amp;lt;button onClick={toggleTheme}&amp;gt;Change Theme&amp;lt;/button&amp;gt;
    &amp;lt;div&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But when components nested with N level deep, will lead to prop drilling, which causes performance issues, in those cases, we should think Global State Management..&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Global State:
&lt;/h2&gt;

&lt;p&gt;When dealing with Global States, I try to follow up with React Context API hooks, Let us go through, how to integrate with an example.&lt;/p&gt;

&lt;p&gt;But When deal with Global State Management, can use any 3rd party libraries most comfortable with like Redux, Mobx, Recoil etc.. or just stick with useState hook..&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
const MyContext = React.createContext({});
const MyProvider = ({ children }) =&amp;gt; {
    const [theme, setTheme] = React.useState('light');
    const nextTheme = theme === 'light' ? 'dark' : 'light';
    const value = {
        theme,
        nextTheme,
        toggleTheme: () =&amp;gt; {
            setTheme(nextTheme);
        }
    }

    return &amp;lt;MyContext.Provider value={value}&amp;gt;{children}&amp;lt;/MyContext.Provider&amp;gt;
}

function App() {
    return &amp;lt;MyProvider&amp;gt;
        &amp;lt;DirectChild /&amp;gt;
    &amp;lt;/MyProvider&amp;gt;
}

const DirectChild = () =&amp;gt; {
    return &amp;lt;DeeperChild /&amp;gt;
};

const DeeperChild = () =&amp;gt; {
    const { nextTheme, toggleTheme } = React.useContext(MyContext);

    return &amp;lt;div&amp;gt;
            &amp;lt;p&amp;gt;Theme : {nextTheme}&amp;lt;/p&amp;gt;
            &amp;lt;button onClick={toggleTheme}&amp;gt;Change Theme&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  External Data Fetching:
&lt;/h2&gt;

&lt;p&gt;Often I try to use with Next.js's &lt;strong&gt;SWR&lt;/strong&gt; or &lt;strong&gt;React Query&lt;/strong&gt; libraries, which are powerful with many features.&lt;br&gt;
on GraphQl fetch, can use &lt;strong&gt;Apollo Client&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Hoping this article have given some idea on managing states with React applications.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to configure JWT in Laravel Applications</title>
      <dc:creator>Niyaz</dc:creator>
      <pubDate>Thu, 24 Jun 2021 13:12:40 +0000</pubDate>
      <link>https://dev.to/jniyaz/how-to-configure-jwt-in-laravel-applications-2n1e</link>
      <guid>https://dev.to/jniyaz/how-to-configure-jwt-in-laravel-applications-2n1e</guid>
      <description>&lt;p&gt;JWT installation &amp;amp; Settings&lt;/p&gt;

&lt;p&gt;Before we get into this JSON Web Token tutorial, what exactly is a JWT?&lt;br&gt;
In modern web trends, Popularity of single page applications, mobile applications, and RESTful API services, We are no longer spending much time building markup, instead we are building APIs that our front-end applications consume.&lt;br&gt;
Back-end is more about business logic and data, while presentation logic is moved exclusively to the front-end or mobile applications. These changes have led to new ways of implementing authentication in modern applications.&lt;/p&gt;

&lt;p&gt;What is a JSON Web Token?&lt;/p&gt;

&lt;p&gt;A JSON Web Token is used to send information that can be verified and trusted by means of a digital signature. It comprises a compact and URL-safe JSON object, which is cryptographically signed to verify its authenticity, and which can also be encrypted if the payload contains sensitive information.&lt;br&gt;
Because of its compact structure, JWT is usually used in HTTP Authorization headers or URL query parameters.&lt;/p&gt;

&lt;p&gt;How to use JWT authentication approach in Laravel applications?&lt;/p&gt;

&lt;p&gt;JWT (Json Web token) to make token based authentication system in Laravel. if we implement everything of jwt authentication system, it’s hard and takes long time. so we decided to use “tymon/jwt-auth” package to integarte quickly to develop jwt authentication system.&lt;/p&gt;

&lt;p&gt;tymon/jwt-auth: &lt;a href="https://github.com/tymondesigns/jwt-auth" rel="noopener noreferrer"&gt;https://github.com/tymondesigns/jwt-auth&lt;/a&gt;&lt;br&gt;
tymon/jwt-auth document: &lt;a href="https://jwt-auth.readthedocs.io/en/develop/" rel="noopener noreferrer"&gt;https://jwt-auth.readthedocs.io/en/develop/&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require tymon/jwt-auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://jwt-auth.readthedocs.io/en/develop/laravel-installation/" rel="noopener noreferrer"&gt;https://jwt-auth.readthedocs.io/en/develop/laravel-installation/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Publish the config&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You should now have a config/jwt.php file that allows you to configure the basics of this package.&lt;/p&gt;

&lt;p&gt;Generate secret key&lt;/p&gt;

&lt;p&gt;I have included a helper command to generate a key for you:&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;Update your User model&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
  namespace App;

  use Tymon\JWTAuth\Contracts\JWTSubject;
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Foundation\Auth\User as Authenticatable;

  class User extends Authenticatable implements JWTSubject
  {
  use Notifiable;

  // Rest omitted for brevity

  /**
    * Get the identifier that will be stored in the subject claim of the JWT.
    *
    * @return mixed
    */
  public function getJWTIdentifier()
  {
      return $this-&amp;gt;getKey();
  }

  /**
    * Return a key value array, containing any custom claims to be added to the JWT.
    *
    * @return array
    */
  public function getJWTCustomClaims()
  {
      return [];
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Configure Auth guard&lt;br&gt;
inside the config/auth.php file, update the following details&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  'defaults' =&amp;gt; [
    'guard' =&amp;gt; 'api',
    'passwords' =&amp;gt; 'users',
  ],
  ......
  'guards' =&amp;gt; [
    'api' =&amp;gt; [
      'driver' =&amp;gt; 'jwt',
      'provider' =&amp;gt; 'users',
    ],
  ],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add authentication routes&lt;br&gt;
First let’s add some routes in routes/api.php as follows:&lt;br&gt;
Jwt Authentication:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::post('register', [JwtAuthController::class, 'register'])-&amp;gt;name('api.jwt.register');
Route::post('login', [JwtAuthController::class, 'login'])-&amp;gt;name('api.jwt.login');
Route::group(['middleware' =&amp;gt; 'auth:api'], function(){
  Route::get('user', [JwtAuthController::class, 'me'])-&amp;gt;name('api.jwt.user');
  Route::get('refresh', [JwtAuthController::class, 'refresh'])-&amp;gt;name('api.jwt.refresh');
  Route::get('logout', [JwtAuthController::class, 'logout'])-&amp;gt;name('api.jwt.logout');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create the JwtAuthController:&lt;br&gt;
    php artisan make:controller JwtAuthController&lt;/p&gt;

&lt;p&gt;Then add the following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
namespace App\Http\Controllers\Auth;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class JwtAuthController extends Controller
{
    /**
    * Create a new AuthController instance.
    *
    * @return void
    */
    public function __construct()
    {
        $this-&amp;gt;middleware('auth:api', ['except' =&amp;gt; ['login', 'register']]);
    }

    public function register(Request $request)
    {
        $validated = $request-&amp;gt;validate([
            'name' =&amp;gt; 'required|string|max:100',
            'email' =&amp;gt; 'required|email|max:255|unique:users',
            'password' =&amp;gt; 'required|string|min:8|max:255|confirmed',
            'password_confirmation' =&amp;gt; 'required|string|min:8|max:255',
        ]);

        $user = new User;
        $user-&amp;gt;fill($request-&amp;gt;all());
        $user-&amp;gt;password = bcrypt($request-&amp;gt;password);
        $user-&amp;gt;save();

        return response()-&amp;gt;json([
            'status' =&amp;gt; 'success',
            'data' =&amp;gt; $user
        ], 200);
    }

    /**
    * Get a JWT via given credentials.
    *
    * @return \Illuminate\Http\JsonResponse
    */
    public function login()
    {
        $credentials = request(['email', 'password']);

        if (!$token = Auth::attempt($credentials)) {
            return response()-&amp;gt;json(['error' =&amp;gt; 'Unauthorized'], 401);
        }

        return $this-&amp;gt;respondWithToken($token);
    }

    /**
    * Get the authenticated User.
    *
    * @return \Illuminate\Http\JsonResponse
    */
    public function me()
    {
        return response()-&amp;gt;json(Auth::user());
    }

    /**
    * Log the user out (Invalidate the token).
    *
    * @return \Illuminate\Http\JsonResponse
    */
    public function logout()
    {
        Auth::logout();

        return response()-&amp;gt;json(['message' =&amp;gt; 'Successfully logged out']);
    }

    /**
    * Refresh a token.
    *
    * @return \Illuminate\Http\JsonResponse
    */
    public function refresh()
    {
        return $this-&amp;gt;respondWithToken(Auth::refresh());
    }

    /**
    * Get the token array structure.
    *
    * @param  string $token
    *
    * @return \Illuminate\Http\JsonResponse
    */
    protected function respondWithToken($token)
    {
        return response()-&amp;gt;json([
            'access_token' =&amp;gt; $token,
            'token_type' =&amp;gt; 'bearer',
            'expires_in' =&amp;gt; Auth::factory()-&amp;gt;getTTL() * 60
        ]);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You should now be able to POST to the login endpoint (e.g. &lt;a href="http://localhost:8000/api/login" rel="noopener noreferrer"&gt;http://localhost:8000/api/login&lt;/a&gt;) with some valid credentials and see a response like: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ",
    "token_type": "bearer",
    "expires_in": 3600
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This token can then be used to make authenticated requests to your application.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>jwt</category>
    </item>
  </channel>
</rss>
