Initial project
This commit is contained in:
22
app/Livewire/Actions/Logout.php
Normal file
22
app/Livewire/Actions/Logout.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Actions;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
|
||||
class Logout
|
||||
{
|
||||
/**
|
||||
* Log the current user out of the application.
|
||||
*/
|
||||
public function __invoke()
|
||||
{
|
||||
Auth::guard('web')->logout();
|
||||
|
||||
Session::invalidate();
|
||||
Session::regenerateToken();
|
||||
|
||||
return redirect('/');
|
||||
}
|
||||
}
|
||||
50
app/Livewire/Chat/ChatPage.php
Normal file
50
app/Livewire/Chat/ChatPage.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Attributes\Title;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Title('Chat')]
|
||||
class ChatPage extends Component
|
||||
{
|
||||
public ?int $selectedConversationId = null;
|
||||
|
||||
public bool $detailsPanelOpen = true;
|
||||
|
||||
#[On('conversation-selected')]
|
||||
public function selectConversation(int $conversationId): void
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->findOrFail($conversationId);
|
||||
|
||||
Gate::authorize('view', $conversation);
|
||||
|
||||
$this->selectedConversationId = $conversation->id;
|
||||
$this->detailsPanelOpen = true;
|
||||
}
|
||||
|
||||
#[On('conversation-closed')]
|
||||
public function clearConversation(): void
|
||||
{
|
||||
$this->selectedConversationId = null;
|
||||
$this->detailsPanelOpen = false;
|
||||
}
|
||||
|
||||
#[On('conversation-details-toggled')]
|
||||
public function toggleDetailsPanel(): void
|
||||
{
|
||||
$this->detailsPanelOpen = ! $this->detailsPanelOpen;
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.chat-page');
|
||||
}
|
||||
}
|
||||
66
app/Livewire/Chat/ConversationDetailsPanel.php
Normal file
66
app/Livewire/Chat/ConversationDetailsPanel.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
class ConversationDetailsPanel extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public int $conversationId;
|
||||
|
||||
public function mount(int $conversationId): void
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function conversation(): Conversation
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->with([
|
||||
'participants' => fn ($query) => $query
|
||||
->select(['id', 'conversation_id', 'user_id', 'role', 'joined_at'])
|
||||
->with('user:id,name,email'),
|
||||
])
|
||||
->withCount('messages')
|
||||
->findOrFail($this->conversationId);
|
||||
|
||||
Gate::authorize('view', $conversation);
|
||||
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
if ($this->conversation->isGroup()) {
|
||||
return $this->conversation->name ?? __('Untitled group');
|
||||
}
|
||||
|
||||
return $this->conversation->participants
|
||||
->first(fn (ConversationParticipant $participant) => $participant->user_id !== Auth::id())
|
||||
?->user?->name ?? __('Direct conversation');
|
||||
}
|
||||
|
||||
public function initials(): string
|
||||
{
|
||||
return collect(explode(' ', $this->title()))
|
||||
->filter()
|
||||
->take(2)
|
||||
->map(fn (string $word) => mb_substr($word, 0, 1))
|
||||
->implode('');
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.conversation-details-panel');
|
||||
}
|
||||
}
|
||||
103
app/Livewire/Chat/ConversationHeader.php
Normal file
103
app/Livewire/Chat/ConversationHeader.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\User;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
class ConversationHeader extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public int $conversationId;
|
||||
|
||||
public function mount(int $conversationId): void
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function conversation(): Conversation
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->with([
|
||||
'participants' => fn ($query) => $query
|
||||
->select(['id', 'conversation_id', 'user_id', 'role'])
|
||||
->with('user:id,name,email'),
|
||||
])
|
||||
->findOrFail($this->conversationId);
|
||||
|
||||
Gate::authorize('view', $conversation);
|
||||
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
if ($this->conversation->isGroup()) {
|
||||
return $this->conversation->name ?? __('Untitled group');
|
||||
}
|
||||
|
||||
return $this->otherParticipant()?->name ?? __('Direct conversation');
|
||||
}
|
||||
|
||||
public function subtitle(): string
|
||||
{
|
||||
if ($this->conversation->isGroup()) {
|
||||
return trans_choice(':count member|:count members', $this->conversation->participants->count(), [
|
||||
'count' => $this->conversation->participants->count(),
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->isOnline() ? __('Online') : __('Recently active');
|
||||
}
|
||||
|
||||
public function initials(): string
|
||||
{
|
||||
if (! $this->conversation->isGroup()) {
|
||||
return $this->otherParticipant()?->initials() ?? 'DC';
|
||||
}
|
||||
|
||||
return collect(explode(' ', $this->title()))
|
||||
->filter()
|
||||
->take(2)
|
||||
->map(fn (string $word) => mb_substr($word, 0, 1))
|
||||
->implode('');
|
||||
}
|
||||
|
||||
public function isOnline(): bool
|
||||
{
|
||||
$participant = $this->otherParticipant();
|
||||
|
||||
return $participant instanceof User && $participant->id % 3 !== 0;
|
||||
}
|
||||
|
||||
public function closeConversation(): void
|
||||
{
|
||||
$this->dispatch('conversation-closed');
|
||||
}
|
||||
|
||||
public function toggleDetails(): void
|
||||
{
|
||||
$this->dispatch('conversation-details-toggled');
|
||||
}
|
||||
|
||||
private function otherParticipant(): ?User
|
||||
{
|
||||
return $this->conversation->participants
|
||||
->first(fn (ConversationParticipant $participant) => $participant->user_id !== Auth::id())
|
||||
?->user;
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.conversation-header');
|
||||
}
|
||||
}
|
||||
166
app/Livewire/Chat/ConversationList.php
Normal file
166
app/Livewire/Chat/ConversationList.php
Normal file
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class ConversationList extends Component
|
||||
{
|
||||
public ?int $selectedConversationId = null;
|
||||
|
||||
public string $search = '';
|
||||
|
||||
#[On('message-created')]
|
||||
public function refreshConversations(): void
|
||||
{
|
||||
unset($this->conversations);
|
||||
}
|
||||
|
||||
public function selectConversation(int $conversationId): void
|
||||
{
|
||||
$this->dispatch('conversation-selected', conversationId: $conversationId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Conversation>
|
||||
*/
|
||||
#[Computed]
|
||||
public function conversations(): Collection
|
||||
{
|
||||
$user = Auth::user();
|
||||
$search = trim($this->search);
|
||||
|
||||
return Conversation::query()
|
||||
->select(['id', 'created_by_id', 'type', 'name', 'description', 'created_at', 'updated_at'])
|
||||
->forUser($user)
|
||||
->with([
|
||||
'participants' => fn ($query) => $query
|
||||
->select(['id', 'conversation_id', 'user_id', 'role', 'last_read_at'])
|
||||
->with('user:id,name,email'),
|
||||
'latestMessage' => fn ($query) => $query
|
||||
->select(['messages.id', 'messages.conversation_id', 'messages.user_id', 'messages.body', 'messages.created_at']),
|
||||
'latestMessage.sender:id,name,email',
|
||||
])
|
||||
->withMax('messages', 'created_at')
|
||||
->withCount([
|
||||
'messages as unread_messages_count' => fn (Builder $messages) => $messages
|
||||
->where('user_id', '!=', $user->id)
|
||||
->whereExists(fn ($participants) => $participants
|
||||
->selectRaw('1')
|
||||
->from('conversation_participants')
|
||||
->whereColumn('conversation_participants.conversation_id', 'messages.conversation_id')
|
||||
->where('conversation_participants.user_id', $user->id)
|
||||
->where(fn ($readState) => $readState
|
||||
->whereNull('conversation_participants.last_read_at')
|
||||
->orWhereColumn('messages.created_at', '>', 'conversation_participants.last_read_at'))),
|
||||
])
|
||||
->when($search !== '', fn (Builder $query) => $query->where(function (Builder $query) use ($search, $user): void {
|
||||
$query
|
||||
->where('name', 'like', "%{$search}%")
|
||||
->orWhereHas('participants.user', fn (Builder $users) => $users
|
||||
->where('users.id', '!=', $user->id)
|
||||
->where(fn (Builder $users) => $users
|
||||
->where('name', 'like', "%{$search}%")
|
||||
->orWhere('email', 'like', "%{$search}%")));
|
||||
}))
|
||||
->orderByDesc('messages_max_created_at')
|
||||
->orderByDesc('updated_at')
|
||||
->limit(40)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function titleFor(Conversation $conversation): string
|
||||
{
|
||||
if ($conversation->isGroup()) {
|
||||
return $conversation->name ?? __('Untitled group');
|
||||
}
|
||||
|
||||
return $this->otherParticipant($conversation)?->name ?? __('Direct conversation');
|
||||
}
|
||||
|
||||
public function initialsFor(Conversation $conversation): string
|
||||
{
|
||||
if ($conversation->isGroup()) {
|
||||
return collect(explode(' ', $this->titleFor($conversation)))
|
||||
->filter()
|
||||
->take(2)
|
||||
->map(fn (string $word) => mb_substr($word, 0, 1))
|
||||
->implode('');
|
||||
}
|
||||
|
||||
return $this->otherParticipant($conversation)?->initials() ?? 'DC';
|
||||
}
|
||||
|
||||
public function previewFor(Conversation $conversation): string
|
||||
{
|
||||
if (! $conversation->latestMessage) {
|
||||
return __('No messages yet');
|
||||
}
|
||||
|
||||
$prefix = $conversation->latestMessage->user_id === Auth::id()
|
||||
? __('You: ')
|
||||
: ($conversation->isGroup() ? $conversation->latestMessage->sender?->name.': ' : '');
|
||||
|
||||
return str($prefix.$conversation->latestMessage->body)
|
||||
->squish()
|
||||
->limit(86)
|
||||
->toString();
|
||||
}
|
||||
|
||||
public function timeFor(?CarbonInterface $timestamp): string
|
||||
{
|
||||
if (! $timestamp) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($timestamp->isToday()) {
|
||||
return $timestamp->format('H:i');
|
||||
}
|
||||
|
||||
if ($timestamp->isCurrentYear()) {
|
||||
return $timestamp->format('M j');
|
||||
}
|
||||
|
||||
return $timestamp->format('M j, Y');
|
||||
}
|
||||
|
||||
public function participantSummaryFor(Conversation $conversation): string
|
||||
{
|
||||
if (! $conversation->isGroup()) {
|
||||
return $this->isOnline($conversation) ? __('Online') : __('Recently active');
|
||||
}
|
||||
|
||||
return trans_choice(':count member|:count members', $conversation->participants->count(), [
|
||||
'count' => $conversation->participants->count(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function isOnline(Conversation $conversation): bool
|
||||
{
|
||||
$participant = $this->otherParticipant($conversation);
|
||||
|
||||
return $participant instanceof User && $participant->id % 3 !== 0;
|
||||
}
|
||||
|
||||
private function otherParticipant(Conversation $conversation): ?User
|
||||
{
|
||||
return $conversation->participants
|
||||
->first(fn (ConversationParticipant $participant) => $participant->user_id !== Auth::id())
|
||||
?->user;
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.conversation-list');
|
||||
}
|
||||
}
|
||||
123
app/Livewire/Chat/ConversationView.php
Normal file
123
app/Livewire/Chat/ConversationView.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\Message;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class ConversationView extends Component
|
||||
{
|
||||
public int $conversationId;
|
||||
|
||||
public int $messageLimit = 40;
|
||||
|
||||
public function mount(int $conversationId): void
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
|
||||
$this->authorizeConversation();
|
||||
$this->markAsRead();
|
||||
}
|
||||
|
||||
public function loadEarlier(): void
|
||||
{
|
||||
$this->messageLimit += 25;
|
||||
}
|
||||
|
||||
#[On('message-created')]
|
||||
public function refreshMessages(int $conversationId): void
|
||||
{
|
||||
if ($conversationId !== $this->conversationId) {
|
||||
return;
|
||||
}
|
||||
|
||||
unset($this->messages, $this->hasMoreMessages);
|
||||
|
||||
$this->markAsRead();
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function conversation(): Conversation
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->with([
|
||||
'participants' => fn ($query) => $query
|
||||
->select(['id', 'conversation_id', 'user_id', 'role', 'last_read_at'])
|
||||
->with('user:id,name,email'),
|
||||
])
|
||||
->findOrFail($this->conversationId);
|
||||
|
||||
Gate::authorize('view', $conversation);
|
||||
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Message>
|
||||
*/
|
||||
#[Computed]
|
||||
public function messages(): Collection
|
||||
{
|
||||
return Message::query()
|
||||
->where('conversation_id', $this->conversationId)
|
||||
->with('sender:id,name,email')
|
||||
->latest()
|
||||
->limit($this->messageLimit)
|
||||
->get()
|
||||
->reverse()
|
||||
->values();
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function hasMoreMessages(): bool
|
||||
{
|
||||
return Message::query()
|
||||
->where('conversation_id', $this->conversationId)
|
||||
->count() > $this->messageLimit;
|
||||
}
|
||||
|
||||
public function dateLabel(CarbonInterface $timestamp): string
|
||||
{
|
||||
if ($timestamp->isToday()) {
|
||||
return __('Today');
|
||||
}
|
||||
|
||||
if ($timestamp->isYesterday()) {
|
||||
return __('Yesterday');
|
||||
}
|
||||
|
||||
return $timestamp->format('F j, Y');
|
||||
}
|
||||
|
||||
private function authorizeConversation(): void
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->findOrFail($this->conversationId);
|
||||
|
||||
Gate::authorize('view', $conversation);
|
||||
}
|
||||
|
||||
private function markAsRead(): void
|
||||
{
|
||||
ConversationParticipant::query()
|
||||
->where('conversation_id', $this->conversationId)
|
||||
->where('user_id', Auth::id())
|
||||
->update(['last_read_at' => now()]);
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.conversation-view');
|
||||
}
|
||||
}
|
||||
77
app/Livewire/Chat/MessageComposer.php
Normal file
77
app/Livewire/Chat/MessageComposer.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Chat;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\Message;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
class MessageComposer extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public int $conversationId;
|
||||
|
||||
public string $body = '';
|
||||
|
||||
public function mount(int $conversationId): void
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
|
||||
$this->conversation();
|
||||
}
|
||||
|
||||
public function sendMessage(): void
|
||||
{
|
||||
$this->body = trim($this->body);
|
||||
|
||||
$validated = $this->validate([
|
||||
'body' => ['required', 'string', 'max:4000'],
|
||||
], [
|
||||
'body.required' => __('Write a message before sending.'),
|
||||
]);
|
||||
|
||||
$conversation = $this->conversation();
|
||||
|
||||
Gate::authorize('sendMessage', $conversation);
|
||||
|
||||
$message = Message::query()->create([
|
||||
'conversation_id' => $conversation->id,
|
||||
'user_id' => Auth::id(),
|
||||
'type' => Message::TypeText,
|
||||
'body' => $validated['body'],
|
||||
]);
|
||||
|
||||
$conversation->touch();
|
||||
|
||||
ConversationParticipant::query()
|
||||
->where('conversation_id', $conversation->id)
|
||||
->where('user_id', Auth::id())
|
||||
->update(['last_read_at' => now()]);
|
||||
|
||||
$this->reset('body');
|
||||
$this->resetValidation();
|
||||
|
||||
$this->dispatch('message-created', conversationId: $conversation->id, messageId: $message->id);
|
||||
}
|
||||
|
||||
private function conversation(): Conversation
|
||||
{
|
||||
$conversation = Conversation::query()
|
||||
->forUser(Auth::user())
|
||||
->findOrFail($this->conversationId);
|
||||
|
||||
Gate::authorize('sendMessage', $conversation);
|
||||
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.chat.message-composer');
|
||||
}
|
||||
}
|
||||
12
app/Livewire/Settings/Appearance.php
Normal file
12
app/Livewire/Settings/Appearance.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Settings;
|
||||
|
||||
use Livewire\Attributes\Title;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Title('Appearance settings')]
|
||||
class Appearance extends Component
|
||||
{
|
||||
//
|
||||
}
|
||||
29
app/Livewire/Settings/DeleteUserForm.php
Normal file
29
app/Livewire/Settings/DeleteUserForm.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Settings;
|
||||
|
||||
use App\Concerns\PasswordValidationRules;
|
||||
use App\Livewire\Actions\Logout;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Component;
|
||||
|
||||
class DeleteUserForm extends Component
|
||||
{
|
||||
use PasswordValidationRules;
|
||||
|
||||
public string $password = '';
|
||||
|
||||
/**
|
||||
* Delete the currently authenticated user.
|
||||
*/
|
||||
public function deleteUser(Logout $logout): void
|
||||
{
|
||||
$this->validate([
|
||||
'password' => $this->currentPasswordRules(),
|
||||
]);
|
||||
|
||||
tap(Auth::user(), $logout(...))->delete();
|
||||
|
||||
$this->redirect('/', navigate: true);
|
||||
}
|
||||
}
|
||||
81
app/Livewire/Settings/Profile.php
Normal file
81
app/Livewire/Settings/Profile.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Settings;
|
||||
|
||||
use App\Concerns\ProfileValidationRules;
|
||||
use Flux\Flux;
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Title;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Title('Profile settings')]
|
||||
class Profile extends Component
|
||||
{
|
||||
use ProfileValidationRules;
|
||||
|
||||
public string $name = '';
|
||||
|
||||
public string $email = '';
|
||||
|
||||
/**
|
||||
* Mount the component.
|
||||
*/
|
||||
public function mount(): void
|
||||
{
|
||||
$this->name = Auth::user()->name;
|
||||
$this->email = Auth::user()->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the profile information for the currently authenticated user.
|
||||
*/
|
||||
public function updateProfileInformation(): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$validated = $this->validate($this->profileRules($user->id));
|
||||
|
||||
$user->fill($validated);
|
||||
|
||||
if ($user->isDirty('email')) {
|
||||
$user->email_verified_at = null;
|
||||
}
|
||||
|
||||
$user->save();
|
||||
|
||||
Flux::toast(variant: 'success', text: __('Profile updated.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an email verification notification to the current user.
|
||||
*/
|
||||
public function resendVerificationNotification(): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user->hasVerifiedEmail()) {
|
||||
$this->redirectIntended(default: route('dashboard', absolute: false));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$user->sendEmailVerificationNotification();
|
||||
|
||||
Flux::toast(text: __('A new verification link has been sent to your email address.'));
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function hasUnverifiedEmail(): bool
|
||||
{
|
||||
return Auth::user() instanceof MustVerifyEmail && ! Auth::user()->hasVerifiedEmail();
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function showDeleteUser(): bool
|
||||
{
|
||||
return ! Auth::user() instanceof MustVerifyEmail
|
||||
|| (Auth::user() instanceof MustVerifyEmail && Auth::user()->hasVerifiedEmail());
|
||||
}
|
||||
}
|
||||
227
app/Livewire/Settings/Security.php
Normal file
227
app/Livewire/Settings/Security.php
Normal file
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Settings;
|
||||
|
||||
use App\Concerns\PasswordValidationRules;
|
||||
use Exception;
|
||||
use Flux\Flux;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Laravel\Fortify\Actions\ConfirmTwoFactorAuthentication;
|
||||
use Laravel\Fortify\Actions\DisableTwoFactorAuthentication;
|
||||
use Laravel\Fortify\Actions\EnableTwoFactorAuthentication;
|
||||
use Laravel\Fortify\Features;
|
||||
use Laravel\Fortify\Fortify;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Attributes\Title;
|
||||
use Livewire\Attributes\Validate;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Title('Security settings')]
|
||||
class Security extends Component
|
||||
{
|
||||
use PasswordValidationRules;
|
||||
|
||||
public string $current_password = '';
|
||||
|
||||
public string $password = '';
|
||||
|
||||
public string $password_confirmation = '';
|
||||
|
||||
#[Locked]
|
||||
public bool $canManageTwoFactor;
|
||||
|
||||
#[Locked]
|
||||
public bool $twoFactorEnabled;
|
||||
|
||||
#[Locked]
|
||||
public bool $requiresConfirmation;
|
||||
|
||||
#[Locked]
|
||||
public string $qrCodeSvg = '';
|
||||
|
||||
#[Locked]
|
||||
public string $manualSetupKey = '';
|
||||
|
||||
public bool $showModal = false;
|
||||
|
||||
public bool $showVerificationStep = false;
|
||||
|
||||
#[Validate('required|string|size:6', onUpdate: false)]
|
||||
public string $code = '';
|
||||
|
||||
/**
|
||||
* Mount the component.
|
||||
*/
|
||||
public function mount(DisableTwoFactorAuthentication $disableTwoFactorAuthentication): void
|
||||
{
|
||||
$this->canManageTwoFactor = Features::canManageTwoFactorAuthentication();
|
||||
|
||||
if ($this->canManageTwoFactor) {
|
||||
if (Fortify::confirmsTwoFactorAuthentication() && is_null(auth()->user()->two_factor_confirmed_at)) {
|
||||
$disableTwoFactorAuthentication(auth()->user());
|
||||
}
|
||||
|
||||
$this->twoFactorEnabled = auth()->user()->hasEnabledTwoFactorAuthentication();
|
||||
$this->requiresConfirmation = Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the password for the currently authenticated user.
|
||||
*/
|
||||
public function updatePassword(): void
|
||||
{
|
||||
try {
|
||||
$validated = $this->validate([
|
||||
'current_password' => $this->currentPasswordRules(),
|
||||
'password' => $this->passwordRules(),
|
||||
]);
|
||||
} catch (ValidationException $e) {
|
||||
$this->reset('current_password', 'password', 'password_confirmation');
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
Auth::user()->update([
|
||||
'password' => $validated['password'],
|
||||
]);
|
||||
|
||||
$this->reset('current_password', 'password', 'password_confirmation');
|
||||
|
||||
Flux::toast(variant: 'success', text: __('Password updated.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable two-factor authentication for the user.
|
||||
*/
|
||||
public function enable(EnableTwoFactorAuthentication $enableTwoFactorAuthentication): void
|
||||
{
|
||||
$enableTwoFactorAuthentication(auth()->user());
|
||||
|
||||
if (! $this->requiresConfirmation) {
|
||||
$this->twoFactorEnabled = auth()->user()->hasEnabledTwoFactorAuthentication();
|
||||
}
|
||||
|
||||
$this->loadSetupData();
|
||||
|
||||
$this->showModal = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the two-factor authentication setup data for the user.
|
||||
*/
|
||||
private function loadSetupData(): void
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
try {
|
||||
$this->qrCodeSvg = $user?->twoFactorQrCodeSvg();
|
||||
$this->manualSetupKey = decrypt($user->two_factor_secret);
|
||||
} catch (Exception) {
|
||||
$this->addError('setupData', 'Failed to fetch setup data.');
|
||||
|
||||
$this->reset('qrCodeSvg', 'manualSetupKey');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the two-factor verification step if necessary.
|
||||
*/
|
||||
public function showVerificationIfNecessary(): void
|
||||
{
|
||||
if ($this->requiresConfirmation) {
|
||||
$this->showVerificationStep = true;
|
||||
|
||||
$this->resetErrorBag();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->closeModal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm two-factor authentication for the user.
|
||||
*/
|
||||
public function confirmTwoFactor(ConfirmTwoFactorAuthentication $confirmTwoFactorAuthentication): void
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
$confirmTwoFactorAuthentication(auth()->user(), $this->code);
|
||||
|
||||
$this->closeModal();
|
||||
|
||||
$this->twoFactorEnabled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset two-factor verification state.
|
||||
*/
|
||||
public function resetVerification(): void
|
||||
{
|
||||
$this->reset('code', 'showVerificationStep');
|
||||
|
||||
$this->resetErrorBag();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable two-factor authentication for the user.
|
||||
*/
|
||||
public function disable(DisableTwoFactorAuthentication $disableTwoFactorAuthentication): void
|
||||
{
|
||||
$disableTwoFactorAuthentication(auth()->user());
|
||||
|
||||
$this->twoFactorEnabled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the two-factor authentication modal.
|
||||
*/
|
||||
public function closeModal(): void
|
||||
{
|
||||
$this->reset(
|
||||
'code',
|
||||
'manualSetupKey',
|
||||
'qrCodeSvg',
|
||||
'showModal',
|
||||
'showVerificationStep',
|
||||
);
|
||||
|
||||
$this->resetErrorBag();
|
||||
|
||||
if (! $this->requiresConfirmation) {
|
||||
$this->twoFactorEnabled = auth()->user()->hasEnabledTwoFactorAuthentication();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current modal configuration state.
|
||||
*/
|
||||
#[Computed]
|
||||
public function modalConfig(): array
|
||||
{
|
||||
if ($this->twoFactorEnabled) {
|
||||
return [
|
||||
'title' => __('Two-factor authentication enabled'),
|
||||
'description' => __('Two-factor authentication is now enabled. Scan the QR code or enter the setup key in your authenticator app.'),
|
||||
'buttonText' => __('Close'),
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->showVerificationStep) {
|
||||
return [
|
||||
'title' => __('Verify authentication code'),
|
||||
'description' => __('Enter the 6-digit code from your authenticator app.'),
|
||||
'buttonText' => __('Continue'),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'title' => __('Enable two-factor authentication'),
|
||||
'description' => __('To finish enabling two-factor authentication, scan the QR code or enter the setup key in your authenticator app.'),
|
||||
'buttonText' => __('Continue'),
|
||||
];
|
||||
}
|
||||
}
|
||||
50
app/Livewire/Settings/TwoFactor/RecoveryCodes.php
Normal file
50
app/Livewire/Settings/TwoFactor/RecoveryCodes.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Settings\TwoFactor;
|
||||
|
||||
use Exception;
|
||||
use Laravel\Fortify\Actions\GenerateNewRecoveryCodes;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
class RecoveryCodes extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public array $recoveryCodes = [];
|
||||
|
||||
/**
|
||||
* Mount the component.
|
||||
*/
|
||||
public function mount(): void
|
||||
{
|
||||
$this->loadRecoveryCodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate new recovery codes for the user.
|
||||
*/
|
||||
public function regenerateRecoveryCodes(GenerateNewRecoveryCodes $generateNewRecoveryCodes): void
|
||||
{
|
||||
$generateNewRecoveryCodes(auth()->user());
|
||||
|
||||
$this->loadRecoveryCodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the recovery codes for the user.
|
||||
*/
|
||||
private function loadRecoveryCodes(): void
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
if ($user->hasEnabledTwoFactorAuthentication() && $user->two_factor_recovery_codes) {
|
||||
try {
|
||||
$this->recoveryCodes = json_decode(decrypt($user->two_factor_recovery_codes), true);
|
||||
} catch (Exception) {
|
||||
$this->addError('recoveryCodes', 'Failed to load recovery codes');
|
||||
|
||||
$this->recoveryCodes = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user