<?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: shaileshjadav</title>
    <description>The latest articles on DEV Community by shaileshjadav (@shaileshjadav).</description>
    <link>https://dev.to/shaileshjadav</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%2F702433%2F8efa81f3-6bb9-42fc-936f-3cf3467c8baf.png</url>
      <title>DEV Community: shaileshjadav</title>
      <link>https://dev.to/shaileshjadav</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shaileshjadav"/>
    <language>en</language>
    <item>
      <title>Laravel 8 REST API Authentication using Sanctum </title>
      <dc:creator>shaileshjadav</dc:creator>
      <pubDate>Wed, 15 Sep 2021 12:02:24 +0000</pubDate>
      <link>https://dev.to/shaileshjadav/laravel-8-rest-api-authentication-using-sanctum-3eb8</link>
      <guid>https://dev.to/shaileshjadav/laravel-8-rest-api-authentication-using-sanctum-3eb8</guid>
      <description>&lt;p&gt;Sanctum is Laravel package for authentication for single page application(SPAs), mobile applications and basic token based APIs. &lt;/p&gt;

&lt;p&gt;For SPA authentication, Sanctum uses Laravel’s built in cookie based authentication services. Means while working with front end technologies like react, Angular Sanctum create cookie and save in browser by using this accomplish authentication. For token based Rest APIs, Sanctum create token and save in &lt;strong&gt;personal_access_tokens&lt;/strong&gt; table. while authentication try to match with this table if token not found then authentication goes to failed.&lt;/p&gt;

&lt;p&gt;Let’s install sanctum package first and then create a register, login, logout APIs and protect our routes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require laravel/sanctum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command install package using composer.&lt;br&gt;
&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="Laravel\Sanctum\SanctumServiceProvider"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next run this command, it creates migration files for personal_access_token table and configuration files for sanctum. You can check this file in database-&amp;gt;migrations folder named like 2019_12_14_000001_create_personal_access_tokens_table.php&lt;br&gt;
&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;p&gt;Then run this command, this create a personal_access_token table in database in which Sanctum store API tokens.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuration:
&lt;/h3&gt;

&lt;p&gt;Go to app-&amp;gt;Http-&amp;gt;Kernal.php and add given lines to apis array of middlewareGroups.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'api' =&amp;gt; [
    \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
    'throttle:api',
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
],

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: If you get too many attempts error while calling APIs multiple time. Then comment ‘throttle:api’ line and keep default as it is like below screenshot.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F60if2tikz9fkmwa32bwe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F60if2tikz9fkmwa32bwe.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next go to UserModel.php file and add set below code so usermodel can use HasApiTokens to issue token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Laravel\Sanctum\HasApiTokens;
use HasApiTokens, HasFactory, Notifiable;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note in my project hasFactory trait not autoloaded.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5v1siiu69oikcu4vqisq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5v1siiu69oikcu4vqisq.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Create Crontroller
&lt;/h4&gt;



&lt;p&gt;&lt;code&gt;php artisan make:controller Api\AuthController&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This command create authController in Api folder of controller’s folder. &lt;/p&gt;

