Compare commits
11 Commits
feature/Au
...
feature/Au
| Author | SHA1 | Date | |
|---|---|---|---|
| f072bd1ee8 | |||
| f01992368e | |||
| b32ee444ca | |||
| cee480fff5 | |||
| 5024a242d2 | |||
| 0bba710b7d | |||
| f788f9a7ea | |||
| e74619d0ab | |||
| 5b42e23afc | |||
| 51aa3b157c | |||
| b24b61c754 |
19
backend/app/Models/User.php
Normal file → Executable file
19
backend/app/Models/User.php
Normal file → Executable file
@@ -5,29 +5,18 @@ namespace App\Models;
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Passport\HasApiTokens;
|
||||
|
||||
#[Fillable(['name', 'email', 'password'])]
|
||||
#[Hidden(['password', 'remember_token'])]
|
||||
#[Guarded(['id'])]
|
||||
#[Hidden(['password'])]
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable, HasApiTokens;
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
protected $guarded = ['id'];
|
||||
}
|
||||
|
||||
2
backend/app/Providers/AppServiceProvider.php
Normal file → Executable file
2
backend/app/Providers/AppServiceProvider.php
Normal file → Executable file
@@ -4,6 +4,7 @@ namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Laravel\Passport\Passport;
|
||||
use Carbon\CarbonInterval;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -21,5 +22,6 @@ class AppServiceProvider extends ServiceProvider
|
||||
public function boot(): void
|
||||
{
|
||||
Passport::enablePasswordGrant();
|
||||
Passport::personalAccessTokensExpireIn(CarbonInterval::minutes(1));
|
||||
}
|
||||
}
|
||||
|
||||
31
backend/app/Traits/ApiResponse.php
Executable file
31
backend/app/Traits/ApiResponse.php
Executable file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
trait ApiResponse
|
||||
{
|
||||
public function successResponse(mixed $data = null, string $message = null, int $statusCode = 200): JsonResponse
|
||||
{
|
||||
if (!is_null($data)) {
|
||||
return response()->json([
|
||||
'data' => $data
|
||||
], $statusCode);
|
||||
}
|
||||
|
||||
$message = $message ?? __('messages.successful');
|
||||
|
||||
return response()->json([
|
||||
'message' => $message
|
||||
], $statusCode);
|
||||
}
|
||||
|
||||
public function errorResponse(string $message, string $type = 'logical_exception', int $statusCode = 422): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'type' => $type,
|
||||
'message' => $message
|
||||
], $statusCode);
|
||||
}
|
||||
}
|
||||
65
backend/app/User/Controller/Auth/AuthController.php
Executable file
65
backend/app/User/Controller/Auth/AuthController.php
Executable file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Controller\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Traits\ApiResponse;
|
||||
use App\User\Requests\Auth\LoginRequest;
|
||||
use App\User\Requests\Auth\RegisterRequest;
|
||||
use App\User\Resources\UserResource;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
use ApiResponse;
|
||||
|
||||
public function register(RegisterRequest $request): JsonResponse
|
||||
{
|
||||
User::query()->create([
|
||||
'username' => $request->username,
|
||||
'email' => $request->email,
|
||||
'password' => Hash::make($request->password),
|
||||
]);
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
||||
return $this->successResponse();
|
||||
}
|
||||
|
||||
public function login(LoginRequest $request): JsonResponse
|
||||
{
|
||||
$credentials = ['password' => $request->password,];
|
||||
|
||||
if (filter_var($request->login, FILTER_VALIDATE_EMAIL)) {
|
||||
$credentials['email'] = $request->login;
|
||||
} else {
|
||||
$credentials['username'] = $request->login;
|
||||
}
|
||||
|
||||
if (Auth::attempt($credentials))
|
||||
{
|
||||
$request->session()->regenerate();
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
return $this->successResponse(new UserResource($user));
|
||||
}
|
||||
|
||||
return $this->errorResponse(__('messages.incorrect_or_username_password'));
|
||||
}
|
||||
|
||||
public function logout(Request $request): JsonResponse
|
||||
{
|
||||
Auth::logout();
|
||||
|
||||
$request->session()->invalidate();
|
||||
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
return $this->successResponse();
|
||||
}
|
||||
}
|
||||
56
backend/app/User/Controller/ProfileController.php
Normal file
56
backend/app/User/Controller/ProfileController.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Controller;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Traits\ApiResponse;
|
||||
use App\User\Requests\Profile\ChangePasswordRequest;
|
||||
use App\User\Requests\Profile\EditRequest;
|
||||
use App\User\Resources\UserResource;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class ProfileController extends Controller
|
||||
{
|
||||
use ApiResponse;
|
||||
|
||||
public function info(): JsonResponse
|
||||
{
|
||||
return $this->successResponse(new UserResource(Auth::user()));
|
||||
}
|
||||
|
||||
public function edit(EditRequest $request): JsonResponse
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$user->update([
|
||||
'first_name' => $request->first_name,
|
||||
'last_name' => $request->last_name,
|
||||
'national_id' => $request->national_id,
|
||||
'address' => $request->address,
|
||||
'postal_code' => $request->postal_code,
|
||||
'account_number' => $request->account_number,
|
||||
'phone_number' => $request->phone_number,
|
||||
'province_id' => $request->province_id,
|
||||
'city_id' => $request->city_id,
|
||||
]);
|
||||
|
||||
return $this->successResponse();
|
||||
}
|
||||
|
||||
public function changePassword(ChangePasswordRequest $request): JsonResponse
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if (! Hash::check($request->current_password, $user->password)) {
|
||||
return $this->errorResponse(__('messages.incorrect_current_password'));
|
||||
}
|
||||
|
||||
$user->update([
|
||||
'password' => Hash::make($request->new_password),
|
||||
]);
|
||||
|
||||
return $this->successResponse();
|
||||
}
|
||||
}
|
||||
29
backend/app/User/Requests/Auth/LoginRequest.php
Executable file
29
backend/app/User/Requests/Auth/LoginRequest.php
Executable file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Requests\Auth;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class LoginRequest 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, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'login' => 'required|string',
|
||||
'password' => 'required|string',
|
||||
];
|
||||
}
|
||||
}
|
||||
30
backend/app/User/Requests/Auth/RegisterRequest.php
Executable file
30
backend/app/User/Requests/Auth/RegisterRequest.php
Executable file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Requests\Auth;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class RegisterRequest 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, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'username' => 'required|unique:users,username',
|
||||
'email' => 'required|email|unique:users,email',
|
||||
'password' => 'required|string|regex:/^(?=.*[a-zA-Z])(?=.*\d).+$/|min:6|confirmed:',
|
||||
];
|
||||
}
|
||||
}
|
||||
30
backend/app/User/Requests/Profile/ChangePasswordRequest.php
Normal file
30
backend/app/User/Requests/Profile/ChangePasswordRequest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Requests\Profile;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ChangePasswordRequest 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, ValidationRule|array|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'current_password' => 'required|string|regex:/^(?=.*[a-zA-Z])(?=.*\d).+$/|min:6',
|
||||
'new_password' => 'required|string|regex:/^(?=.*[a-zA-Z])(?=.*\d).+$/|min:6',
|
||||
];
|
||||
}
|
||||
}
|
||||
37
backend/app/User/Requests/Profile/EditRequest.php
Normal file
37
backend/app/User/Requests/Profile/EditRequest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Requests\Profile;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class EditRequest 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, ValidationRule|array|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'first_name' => 'required|string',
|
||||
'last_name' => 'required|string',
|
||||
'national_id' => 'required',
|
||||
'address' => 'required',
|
||||
'postal_code' => 'required',
|
||||
'account_number' => 'required',
|
||||
'phone_number' => 'required|regex:/^964\d{9}$/|numeric|digits:11',
|
||||
'province_id' => 'required',
|
||||
'city_id' => 'required',
|
||||
];
|
||||
}
|
||||
}
|
||||
23
backend/app/User/Resources/UserResource.php
Executable file
23
backend/app/User/Resources/UserResource.php
Executable file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\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 [
|
||||
'id' => $this->id,
|
||||
'username' => $this->username,
|
||||
'email' => $this->email,
|
||||
];
|
||||
}
|
||||
}
|
||||
2
backend/bootstrap/app.php
Normal file → Executable file
2
backend/bootstrap/app.php
Normal file → Executable file
@@ -12,7 +12,7 @@ return Application::configure(basePath: dirname(__DIR__))
|
||||
health: '/up',
|
||||
)
|
||||
->withMiddleware(function (Middleware $middleware): void {
|
||||
//
|
||||
$middleware->web()->validateCsrfTokens(['*']);
|
||||
})
|
||||
->withExceptions(function (Exceptions $exceptions): void {
|
||||
//
|
||||
|
||||
0
backend/config/auth.php
Normal file → Executable file
0
backend/config/auth.php
Normal file → Executable file
34
backend/config/cors.php
Executable file
34
backend/config/cors.php
Executable file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Cross-Origin Resource Sharing (CORS) Configuration
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here you may configure your settings for cross-origin resource sharing
|
||||
| or "CORS". This determines what cross-origin operations may execute
|
||||
| in web browsers. You are free to adjust these settings as needed.
|
||||
|
|
||||
| To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
||||
|
|
||||
*/
|
||||
|
||||
'paths' => ['*'],
|
||||
|
||||
'allowed_methods' => ['*'],
|
||||
|
||||
'allowed_origins' => ['*'],
|
||||
|
||||
'allowed_origins_patterns' => [],
|
||||
|
||||
'allowed_headers' => ['*'],
|
||||
|
||||
'exposed_headers' => [],
|
||||
|
||||
'max_age' => 0,
|
||||
|
||||
'supports_credentials' => true,
|
||||
|
||||
];
|
||||
0
backend/config/session.php
Normal file → Executable file
0
backend/config/session.php
Normal file → Executable file
2
backend/database/factories/UserFactory.php
Normal file → Executable file
2
backend/database/factories/UserFactory.php
Normal file → Executable file
@@ -25,7 +25,7 @@ class UserFactory extends Factory
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'name' => fake()->name(),
|
||||
'username' => fake()->name(),
|
||||
'email' => fake()->unique()->safeEmail(),
|
||||
'email_verified_at' => now(),
|
||||
'password' => static::$password ??= Hash::make('password'),
|
||||
|
||||
24
backend/database/migrations/0001_01_01_000000_create_users_table.php
Normal file → Executable file
24
backend/database/migrations/0001_01_01_000000_create_users_table.php
Normal file → Executable file
@@ -13,18 +13,23 @@ return new class extends Migration
|
||||
{
|
||||
Schema::create('users', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('username');
|
||||
$table->string('first_name')->nullable();
|
||||
$table->string('last_name')->nullable();
|
||||
$table->string('email')->unique();
|
||||
$table->timestamp('email_verified_at')->nullable();
|
||||
$table->string('password');
|
||||
$table->rememberToken();
|
||||
$table->timestamps();
|
||||
});
|
||||
$table->tinyInteger('status')->default(0);
|
||||
$table->string('national_id')->nullable();
|
||||
$table->string('address')->nullable();
|
||||
$table->string('postal_code')->nullable();
|
||||
$table->string('account_number')->nullable();
|
||||
$table->string('phone_number')->nullable();
|
||||
$table->string('province_id')->nullable();
|
||||
$table->string('province_name')->nullable();
|
||||
$table->string('city_id')->nullable();
|
||||
$table->string('city_name')->nullable();
|
||||
|
||||
Schema::create('password_reset_tokens', function (Blueprint $table) {
|
||||
$table->string('email')->primary();
|
||||
$table->string('token');
|
||||
$table->timestamp('created_at')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
Schema::create('sessions', function (Blueprint $table) {
|
||||
@@ -43,7 +48,6 @@ return new class extends Migration
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('users');
|
||||
Schema::dropIfExists('password_reset_tokens');
|
||||
Schema::dropIfExists('sessions');
|
||||
}
|
||||
};
|
||||
|
||||
3
backend/database/seeders/DatabaseSeeder.php
Normal file → Executable file
3
backend/database/seeders/DatabaseSeeder.php
Normal file → Executable file
@@ -18,8 +18,9 @@ class DatabaseSeeder extends Seeder
|
||||
// User::factory(10)->create();
|
||||
|
||||
User::factory()->create([
|
||||
'name' => 'Test User',
|
||||
'username' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
'password' => 'password123@',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
2
backend/routes/api.php
Normal file → Executable file
2
backend/routes/api.php
Normal file → Executable file
@@ -1,8 +1,10 @@
|
||||
<?php
|
||||
|
||||
use App\User\Controller\Auth\AuthController;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::get('/user', function (Request $request) {
|
||||
return $request->user();
|
||||
})->middleware('auth:api');
|
||||
|
||||
|
||||
23
backend/routes/web.php
Normal file → Executable file
23
backend/routes/web.php
Normal file → Executable file
@@ -1,7 +1,24 @@
|
||||
<?php
|
||||
|
||||
use App\User\Controller\Auth\AuthController;
|
||||
use App\User\Controller\ProfileController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::get('/', function () {
|
||||
return view('welcome');
|
||||
});
|
||||
Route::prefix('auth')
|
||||
->name('Auth.')
|
||||
->controller(AuthController::class)
|
||||
->group(function () {
|
||||
Route::post('register', 'register')->name('register');
|
||||
Route::post('login', 'login')->name('login');
|
||||
Route::post('logout', 'logout')->name('logout');
|
||||
});
|
||||
|
||||
Route::prefix('profile')
|
||||
->name('profile.')
|
||||
->middleware('auth')
|
||||
->controller(ProfileController::class)
|
||||
->group(function () {
|
||||
Route::get('info', 'info')->name('info');
|
||||
Route::post('edit', 'edit')->name('edit');
|
||||
Route::post('change_password', 'changePassword')->name('changePassword');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user