<?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: Moudjar Abdelkader</title>
    <description>The latest articles on DEV Community by Moudjar Abdelkader (@abdoumjr).</description>
    <link>https://dev.to/abdoumjr</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%2F166362%2Fa58f60db-c619-4827-b3e9-1d72500c072d.jpeg</url>
      <title>DEV Community: Moudjar Abdelkader</title>
      <link>https://dev.to/abdoumjr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdoumjr"/>
    <language>en</language>
    <item>
      <title>Laravel Passport, Create REST API With Authentication</title>
      <dc:creator>Moudjar Abdelkader</dc:creator>
      <pubDate>Sat, 11 May 2019 01:23:04 +0000</pubDate>
      <link>https://dev.to/abdoumjr/laravel-passport-create-rest-api-with-authentication-5enk</link>
      <guid>https://dev.to/abdoumjr/laravel-passport-create-rest-api-with-authentication-5enk</guid>
      <description>&lt;p&gt;Application Program Interfaces, APIs, are snippets of code that allow one software application to talk to another, providing a common language. Whether allowing seamless experiences for end users across multiple applications, or allowing data from one application to be fed into another, APIs have revolutionised in the last years.&lt;/p&gt;

&lt;p&gt;If you are a beginner and you are learning and figuring out how to make apis and secure them, then you came to the right place, in this article i will show you how to set up an api authentication.&lt;/p&gt;

&lt;p&gt;What is passport ?&lt;br&gt;
Laravel Passport is a full OAuth2 server implementation, it was built to make it easy to apply authentication over an API for laravel based web applications.&lt;/p&gt;

&lt;p&gt;Lets start&lt;/p&gt;

&lt;p&gt;After setting up laravel and installing composer please follow the following steps:&lt;/p&gt;

&lt;p&gt;1- Install Passport via the Composer package manager:&lt;/p&gt;

&lt;p&gt;composer require laravel/passport&lt;/p&gt;

&lt;p&gt;the passport package will register its own database migrations.&lt;/p&gt;

&lt;p&gt;2- Migrate the passport tables:&lt;/p&gt;

&lt;p&gt;php artisan migrate&lt;/p&gt;

&lt;p&gt;3- Install passport:&lt;/p&gt;

&lt;p&gt;php artisan passport:install&lt;/p&gt;

&lt;p&gt;This command will create the encryption keys needed to generate secure access tokens.&lt;/p&gt;

&lt;p&gt;4- Configuring passport:&lt;/p&gt;

&lt;p&gt;add the Laravel\Passport\HasApiTokens trait to your App\Usermodel.&lt;/p&gt;

&lt;p&gt;→ /project/app/User.php&lt;/p&gt;

&lt;p&gt;&lt;a href="https://thepracticaldev.s3.amazonaws.com/i/ovyjolcls53zenwm2m7m.png"&gt;https://thepracticaldev.s3.amazonaws.com/i/ovyjolcls53zenwm2m7m.png&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Call Passport::routes method within the boot method of your AuthServiceProvider&lt;/p&gt;

&lt;p&gt;→ /project/app/Providers/AuthServiceProvider.php&lt;/p&gt;

&lt;p&gt;&lt;a href="https://thepracticaldev.s3.amazonaws.com/i/3yqsq0ut0lm6ik36t124.png"&gt;https://thepracticaldev.s3.amazonaws.com/i/3yqsq0ut0lm6ik36t124.png&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set the driver option of the api authentication guard to passport&lt;/p&gt;

&lt;p&gt;→ /project/config/auth.php&lt;/p&gt;

&lt;p&gt;&lt;a href="https://thepracticaldev.s3.amazonaws.com/i/xjecpwgz8x14bz9ms7ox.png"&gt;https://thepracticaldev.s3.amazonaws.com/i/xjecpwgz8x14bz9ms7ox.png&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5- Creating the routes&lt;/p&gt;

&lt;p&gt;→/project/routes/api.php&lt;/p&gt;

