Start Coding with Codelab | Jaeves AI

Swagger Authenticate the API in Laravel – Jaeves Codelab

Written by Admin | Oct 2, 2023 10:53:42 PM

In previous article we Create Swagger Post Method in Laravel and Create Swagger GET Method in Laravel. Now we will do Swagger Authenticate the API in Laravel.

Open config/l5-swagger.php file and add securityDefinitions and securitySchemes. Check Create Swagger Configuration in Laravel.

'securityDefinitions' => [
            'securitySchemes' => [
                /*
                 * Examples of Security schemes
                */
                'Authentication_Token' => [ // Unique name of security
                    'type' => 'apiKey', // The type of the security scheme. Valid values are "basic", "apiKey" or "oauth2".
                    'description' => 'An authorization header. Example: Token',
                    'name' => 'Authorization', // The name of the header or query parameter to be used.
                    'in' => 'header', // The location of the API key. Valid values are "query" or "header".
                ],
            ]
        ]

Now, add security=} comment in functions. If required We can also access the token in header Authorization.

/**
     * @OA\Get(
     *      path="/get-user/{id}",
     *      operationId="getUser",
     *      tags={"User"},
     *      summary="Get User",
     *      description="Get User",
     *      security=},
     *      @OA\Parameter(
     *      name="id",
     *      in="path",
     *      required=true,
     *      description= "user id",
     *      example="10",
     *      @OA\Schema(
     *           type="integer"
     *      )
     *      ),
     *       @OA\Response(
     *      response=200,
     *      description="Success response",
     *      @OA\JsonContent(
     *      @OA\Property(property="status", type="number", example="200"),
     *      @OA\Property(property="user", type="string", example="{'full_name':'Chetan','email_id':'chetan1234@gmail.com','created_at':'2022-05-27T07:16:57.000000Z','updated_at':'2022-05-27T07:16:57.000000Z'}"),
     *        )
     *     ),
     *        @OA\Response(
     *      response=400,
     *      description="Bad Request",
     *      @OA\JsonContent(
     *      @OA\Property(property="status", type="number", example="400"),
     *      @OA\Property(property="message", type="string", example="Error in processing request")
     *        )
     *     )
     * )
     *      
     * )
     */

    public function getUser($id)
    {
    	try {
	    	$user = User::where('id', $id)->get();

	    	if($user){
	    		return response()->json(['status' => 200, 'user' => $user], 200);
	    	}
    	}catch (\Exception $e) {
            return response()->json(['status' => 400, 'message' => 'Error in processing request'], 400);
        }
    }

In routes/api.php add middleware auth:api.

Route::group(['middleware' => ['auth:api']], function () {

});

Now, run php artisan l5-swagger:generate and check api at https://mywebsite.com/api/documentation.

For more code examples click here. Read laravel swagger documentation here.