Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Services\Serializer;
use Cose\Algorithms;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;
Expand All @@ -23,7 +24,7 @@

class GeneratePasskeyRegisterOptionsController extends Controller
{
public function __invoke(Request $request, Serializer $serializer): JsonResponse|string
public function __invoke(#[CurrentUser] User $user, Serializer $serializer): JsonResponse|string
{
// 建立一個信賴方實體
// id 是網站的網域名稱
Expand All @@ -37,13 +38,13 @@ public function __invoke(Request $request, Serializer $serializer): JsonResponse
// id 必須是唯一的,通常是用戶的 ID 或 UUID
// 需要注意的是,name 不可以使用用戶的敏感資訊,例如 email 或電話號碼
$userEntity = new PublicKeyCredentialUserEntity(
name: $request->user()->name,
id: (string) $request->user()->id,
displayName: $request->user()->name
name: $user->name,
id: (string) $user->id,
displayName: $user->name
);
} catch (InvalidDataException $e) {
Log::error('無法建立 Webauthn 用戶實體', [
'user_id' => $request->user()->id,
'user_id' => $user->id,
'exception' => $e->getMessage(),
]);

Expand Down Expand Up @@ -81,7 +82,7 @@ public function __invoke(Request $request, Serializer $serializer): JsonResponse
);
} catch (InvalidDataException $e) {
Log::error('無法建立 Webauthn 註冊選項', [
'user_id' => $request->user()->id,
'user_id' => $user->id,
'exception' => $e->getMessage(),
]);

Expand All @@ -95,7 +96,7 @@ public function __invoke(Request $request, Serializer $serializer): JsonResponse
$optionsJson = $serializer->toJson($options);
} catch (SerializerExceptionInterface $e) {
Log::error('Webauthn 註冊選項序列化失敗', [
'user_id' => $request->user()->id,
'user_id' => $user->id,
'exception' => $e->getMessage(),
]);

Expand Down
15 changes: 9 additions & 6 deletions resources/views/components/comments/⚡create-modal.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use App\Livewire\Forms\CommentForm;
use App\Models\Comment;
use App\Models\Post;
use App\Models\User;
use App\Notifications\NewComment;
use App\Rules\Captcha;
use App\Traits\MarkdownConverter;
use Illuminate\Container\Attributes\CurrentUser;
use Livewire\Attributes\Locked;
use Livewire\Component;

Expand All @@ -24,13 +26,14 @@

public bool $previewIsEnable = false;

public function mount(): void
public function mount(#[CurrentUser] ?User $user): void
{
$this->currentUser = $user;
$this->form->post_id = $this->postId;
$this->form->user_id = auth()->id();
$this->form->user_id = $user?->id;
}

public function save(): void
public function save(#[CurrentUser] ?User $user): void
{
$this->validate(
rules: [
Expand Down Expand Up @@ -78,8 +81,8 @@ public function save(): void
'body' => $comment->body,
'created_at' => $comment->created_at->toDateTimeString(),
'updated_at' => $comment->updated_at->toDateTimeString(),
'user_name' => auth()->check() ? auth()->user()->name : null,
'user_gravatar_url' => auth()->check() ? get_gravatar(auth()->user()->email) : null,
'user_name' => $user?->name,
'user_gravatar_url' => $user !== null ? get_gravatar($user->email) : null,
'children_count' => 0,
],
);
Expand Down Expand Up @@ -198,7 +201,7 @@ class="space-y-2"
>
<div class="relative space-x-4">
<span class="font-semibold dark:text-zinc-50">
{{ auth()->check() ? auth()->user()->name : '訪客' }}
{{ auth()->check() ? auth()->user()->name : '訪客' }}
</span>
<span class="text-zinc-400">{{ now()->format('Y 年 m 月 d 日') }}</span>
</div>
Expand Down
10 changes: 7 additions & 3 deletions resources/views/components/comments/⚡edit-modal.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use App\Livewire\Forms\CommentForm;
use App\Models\Comment;
use App\Models\User;
use App\Traits\MarkdownConverter;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Container\Attributes\CurrentUser;
use Livewire\Component;

new class extends Component
Expand All @@ -21,9 +23,11 @@

public bool $previewIsEnable = false;

public function mount(): void
{
if (! auth()->check()) {
public function mount(
#[CurrentUser]
?User $user
): void {
if ($user === null) {
throw new Exception(message: 'Edit modal part component requires authentication.');
}
}
Expand Down
7 changes: 5 additions & 2 deletions resources/views/components/layouts/⚡header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Livewire\Actions\Logout;
use App\Models\Category;
use App\Models\User;
use App\Services\SettingService;
use Illuminate\Support\Facades\Cache;
use Livewire\Component;
Expand Down Expand Up @@ -59,6 +60,7 @@ public function logout(Logout $logout): void
</script>
@endscript


@php
$hasUnreadNotifications = auth()->check() && auth()->user()->unreadNotifications()->exists();
@endphp
Expand Down Expand Up @@ -234,7 +236,7 @@ class="lg:hidden bg-zinc-50 dark:bg-zinc-800"
id="mobile-header"
>
<div class="px-2 mx-auto max-w-7xl sm:px-6 lg:px-8">
<div class="flex relative justify-between items-center h-[4.5rem]">
<div class="flex relative justify-between items-center h-18">
<div class="flex absolute inset-y-0 left-0 items-center">
{{-- category dropdown menu toggle --}}
<button
Expand Down Expand Up @@ -371,7 +373,8 @@ class="absolute right-0 top-12"
<span class="ml-2">個人資訊</span>
</x-dropdown.link>

<x-dropdown.link href="{{ route('settings.users.edit', ['id' => auth()->id()]) }}">
<x-dropdown.link
href="{{ route('settings.users.edit', ['id' => auth()->id()]) }}">
<x-icons.geer-fill class="w-4" />
<span class="ml-2">設定</span>
</x-dropdown.link>
Expand Down
12 changes: 8 additions & 4 deletions resources/views/pages/notifications/⚡index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use App\Models\User;
use Illuminate\Container\Attributes\CurrentUser;
use Livewire\Attributes\Title;
use Livewire\Component;
use Livewire\WithPagination;
Expand All @@ -10,12 +12,14 @@
{
use WithPagination;

public function render()
{
auth()->user()->unreadNotifications->markAsRead();
public function render(
#[CurrentUser]
User $user
) {
$user->unreadNotifications->markAsRead();

return $this->view([
'notifications' => auth()->user()->notifications()->paginate(20),
'notifications' => $user->notifications()->paginate(20),
])->title('我的通知');
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function rules(): array
$passwordRule = Password::min(8)->letters()->mixedCase()->numbers();

return [
'current_password' => ['required', new MatchOldPassword()],
'current_password' => ['required', new MatchOldPassword],
'new_password' => ['required', 'confirmed', $passwordRule],
];
}
Expand Down
7 changes: 4 additions & 3 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use App\Http\Controllers\Api\ShowLatestPostController;
use App\Http\Controllers\Api\TwitterOembedController;
use App\Http\Controllers\Api\UploadImageController;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Support\Facades\Route;

/*
Expand All @@ -20,8 +21,8 @@
|
*/

Route::middleware('auth:sanctum')->get('user', function (Request $request) {
return $request->user();
Route::middleware('auth:sanctum')->get('user', function (#[CurrentUser] User $user) {
return $user;
});

Route::get('/passkeys/register-options', GeneratePasskeyRegisterOptionsController::class)
Expand Down
Loading