mirror of
https://github.com/koel/koel
synced 2024-12-04 09:49:23 +00:00
124 lines
2.9 KiB
PHP
124 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Http\Requests\API\ProfileUpdateRequest;
|
|
use App\Http\Requests\API\UserLoginRequest;
|
|
use App\Http\Requests\API\UserStoreRequest;
|
|
use App\Http\Requests\API\UserUpdateRequest;
|
|
use App\Models\User;
|
|
use Exception;
|
|
use Hash;
|
|
use JWTAuth;
|
|
use Log;
|
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
/**
|
|
* Log a user in.
|
|
*
|
|
* @param UserLoginRequest $request
|
|
*
|
|
* @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) {
|
|
Log:error($e);
|
|
|
|
return response()->json(['error' => 'could_not_create_token'], 500);
|
|
}
|
|
|
|
return response()->json(compact('token'));
|
|
}
|
|
|
|
/**
|
|
* Log the current user out.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function logout()
|
|
{
|
|
if ($token = JWTAuth::getToken()) {
|
|
try {
|
|
JWTAuth::invalidate($token);
|
|
} catch (Exception $e) {
|
|
Log::error($e);
|
|
}
|
|
}
|
|
|
|
return response()->json();
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
* @param User $user
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function update(UserUpdateRequest $request, User $user)
|
|
{
|
|
$data = $request->only('name', 'email');
|
|
|
|
if ($password = $request->input('password')) {
|
|
$data['password'] = Hash::make($password);
|
|
}
|
|
|
|
return response()->json($user->update($data));
|
|
}
|
|
|
|
/**
|
|
* Delete a user.
|
|
*
|
|
* @param User $user
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function destroy(User $user)
|
|
{
|
|
$this->authorize($user);
|
|
|
|
return response()->json($user->delete());
|
|
}
|
|
|
|
/**
|
|
* Update the current user's profile.
|
|
*
|
|
* @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));
|
|
}
|
|
}
|