koel/app/Http/Controllers/API/AuthController.php

54 lines
1.2 KiB
PHP
Raw Normal View History

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;
use Tymon\JWTAuth\Exceptions\JWTException;
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)
{
try {
if (!$token = JWTAuth::attempt($request->only('email', 'password'))) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
2016-06-10 07:51:08 +00:00
Log::error($e);
2016-05-30 05:50:59 +00:00
return response()->json(['error' => 'could_not_create_token'], 500);
}
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();
}
}