Skip to content

Commit b77d01c

Browse files
committed
Sanitize username input by replacing invalid characters with underscores
1 parent 391b8f0 commit b77d01c

4 files changed

Lines changed: 18 additions & 0 deletions

File tree

app/Livewire/Profile.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public function updateProfileInformation(): void
102102
{
103103
$validated = $this->validate($this->profileRules($this->user->id));
104104

105+
$validated['username'] = preg_replace('/[^a-zA-Z0-9]/', '_', $validated['username']);
106+
105107
$this->user->fill($validated);
106108

107109
if ($this->user->isDirty('email')) {

app/Livewire/Users/Profile.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public function updateProfileInformation(): void
100100
{
101101
$validated = $this->validate($this->profileRules($this->user->id));
102102

103+
$validated['username'] = preg_replace('/[^a-zA-Z0-9]/', '_', $validated['username']);
104+
103105
$this->user->fill($validated);
104106

105107
if ($this->user->isDirty('email')) {

resources/views/livewire/profile.blade.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class="cursor-pointer hover:underline"
5757
label="Username"
5858
type="text"
5959
wire:model="username"
60+
x-on:input="username"
6061
/>
6162

6263
<x-input-text
@@ -163,3 +164,9 @@ class="xs:max-w-30 ml-auto w-full text-xs/3"
163164
</x-form>
164165
</div>
165166
</x-contents>
167+
168+
<script lang="js">
169+
const username = (event) => {
170+
event.target.value = event.target.value.replaceAll(/[^a-zA-Z0-9_]/g, "_").toLowerCase()
171+
}
172+
</script>

resources/views/livewire/users/profile.blade.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class="size-24 rounded-full"
2929
label="Username"
3030
type="text"
3131
wire:model="username"
32+
x-on:input="username"
3233
/>
3334

3435
<x-input-text
@@ -140,3 +141,9 @@ class="xs:max-w-30 ml-auto w-full text-xs/3"
140141
</x-button>
141142
</div>
142143
</x-contents>
144+
145+
<script lang="js">
146+
const username = (event) => {
147+
event.target.value = event.target.value.replaceAll(/[^a-zA-Z0-9_]/g, "_").toLowerCase()
148+
}
149+
</script>

0 commit comments

Comments
 (0)