Search code examples
phplaravelswaggerl5-swaggerdarkaonlinel5-swagger

Using Bearer token in l5 swagger laravel has a.forEach error


I've using l5-swagger in Laravel and I need sanctum authorization with bearer token But when I set bearer token and try an API that used this token, the swagger return this error:

"darkaonline/l5-swagger": "^8.5", "zircote/swagger-php": "^4.8"

TypeError: P.forEach is not a function
    at applySecurities (build-request.js:106:12)
    at buildRequest (build-request.js:16:9)
    at Object.execute_buildRequest [as buildRequest] (index.js:249:11)
    at actions.js:454:24
    at index.js:174:16
    at redux.mjs:331:12
    at wrap-actions.js:33:10
    at Object.newAction (system.js:175:26)
    at Object.executeRequest (system.js:487:17)
    at actions.js:501:22
(anonymous) @ system.js:490
(anonymous) @ actions.js:501
(anonymous) @ index.js:174
(anonymous) @ redux.mjs:331
(anonymous) @ wrap-actions.js:9
newAction @ system.js:175
(anonymous) @ system.js:487
handleValidationResultPass @ execute.jsx:66
handleValidationResult @ execute.jsx:80
onClick @ execute.jsx:90
....

This is my l5-swagger config

'securityDefinitions' => [
            'securitySchemes' => [
                /*
                 * Examples of Security schemes
                */
                'sanctum' => [ // Unique name of security
                    'securityDefinition' => "Bearer",
                    'type' => 'apiKey', // Valid values are "basic", "apiKey" or "oauth2".
                    'description' => 'Enter token in format (Bearer <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".
                ],

            ],
            'security' => [
                /*
                 * Examples of Securities
                */
                [
                    'sanctum' => []
                    /*
                    'oauth2_security_example' => [
                        'read',
                        'write'
                    ],

                    'passport' => []
                    */
                ],
            ],
        ],

This is OA annotation that I've used in my controller:

/**
     * @OA\Post(
     *     path="/manager/user/add",
     *     tags={"Manager"},
     *     security={"sanctum": {}},
     *     @OA\Response(response="200", description="An example resource",@OA\JsonContent()),
     *     @OA\Response(response="401", description="unAutosize",@OA\JsonContent()),
     *     @OA\RequestBody(
     *     required=true,
     *     @OA\JsonContent(
     *     required={"name", "email","password"},
     *      @OA\Property(
     *          property="name",
     *          type="string",
     *          ),
     *      @OA\Property(
     *          property="email",
     *          type="string",
     *          ),
     *      @OA\Property(
     *          property="password",
     *          type="string"
     *          )
     *      )))
     * )
     */




Solution

  • I found another solution for this problem:

    You can use this annotate in Controller.php

    /**
    * @OA\Info(
    * title="Project API",
    * version="0.1",
    * )
    * @OA\SecurityScheme(
    * type="http",
    * securityScheme="bearerAuth",
    * scheme="bearer",
    * bearerFormat="JWT"
    * )
    
    */
    
    

    And then in your api controller write this:

    /**
         * @OA\Post(
         *     path="/manager/user/add",
         *     tags={"Manager"},
         *    security={{"bearerAuth":{}}},
         *    . 
         *    .
         * )
         */