&lt;p&gt;Set routes in routes-&amp;gt;api.php file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use App\Http\Controllers\Api\AuthController;
Route::post("/register",[AuthController::class,'register']);
Route::post("/login",[AuthController::class,'login']);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s make register function in authController. in this new user and token creates and given to response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function register(Request $request){
        $fields = $request-&amp;gt;validate([
            'name' =&amp;gt;'required|string',
            'email'=&amp;gt;'required|string|email|unique:users,email',
            'password' =&amp;gt;'required|confirmed'
        ]);

        $user = User::create([
            'name'=&amp;gt;$fields['name'],
            'email'=&amp;gt;$fields['email'],
            'password'=&amp;gt;Hash::make($fields['password']),
        ]);

        //create token
        $token = $user-&amp;gt;createToken('myapptoken')-&amp;gt;plainTextToken;

        $response = [
            'status'=&amp;gt;true,
            'message'=&amp;gt;'registered successfully!',
            'data' =&amp;gt;[
                'user'=&amp;gt;$user,
                'token'=&amp;gt;$token
            ]
        ];
        return response($response,201);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And call api using postman as below:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6whfbewcpsxk2g7jcl5u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6whfbewcpsxk2g7jcl5u.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Login Function:
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function login(Request $request){
        $fields = $request-&amp;gt;validate([
            'email'=&amp;gt;'required|string|email',
            'password' =&amp;gt;'required|confirmed'
        ]);
        //check email
        $user = User::where('email',$fields['email'])-&amp;gt;first();
        //check password
        if(!$user || !Hash::check($fields['password'],$user-&amp;gt;password)){
            return response(['status'=&amp;gt;false,'message'=&amp;gt;'invalid email or password'],401);
        }

        //create token
        $token = $user-&amp;gt;createToken('myapptoken')-&amp;gt;plainTextToken;

        $response = [
            'status'=&amp;gt;true,
            'message'=&amp;gt;'Login successful!',
            'data' =&amp;gt;[
                'user'=&amp;gt;$user,
                'token'=&amp;gt;$token
            ]
        ];
        return response($response,201);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Call login api as below:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flx65stdnl037lc11jpom.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flx65stdnl037lc11jpom.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Let’s create authenticate routes in api-routes file as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::group(['middleware'=&amp;gt;['auth:sanctum']],function(){
    Route::post("/logout",[AuthController::class,'logout']);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above routes sanctum middleware validates token from Authorization header.&lt;/p&gt;

&lt;p&gt;Let’s make function logout in authController.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function logout(Request $request){
        auth()-&amp;gt;user()-&amp;gt;tokens()-&amp;gt;delete();
        $response = [
            'status'=&amp;gt;true,
            'message'=&amp;gt;'Logout successfully',
        ];
        return response($response,201);
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4lg03njzbyhwzfsc0pi3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4lg03njzbyhwzfsc0pi3.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While token is not set/validate then will receive below response.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
    "message": "Unauthenticated."&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Note: Set header Accept:application/json to get response in json for above apis and Authorization header with Bearer .&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>authentication</category>
      <category>sanctum</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>PHP vs Nodejs</title>
      <dc:creator>shaileshjadav</dc:creator>
      <pubDate>Fri, 10 Sep 2021 12:38:50 +0000</pubDate>
      <link>https://dev.to/shaileshjadav/php-vs-nodejs-imi</link>
      <guid>https://dev.to/shaileshjadav/php-vs-nodejs-imi</guid>
      <description>&lt;p&gt;Hi All,&lt;br&gt;
This is my first blog related to PHP and Nodejs. I will go through compare both this technologies and let's understand both languages!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is PHP?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;PHP (Hypertext Preprocessor) was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994. It is highly used languages for creating websites. More than 45% of the Website Still use PHP.&lt;br&gt;
Some of successful websites which are now globally still uses PHP like Facebook, Wikipedia, Tumblr, Slack, etc...&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is Nodejs?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In 2009, Node js was born by Ryan Dahl. The idea was the pick-up V8 engine (V8 is an open-source JavaScript engine developed by the Chromium Project for Google Chrome and Chromium web browsers) out side of web browser so can executes javascript codes at server side. Node js is not language but it is open source run-time environment for back-end scripting for in javascipt.&lt;br&gt;
Node js uses by popular companies like Uber, Trello, PayPal, etc..&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Which one you should Learn?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;For Learning purpose you can dirty your hands on both. PHP is easy to learn and can create a website easily with only some of knowledge of html and css and can deploy with one click. &lt;br&gt;
While in Nodejs You must understand asynchronous behaviour and syntax of javacript. It's not big deal, but must have knowledge of async/await, callbacks, promises, modules, prototypes.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Which one you should pick for next project?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Well, This is interesting. Both languages have their own pros and cons. It does mean that you can not build same application in either PHP or Node js. It depends on requirements of project. &lt;br&gt;
If you need build a blogging, E-commerce website then PHP best suitable. &lt;br&gt;
If you building realtime application like location based or chat application then node js best. Node js also ideal when frontend technologies are also javascript framework like reactjs OR Angular.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy Coding!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>computerscience</category>
      <category>node</category>
      <category>php</category>
    </item>
  </channel>
</rss>