&lt;p&gt;&amp;lt;?php&lt;br&gt;
use Illuminate\Http\Request;&lt;br&gt;
/*&lt;br&gt;
| — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —&lt;br&gt;
| API Routes&lt;br&gt;
| — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —&lt;br&gt;
|&lt;br&gt;
| Here is where you can register API routes for your application. These&lt;br&gt;
| routes are loaded by the RouteServiceProvider within a group which&lt;br&gt;
| is assigned the “api” middleware group. Enjoy building your API!&lt;br&gt;
|&lt;br&gt;
*/&lt;br&gt;
Route::post(‘login’, ‘AuthController@login’);&lt;br&gt;
Route::post(‘register’, ‘AuthController@register’);&lt;br&gt;
Route::middleware(‘auth:api’)-&amp;gt;get(‘/user’, function (Request $request) {&lt;br&gt;
return $request-&amp;gt;user();&lt;br&gt;
});&lt;br&gt;
6- Creating the controller&lt;/p&gt;

&lt;p&gt;php artisan make:controller AuthController&lt;br&gt;
then just copy and paste the code below to your AuthController :&lt;/p&gt;

&lt;p&gt;&amp;lt;?php&lt;br&gt;
namespace App\Http\Controllers;&lt;br&gt;
use Illuminate\Http\Request;&lt;br&gt;
use App\Http\Controllers\Controller;&lt;br&gt;
use App\User;&lt;br&gt;
use Illuminate\Support\Facades\Auth;&lt;br&gt;
use Validator;&lt;br&gt;
class AuthController extends Controller&lt;br&gt;
{&lt;br&gt;
/**&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;login api
*&lt;/li&gt;
&lt;li&gt;@return \Illuminate\Http\Response
&lt;em&gt;/
public function login(){
if(Auth::attempt([‘email’ =&amp;gt; request(‘email’), ‘password’ =&amp;gt; request(‘password’)])){
$user = Auth::user();
$success[‘token’] = $user-&amp;gt;createToken(‘myApp’)-&amp;gt; accessToken;
return response()-&amp;gt;json([‘success’ =&amp;gt; $success], 200);
}
else{
return response()-&amp;gt;json([‘error’=&amp;gt;’Unauthorised’], 401);
}
}
/&lt;/em&gt;*&lt;/li&gt;
&lt;li&gt;Register api
*&lt;/li&gt;
&lt;li&gt;@return \Illuminate\Http\Response
*/
public function register(Request $request)
{
$validator = Validator::make($request-&amp;gt;all(), [
‘name’ =&amp;gt; ‘required’,
‘email’ =&amp;gt; ‘required|email’,
‘password’ =&amp;gt; ‘required’,
‘confirm_password’ =&amp;gt; ‘required|same:password’,
]);
if ($validator-&amp;gt;fails()) {
return response()-&amp;gt;json([‘error’=&amp;gt;$validator-&amp;gt;errors()], 401);
}
$input = $request-&amp;gt;all();
$input[‘password’] = bcrypt($input[‘password’]);
$user = User::create($input);
$success[‘token’] = $user-&amp;gt;createToken(‘myApp’)-&amp;gt; accessToken;
$success[‘name’] = $user-&amp;gt;name;
return response()-&amp;gt;json([‘success’=&amp;gt;$success], 200);
}
}&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before your application can issue personal access tokens, you will need to create a personal access client:&lt;/p&gt;

&lt;p&gt;You need to create a personal access token&lt;/p&gt;

&lt;p&gt;php artisan passport:client --personal&lt;/p&gt;

&lt;p&gt;Finally, let’s try our register and login functionality :&lt;/p&gt;

&lt;p&gt;php artisan serve&lt;/p&gt;

&lt;p&gt;For me, i’m using insomnia for HTTP-based APIs, to send http requests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://thepracticaldev.s3.amazonaws.com/i/69j0jgndf38s5jkfktc5.png"&gt;https://thepracticaldev.s3.amazonaws.com/i/69j0jgndf38s5jkfktc5.png&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By sending a register request with all the data needed we can see a success response from our api , with a special token, we can use this token to communicate with the api.&lt;/p&gt;

&lt;p&gt;Now, after that if we disconnect or the token has expired we can login again and get our token, throw the login api :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://thepracticaldev.s3.amazonaws.com/i/qibwbe4h3s3xcopsqwdt.png"&gt;https://thepracticaldev.s3.amazonaws.com/i/qibwbe4h3s3xcopsqwdt.png&lt;/a&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>laravel</category>
      <category>passport</category>
    </item>
  </channel>
</rss>
