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
9 changes: 9 additions & 0 deletions app/Http/Requests/Profile/UpdateProfileRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateProfileRequest extends FormRequest
{
Expand All @@ -24,6 +25,14 @@ public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'username' => [
'sometimes',
'string',
'min:3',
'max:30',
'regex:/^[a-zA-Z0-9_]+$/',
Rule::unique('users', 'username')->ignore($this->user()->id),
],
'file' => ['sometimes', 'nullable', 'image', 'max:2048'],
'about' => ['sometimes', 'nullable', 'string'],
'institution' => ['sometimes', 'nullable', 'string', 'max:255'],
Expand Down
10 changes: 10 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class User extends Authenticatable

protected $fillable = [
'name',
'username',
'email',
'receive_emails',
'google_id',
Expand All @@ -50,6 +51,15 @@ class User extends Authenticatable
'instagram',
'github',
];
protected static function booted(): void
{
static::created(function (User $user) {
if (! $user->username) {
$user->updateQuietly(['username' => "student_{$user->id}"]);
$user->username = "student_{$user->id}";
}
});
}

protected $appends = [
'image_url',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('username')->nullable()->unique()->after('name');
});

User::whereNull('username')->each(function ($user) {
$user->updateQuietly(['username' => "student_{$user->id}"]);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('username');
});
}
};
42 changes: 42 additions & 0 deletions resources/js/pages/Profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const showConfirmModal = ref(false);
const form = useForm({
_method: 'PUT',
name: user.value?.name || '',
username: user.value?.username || '',
file: null as File | null,
about: user.value?.about || '',
institution: user.value?.institution || '',
Expand Down Expand Up @@ -178,6 +179,47 @@ const submitForm = () => {
</p>
</div>

<div>
<label
for="username"
class="mb-1.5 block text-xs font-semibold text-slate-700 dark:text-gray-300"
>
Username
</label>
<div class="relative">
<span
class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3.5 text-sm font-semibold text-slate-400 dark:text-gray-500"
>
@
</span>
<input
v-model="form.username"
type="text"
id="username"
required
placeholder="your_handle"
:disabled="form.processing"
class="w-full rounded-lg border border-slate-300 bg-white py-2.5 pr-3.5 pl-8 text-sm text-slate-900 transition outline-none placeholder:text-slate-400 focus:border-blue-500 focus:ring-2 focus:ring-blue-500/20 disabled:bg-slate-50 dark:border-gray-600 dark:bg-gray-950 dark:text-gray-100 dark:placeholder:text-gray-500"
:class="{
'border-rose-500 focus:ring-rose-500/20':
form.errors.username,
}"
/>
</div>
<p
v-if="form.errors.username"
class="mt-1 text-xs text-rose-600"
>
{{ form.errors.username }}
</p>
<p
v-else
class="mt-1 text-[11px] text-slate-400 dark:text-gray-500"
>
Letters, numbers, and underscores (3–30 chars).
</p>
</div>

<div>
<label
for="email"
Expand Down
59 changes: 59 additions & 0 deletions tests/Feature/ProfileUsernameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use App\Models\User;

test('new user automatically gets default student_{id} username on creation', function () {
$user = User::create([
'name' => 'Tajim Ahmed',
'email' => 'tajim@example.com',
]);

expect($user->username)->toBe("student_{$user->id}");
});

test('authenticated user can update their username in profile', function () {
$user = User::factory()->create([
'username' => 'student_1',
]);

$response = $this->actingAs($user)->put('/profile', [
'name' => $user->name,
'username' => 'custom_tajim',
]);

$response->assertRedirect(route('profile.edit'));
expect($user->fresh()->username)->toBe('custom_tajim');
});

test('user cannot update username to one already taken by someone else', function () {
User::factory()->create([
'username' => 'existing_user',
]);

$user = User::factory()->create([
'username' => 'my_user',
]);

$response = $this->actingAs($user)->put('/profile', [
'name' => $user->name,
'username' => 'existing_user',
]);

$response->assertSessionHasErrors(['username']);
expect($user->fresh()->username)->toBe('my_user');
});

test('user can submit profile without changing their existing username', function () {
$user = User::factory()->create([
'username' => 'my_handle',
]);

$response = $this->actingAs($user)->put('/profile', [
'name' => 'Updated Name',
'username' => 'my_handle',
]);

$response->assertRedirect(route('profile.edit'));
expect($user->fresh()->name)->toBe('Updated Name');
expect($user->fresh()->username)->toBe('my_handle');
});