SyntaxSquad/DADProject#10 feat: Account deletion (soft delete)

This commit is contained in:
AfonsoCMSousa
2025-12-23 21:39:38 +00:00
parent c34d74db46
commit 30705fd194
3 changed files with 173 additions and 20 deletions
+12 -18
View File
@@ -2,7 +2,6 @@
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
@@ -11,30 +10,27 @@ use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable, HasApiTokens, SoftDeletes;
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var list<string>
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'nickname',
'photo_avatar_filename',
'type',
'blocked',
'photo_avatar_filename',
'coins_balance',
'custom',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
* @var array<int, string>
*/
protected $hidden = [
'password',
@@ -42,16 +38,14 @@ class User extends Authenticatable
];
/**
* Get the attributes that should be cast.
* The attributes that should be cast.
*
* @return array<string, string>
* @var array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'blocked' => 'boolean',
];
}
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'blocked' => 'boolean',
'deleted_at' => 'datetime',
];
}
+15 -1
View File
@@ -1,5 +1,4 @@
<?php
use App\Http\Controllers\AuthController;
use App\Http\Controllers\GameController;
use App\Http\Controllers\MatchController;
@@ -65,10 +64,12 @@ Route::middleware('auth:sanctum')->group(function () {
$user = $request->user();
// Delete old avatar if exists
if ($user->photo_avatar_filename) {
\Storage::disk('public')->delete('photos_avatars/' . $user->photo_avatar_filename);
}
// Store new avatar
$file = $request->file('avatar');
$filename = str_pad($user->id, 5, '0', STR_PAD_LEFT) . '_' . \Str::random(10) . '.' . $file->extension();
$file->storeAs('photos_avatars', $filename, 'public');
@@ -79,6 +80,19 @@ Route::middleware('auth:sanctum')->group(function () {
return response()->json($user);
});
// Delete account
Route::delete('/me', function (Request $request) {
$user = $request->user();
$user->tokens()->delete();
$user->delete();
return response()->json([
'message' => 'Account deleted successfully'
]);
});
Route::get('/{user}/matches', [UserController::class, 'getMatches']);
});