Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add endpoints for fetching and updating user data, add validati… #6

Merged
merged 8 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 23 additions & 1 deletion app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace App\Http\Controllers;

use App\Http\Requests\UserUpdateRequest;
use App\Http\Resources\UserResource;
use App\Http\Requests\LoginRequest;
use App\Models\User;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Log;
use Symfony\Component\HttpFoundation\Response;

class AuthController extends Controller
Expand Down Expand Up @@ -67,4 +69,24 @@ public function login(LoginRequest $request): JsonResponse
'message' => 'Login successful',
], Response::HTTP_OK);
}

public function user(): UserResource
{
return new UserResource(auth()->user());
}

public function update(UserUpdateRequest $request): JsonResponse
{
$data = $request->validated();
$user = auth()->user();

try {
$user->update($data);
}
catch (Exception $e)
{
return response()->json(['message' => "Failed to update user's profile!"], Response::HTTP_INTERNAL_SERVER_ERROR);
}
return response()->json(['success' => true], Response::HTTP_OK);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Użycie struktury stworzonej w Course

}
}
42 changes: 42 additions & 0 deletions app/Http/Requests/UserUpdateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserUpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/',
'last_name' => 'string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/',
'email' => 'email|max:255',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dodaj tutaj również unique

'phone_number' => 'string|regex:/^\+?\d{9,15}$/',
'voivodship' => 'string|min:1|max:30',
'city' => 'string|min:1|max:30',
'zip_code' => 'string|regex:/^\d{2}-\d{3}$/',
'street' => 'string|min:1|max:30',
'house_number' => 'string|regex:/^\d+[a-zA-Z]?$/',

'card_first_name' => 'string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/',
'card_last_name' => 'string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/',
'card_number' => 'string|regex:/^\d{16}$/',
'card_expiry_date' => 'string|regex:/^\d{2}\/\d{2}$/',
'card_cvv' => 'string|regex:/^\d{3,4}$/',
];
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return parent::toArray($request);
}
}
14 changes: 14 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,22 @@ class User extends Authenticatable
*/
protected $fillable = [
'name',
'last_name',
'email',
'password',
'phone_number',
'voivodship',
'city',
'zip_code',
'street',
'house_number',

'card_first_name',
'card_last_name',
'card_number',
'card_expiry_date',
'card_cvv',

];

/**
Expand Down
15 changes: 15 additions & 0 deletions database/migrations/0001_01_01_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,24 @@ public function up(): void
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('last_name')->nullable();
$table->string('email')->unique();
$table->string('role')->nullable();
$table->string('phone_number')->nullable();
$table->string('voivodship')->nullable();
$table->string('city')->nullable();
$table->string('zip_code')->nullable();
$table->string('street')->nullable();
$table->string('house_number')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');

$table->string('card_first_name')->nullable();
$table->string('card_last_name')->nullable();
$table->string('card_number')->nullable();
$table->string('card_expiry_date')->nullable();
$table->string('card_cvv')->nullable();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Przenieść do oddzielenej tabeli z relacją 1:1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zadbać o odpowiednie przechowywanie danych, ich zwracanie oraz szyfrowanie


$table->rememberToken();
$table->timestamps();
});
Expand Down
8 changes: 8 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::middleware('auth:sanctum')->group(function () {
Route::controller(AuthController::class)->group(function () {
Route::get('user', 'user');
Route::put('user', 'update');
});
});
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum');
Expand All @@ -17,3 +23,5 @@

Route::get('/google/redirect', [GoogleAuthController::class, 'redirectToGoogle']);
Route::get('/google/callback', [GoogleAuthController::class, 'handleGoogleCallback']);


Loading