. Hello, world!

Authentication API's

we will do this using laravel password

composer require laravel/password

 now 

php artisan migrate
php artisan passport:install

now in your user model

//add library
use Laravel\Passport\HasApiTokens;
use laravel\Sanctum\HasApiTokens;
 use HasFactory, Notifiable,HasApiTokens; // Add Has Api Token

Now in your App→Providers→AppserviceProvider in your boot function

use laravel\Passport\passport
  if(! $this→app→routesAreCached())
{
Passport::routes()'
}

Now in config→Auth.php

 

in guards array

 ‘api’=>[
‘driver’=>'passport',
‘provider’=>'users'
]

 

validation in api

when you register a user create token like below

$token=$user→createToken('auth_token')→accessToken;

return response()->json([
'token'=>$token,
'user'=>$user,
'message'=>'User Created Successfully',
'status'=>200
])

 

 

now you will get token when you hit request from post man

now we can use this token for authenticated routes  using token

 

Here api is the guard we created for api

now you need to copy token inside header as shown below to access these routes