Created
September 9, 2018 06:46
-
-
Save vs0uz4/19da2a687731e7ba8a34eb5b02f8b828 to your computer and use it in GitHub Desktop.
Example Swagger Laravel - Definitions for Response Content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Backend\Http\Controllers\Api; | |
use Illuminate\Http\Request; | |
use Backend\Http\Controllers\Controller; | |
use Tymon\JWTAuth\Exceptions\JWTException; | |
class AuthController extends Controller | |
{ | |
/** | |
* @SWG\Swagger( | |
* basePath="/api", | |
* schemes={"http"}, | |
* consumes={"application/json"}, | |
* produces={"application/json"}, | |
* @SWG\Info( | |
* version="0.0.1", | |
* title="Backend API", | |
* description="BillPays API RESTful", | |
* @SWG\Contact( | |
* name="Vitor Rodrigues", | |
* email="vitor.rodrigues@gmail.com", | |
* url="https://github.com/vs0uz4/cr_laravel_com_android" | |
* ), | |
* ), | |
* @SWG\Definition( | |
* definition="User", | |
* required={"id", "name", "email", "created_at", "updated_at"}, | |
* @SWG\Property( | |
* property="id", | |
* type="integer", | |
* format="int64" | |
* ), | |
* @SWG\Property( | |
* property="name", | |
* type="string" | |
* ), | |
* @SWG\Property( | |
* property="email", | |
* type="string" | |
* ), | |
* @SWG\Property( | |
* property="created_at", | |
* type="string", | |
* format="date-time" | |
* ), | |
* @SWG\Property( | |
* property="updated_at", | |
* type="string", | |
* format="date-time" | |
* ) | |
* ) | |
* ) | |
*/ | |
/** | |
* @SWG\Post( | |
* path="/login", | |
* operationId="login", | |
* tags={"Authentication"}, | |
* summary="Request an JWT Token", | |
* description="Request an JWT Token", | |
* @SWG\Parameter( | |
* name="body", | |
* in="body", | |
* required=true, | |
* @SWG\Schema( | |
* @SWG\Property( property="email", type="string" ), | |
* @SWG\Property( property="password", type="string" ), | |
* ) | |
* ), | |
* @SWG\Response( | |
* response="200", | |
* description="Return Token and User", | |
* @SWG\Schema( | |
* required={"token","user"}, | |
* @SWG\Property( property="token", type="string", example="Bearer _token_"), | |
* @SWG\Property( property="user", type="object", ref="#/definitions/User"), | |
* ), | |
* ), | |
* @SWG\Response( | |
* response="401", | |
* description="Invalid Credentials", | |
* ), | |
* @SWG\Response( | |
* response="500", | |
* description="Could not Create Token", | |
* ), | |
* ) | |
* | |
* Request a JWT Token | |
* | |
* @param Request $request | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
public function login(Request $request) | |
{ | |
$credentials = $request->only('email', 'password'); | |
try { | |
$token = \JWTAuth::attempt($credentials); | |
$user = \JWTAuth::authenticate($token); | |
} catch (JWTException $ex) { | |
return response()->json(['error' => 'could_not_create_token'], 500); | |
} | |
if (!$token) { | |
return response()->json(['error' => 'invalid_credentials'], 401); | |
} | |
return response()->json(compact(['token','user']), 200); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment