-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FWW-16: Removed passport and created auth with jwt with the client model
- Loading branch information
1 parent
c912050
commit 71a9589
Showing
10 changed files
with
207 additions
and
107 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,23 @@ | ||
# fibonacci-web | ||
Vendor fibonacci | ||
|
||
|
||
To use the middlewares you will have to register them in app/Http/Kernel.php under the $routeMiddleware property: | ||
|
||
protected $routeMiddleware = [ | ||
... | ||
'jwt.auth' => 'Tymon\JWTAuth\Middleware\GetUserFromToken', | ||
'jwt.refresh' => 'Tymon\JWTAuth\Middleware\RefreshToken', | ||
]; | ||
|
||
|
||
Add the following code to the render method within app/Exceptions/Handler.php | ||
|
||
if ($exception instanceof Tymon\JWTAuth\Exceptions\TokenExpiredException) { | ||
return response()->json(['error'=>'token_expired'], $exception->getStatusCode()); | ||
} else if ($exception instanceof Tymon\JWTAuth\Exceptions\TokenInvalidException) { | ||
return response()->json(['token_invalid'], $exception->getStatusCode()); | ||
} else if($exception instanceof \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException ){ | ||
return response()->json(['error'=>'token_not_found'], $exception->getStatusCode()); | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Kadevjo\Fibonacci\Controllers\Auth; | ||
|
||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Routing\Controller as BaseController; | ||
use Illuminate\Foundation\Validation\ValidatesRequests; | ||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | ||
|
||
class JwtApiController extends BaseController | ||
{ | ||
/** | ||
* Create a new AuthController instance. | ||
* | ||
* @return void | ||
*/ | ||
protected $guard = 'api'; | ||
|
||
public function __construct() | ||
{ | ||
//$this->middleware('auth:api', ['except' => ['login']]); | ||
} | ||
|
||
/** | ||
* Get a JWT via given credentials. | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function login() | ||
{ | ||
$credentials = request(['email', 'password']); | ||
if ($token = auth('api')->attempt($credentials)) { | ||
$user = \Kadevjo\Fibonacci\Models\Client::where('email',$credentials['email'])->first(); | ||
return response()->json(['user'=>$user,'token'=>$token]); | ||
} | ||
return response()->json(['error' => 'Unauthorized'], 401); | ||
} | ||
|
||
/** | ||
* Get the authenticated User. | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function me() | ||
{ | ||
return response()->json(\JWTAuth::parseToken()->authenticate()); | ||
} | ||
|
||
/** | ||
* Log the user out (Invalidate the token). | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function logout() | ||
{ | ||
auth('api')->logout(); | ||
return response()->json(['message' => 'Successfully logged out']); | ||
} | ||
|
||
/** | ||
* Refresh a token. | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function refresh() | ||
{ | ||
$user = \JWTAuth::parseToken()->authenticate(); | ||
return response()->json(['user'=>$user,'token'=>auth('api')->refresh()]); | ||
} | ||
|
||
/** | ||
* Get the token array structure. | ||
* | ||
* @param string $token | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
protected function respondWithToken($token) | ||
{ | ||
return response()->json([ | ||
'access_token' => $token, | ||
'token_type' => 'bearer', | ||
'expires_in' => auth('api')->factory()->getTTL() * 60 | ||
]); | ||
} | ||
} |
Oops, something went wrong.