2016-05-30 05:50:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
|
|
|
use App\Http\Requests\API\UserLoginRequest;
|
|
|
|
use Exception;
|
2017-06-04 01:30:45 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2016-05-30 05:50:59 +00:00
|
|
|
use JWTAuth;
|
|
|
|
use Log;
|
|
|
|
|
|
|
|
class AuthController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Log a user in.
|
|
|
|
*
|
|
|
|
* @param UserLoginRequest $request
|
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
2016-05-30 05:50:59 +00:00
|
|
|
*/
|
|
|
|
public function login(UserLoginRequest $request)
|
|
|
|
{
|
2017-12-09 18:34:27 +00:00
|
|
|
$token = JWTAuth::attempt($request->only('email', 'password'));
|
|
|
|
abort_unless($token, 401, 'Invalid credentials');
|
2016-05-30 05:50:59 +00:00
|
|
|
|
|
|
|
return response()->json(compact('token'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log the current user out.
|
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
2016-05-30 05:50:59 +00:00
|
|
|
*/
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
if ($token = JWTAuth::getToken()) {
|
|
|
|
try {
|
|
|
|
JWTAuth::invalidate($token);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
}
|