Connect with us

How to Create Rest API Authentication with Laravel 5.6 and 5.7 using Passport (source code available)




NEWS:   How to Create Rest API Authentication with Laravel 5.6 and 5.7 using Passport (source code available) [New  Developer] » Naijacrawl
Photo: Naijacrawl
Download zip 518.42 Kb


You will learn how  to Create Rest API Authentication with Laravel 5.6 and 5.7 using Passport  after going through this article.


Step 1. Install Laravel


In first step, we require to get fresh Laravel application using below command, So open your terminal or command prompt and run below command:


laravel new auth


Step 2. Install Laravel Passport Package


Laravel Passport provides a full OAuth2 server implementation for your Laravel application in a matter of minutes.

composer require laravel/passport


Step 3. Run Migration


The Passport migrations will create the tables your application needs to store clients and access tokens.

php artisan migrate


Step 4. Generate keys


This command will create the encryption keys needed to generate secure access tokens. In addition, the command will create “personal access” and “password grant” clients which will be used to generate access tokens:

php artisan passport:install


After running this command, add the Laravel\Passport\HasApiTokens trait to your App\Usermodel. This trait will provide a few helper methods to your model which allow you to inspect the authenticated user's token and scopes:


Step 5. Passport Config


Next, you should call the Passport::routes method within the boot method of your AuthServiceProvider. This method will register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens:


     * Register any authentication / authorization services.
     * @return void
    public function boot()

Finally, in your config/auth.php configuration file, you should set the driver option of the api authentication guard to passport. This will instruct your application to use Passport's TokenGuard when authenticating incoming API requests:


'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    'api' => [
        'driver' => 'passport',
        'provider' => 'users',



Step 6. Create API Routes


We will create api routes. Laravel provide routes/api.php file for write web services route. So, let’s add new route on that file.


], function () {
    Route::post('login', 'AuthController@login');
    Route::post('signup', 'AuthController@signup');
      'middleware' => 'auth:api'
    ], function() {
        Route::get('logout', 'AuthController@logout');
        Route::get('user', 'AuthController@user');


Step 7: Create Controller


In last step we have to create new controller and four api method. So let’s create AuthController and put bellow code:


            'name' => 'required|string',
            'email' => 'required|string|email|unique:users',
            'password' => 'required|string|confirmed'
        $user = new User([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password)
        return response()->json([
            'message' => 'Successfully created user!'
        ], 201);
     * Login user and create token
     * @param  [string] email
     * @param  [string] password
     * @param  [boolean] remember_me
     * @return [string] access_token
     * @return [string] token_type
     * @return [string] expires_at
    public function login(Request $request)
            'email' => 'required|string|email',
            'password' => 'required|string',
            'remember_me' => 'boolean'
        $credentials = request(['email', 'password']);
            return response()->json([
                'message' => 'Unauthorized'
            ], 401);
        $user = $request->user();
        $tokenResult = $user->createToken('Personal Access Token');
        $token = $tokenResult->token;
        if ($request->remember_me)
            $token->expires_at = Carbon::now()->addWeeks(1);
        return response()->json([
            'access_token' => $tokenResult->accessToken,
            'token_type' => 'Bearer',
            'expires_at' => Carbon::parse(
     * Logout user (Revoke the token)
     * @return [string] message
    public function logout(Request $request)
        return response()->json([
            'message' => 'Successfully logged out'
     * Get the authenticated User
     * @return [json] user object
    public function user(Request $request)
        return response()->json($request->user());


Now we are ready to run our example so run bellow command to quick run:

php artisan serve


Now, we can simple test by rest client tools (Postman), So I test it and you can see below screenshots.


 Screen shots coming soon

Thanks for reading 

Click Here To Comment

Download File 518.42 Kb 39



Adike Kizito is a top-rated software developer, blogger, sports, and entertainment lover. He is the CEO of these popular websites and He operates his software developing task through,... Read More

Continue Reading
1 Comment

1 Comment

  1.   kizito

    reading this post, will provide a screenshot on how to use postman to access the api 

Leave a Reply

Your email address will not be published. Required fields are marked *


Download Naijacrawl App today

Fastest way to read on the go

Download the Naijacrawl App.