2016-05-30 05:50:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
|
|
|
use App\Http\Requests\API\ProfileUpdateRequest;
|
2020-09-13 22:04:07 +00:00
|
|
|
use App\Models\User;
|
2021-01-31 17:21:57 +00:00
|
|
|
use App\Services\TokenManager;
|
2020-09-13 22:04:07 +00:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2018-08-22 19:46:36 +00:00
|
|
|
use Illuminate\Contracts\Hashing\Hasher as Hash;
|
2016-05-30 05:50:59 +00:00
|
|
|
|
|
|
|
class ProfileController extends Controller
|
|
|
|
{
|
2018-08-22 19:46:36 +00:00
|
|
|
private $hash;
|
2021-01-31 17:21:57 +00:00
|
|
|
private $tokenManager;
|
2018-08-22 19:46:36 +00:00
|
|
|
|
2020-09-13 22:04:07 +00:00
|
|
|
/** @var User */
|
|
|
|
private $currentUser;
|
|
|
|
|
2021-01-31 17:21:57 +00:00
|
|
|
public function __construct(Hash $hash, TokenManager $tokenManager, ?Authenticatable $currentUser)
|
2018-08-22 19:46:36 +00:00
|
|
|
{
|
|
|
|
$this->hash = $hash;
|
2021-01-31 17:21:57 +00:00
|
|
|
$this->tokenManager = $tokenManager;
|
2020-09-13 22:04:07 +00:00
|
|
|
$this->currentUser = $currentUser;
|
2018-08-22 19:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 22:04:07 +00:00
|
|
|
public function show()
|
2017-08-27 15:53:17 +00:00
|
|
|
{
|
2020-09-13 22:04:07 +00:00
|
|
|
return response()->json($this->currentUser);
|
2017-08-27 15:53:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 05:50:59 +00:00
|
|
|
public function update(ProfileUpdateRequest $request)
|
|
|
|
{
|
2019-11-06 11:33:40 +00:00
|
|
|
if (config('koel.misc.demo')) {
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:50:59 +00:00
|
|
|
$data = $request->only('name', 'email');
|
|
|
|
|
2017-04-29 02:55:41 +00:00
|
|
|
if ($request->password) {
|
2018-08-22 19:46:36 +00:00
|
|
|
$data['password'] = $this->hash->make($request->password);
|
2016-05-30 05:50:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 22:04:07 +00:00
|
|
|
$this->currentUser->update($data);
|
|
|
|
|
2021-01-31 17:21:57 +00:00
|
|
|
$responseData = $request->password
|
|
|
|
? ['token' => $this->tokenManager->refreshToken($this->currentUser)->plainTextToken]
|
|
|
|
: [];
|
|
|
|
|
|
|
|
return response()->json($responseData);
|
2016-05-30 05:50:59 +00:00
|
|
|
}
|
|
|
|
}
|