koel/app/Http/Controllers/API/AuthController.php
2017-12-09 21:10:55 +01:00

45 lines
933 B
PHP

<?php
namespace App\Http\Controllers\API;
use App\Http\Requests\API\UserLoginRequest;
use Exception;
use Illuminate\Http\JsonResponse;
use JWTAuth;
use Log;
class AuthController extends Controller
{
/**
* Log a user in.
*
* @param UserLoginRequest $request
*
* @return JsonResponse
*/
public function login(UserLoginRequest $request)
{
$token = JWTAuth::attempt($request->only('email', 'password'));
abort_unless($token, 401, 'Invalid credentials');
return response()->json(compact('token'));
}
/**
* Log the current user out.
*
* @return JsonResponse
*/
public function logout()
{
if ($token = JWTAuth::getToken()) {
try {
JWTAuth::invalidate($token);
} catch (Exception $e) {
Log::error($e);
}
}
return response()->json();
}
}