2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
2015-12-14 13:22:39 +00:00
|
|
|
use App\Http\Requests\API\ProfileUpdateRequest;
|
2015-12-29 01:35:22 +00:00
|
|
|
use App\Http\Requests\API\UserLoginRequest;
|
2015-12-13 04:42:28 +00:00
|
|
|
use App\Http\Requests\API\UserStoreRequest;
|
|
|
|
use App\Http\Requests\API\UserUpdateRequest;
|
2015-12-14 13:22:39 +00:00
|
|
|
use App\Models\User;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Hash;
|
2015-12-29 01:35:22 +00:00
|
|
|
use JWTAuth;
|
2016-01-25 10:37:14 +00:00
|
|
|
use Log;
|
2015-12-29 01:35:22 +00:00
|
|
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2015-12-29 01:35:22 +00:00
|
|
|
/**
|
|
|
|
* Log a user in.
|
2016-02-03 15:39:15 +00:00
|
|
|
*
|
|
|
|
* @param UserLoginRequest $request
|
|
|
|
*
|
2015-12-29 01:35:22 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
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-01-25 10:37:14 +00:00
|
|
|
Log:error($e);
|
2016-01-26 15:49:03 +00:00
|
|
|
|
2015-12-29 01:35:22 +00:00
|
|
|
return response()->json(['error' => 'could_not_create_token'], 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json(compact('token'));
|
|
|
|
}
|
|
|
|
|
2016-01-25 10:37:14 +00:00
|
|
|
/**
|
|
|
|
* Log the current user out.
|
2016-03-06 04:11:28 +00:00
|
|
|
*
|
2016-01-25 10:37:14 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
JWTAuth::invalidate(JWTAuth::getToken());
|
|
|
|
} catch (JWTException $e) {
|
|
|
|
Log:error($e);
|
2016-01-26 15:49:03 +00:00
|
|
|
|
2016-01-25 10:37:14 +00:00
|
|
|
return response()->json(['error' => 'could_not_invalidate_token'], 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Create a new user.
|
|
|
|
*
|
|
|
|
* @param UserStoreRequest $request
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function store(UserStoreRequest $request)
|
|
|
|
{
|
|
|
|
return response()->json(User::create([
|
|
|
|
'name' => $request->input('name'),
|
|
|
|
'email' => $request->input('email'),
|
|
|
|
'password' => Hash::make($request->input('password')),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a user.
|
|
|
|
*
|
|
|
|
* @param UserUpdateRequest $request
|
2015-12-15 10:35:46 +00:00
|
|
|
* @param User $user
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2015-12-15 00:45:10 +00:00
|
|
|
public function update(UserUpdateRequest $request, User $user)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
$data = $request->only('name', 'email');
|
|
|
|
|
|
|
|
if ($password = $request->input('password')) {
|
|
|
|
$data['password'] = Hash::make($password);
|
|
|
|
}
|
|
|
|
|
2015-12-14 16:27:26 +00:00
|
|
|
return response()->json($user->update($data));
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a user.
|
|
|
|
*
|
2015-12-15 10:35:46 +00:00
|
|
|
* @param User $user
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2015-12-14 16:27:26 +00:00
|
|
|
public function destroy(User $user)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2015-12-14 16:27:26 +00:00
|
|
|
$this->authorize($user);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2015-12-14 16:27:26 +00:00
|
|
|
return response()->json($user->delete());
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the current user's profile.
|
2015-12-15 10:35:46 +00:00
|
|
|
*
|
2015-12-13 04:42:28 +00:00
|
|
|
* @param ProfileUpdateRequest $request
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function updateProfile(ProfileUpdateRequest $request)
|
|
|
|
{
|
|
|
|
$data = $request->only('name', 'email');
|
|
|
|
|
|
|
|
if ($password = $request->input('password')) {
|
|
|
|
$data['password'] = Hash::make($password);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json(auth()->user()->update($data));
|
|
|
|
}
|
|
|
|
}
|