LAR_Maintenance/app/Http/Controllers/Auth/ProfileController.php
JonatanRek 571df9a0a4 Fixes
2024-07-30 18:13:21 +02:00

71 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Auth\UpdateUserRequest;
use App\Http\Requests\Auth\CreateApiTokenRequest;
use App\Http\Requests\Auth\RemoveApiTokenRequest;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Hash;
class ProfileController extends BaseController
{
public function index(Request $request)
{
return view('auth.profile', [
'user' => $request->user(),
'sessions' => [],
]);
}
public function update(UpdateUserRequest $request): RedirectResponse
{
$validated = $request->validated();
if (!Hash::check($validated['password'], auth()->user()->password))
{
return redirect()->route('profile.index')->with('error', __('boilerplate::ui.incorect.old.password'));
}
if (!empty($validated['newPassword']) && !empty($validated['password'])) {
$validated['password'] = Hash::make($validated['newPassword']);
unset($validated['newPassword']);
} else {
unset($validated['newPassword']);
unset($validated['password']);
}
$request->user()->update($validated);
return redirect()->route('profile.index')->with('success', __('boilerplate::ui.updated'));
}
public function api(Request $request)
{
return view('auth.profile_api', [
'user' => $request->user(),
'tokens' => $request->user()->tokens->all(),
]);
}
public function createApiToken(CreateApiTokenRequest $request): RedirectResponse
{
$validated = $request->validated();
if (empty($validated['expire_at'])) {
$validated['expire_at'] = null;
}
$newToken = $request->user()->createToken($validated['token_name'], ['*'], Carbon::parse($validated['expire_at']))->plainTextToken;
return redirect()->route('profile.api')->with([
'success'=> __('boilerplate::ui.created'),
'secret'=> $newToken,
]);
}
public function removeApiToken(RemoveApiTokenRequest $request): RedirectResponse
{
$validated = $request->validated();
$request->user()->tokens()->where('id', $validated['token_id'])->first()->delete();
return redirect()->route('profile.api')->with('success', __('boilerplate::ui.removed'));
}
}