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
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 Alaa Nabawii

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ seed: ## Run database seeders

test: ## Run tests
docker compose -f compose.dev.yaml exec workspace php artisan test
@echo "🔄 Ensuring Passport setup is intact after tests..."
docker compose -f compose.dev.yaml exec workspace ./scripts/setup-passport.sh

test-coverage: ## Run tests with coverage
docker compose -f compose.dev.yaml exec workspace php artisan test --coverage
@echo "🔄 Ensuring Passport setup is intact after tests..."
docker compose -f compose.dev.yaml exec workspace ./scripts/setup-passport.sh

# Frontend Commands
npm: ## Run npm command (usage: make npm cmd="install")
Expand Down
11 changes: 7 additions & 4 deletions app/Console/Commands/MakeStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ protected function generateService(string $service, string $repository): void
$servicePath = app_path('Services/' . $service . '.php');

if (! file_exists($servicePath)) {
// Extract model name from service name (remove 'Service' suffix)
$modelName = str_replace('Service', '', $service);

$serviceTemplate = str_replace(
['{{ServiceName}}', '{{RepositoryName}}', '{{repositoryName}}'],
[$service, $repository, lcfirst($repository)],
['{{model}}', '{{className}}', '{{method}}', '{{lowerMethod}}', '{{lowerModel}}'],
[$modelName, $service, 'Handle', 'handle', strtolower($modelName)],
file_get_contents(base_path('stubs/service.stub'))
);

Expand All @@ -114,8 +117,8 @@ protected function generateRepository(string $repository, string $mainModel): vo

if (! file_exists($repositoryPath)) {
$repositoryTemplate = str_replace(
['{{RepositoryName}}', '{{MainModelClass}}'],
[$repository, $mainModel],
['{{model}}'],
[$mainModel],
file_get_contents($stubPath)
);

Expand Down
73 changes: 73 additions & 0 deletions app/Http/Controllers/Api/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\Api\LoginRequest;
use App\Http\Requests\Auth\Api\SignupRequest;
use App\Http\Resources\Auth\Api\UserResource;
use App\Services\Auth\Api\LoginService;
use App\Services\Auth\Api\SignupService;
use App\Traits\ApiResponseTrait;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class AuthController extends Controller
{
use ApiResponseTrait;

/**
* User login.
*
* @throws ValidationException
*/
public function login(LoginRequest $request, LoginService $loginService): JsonResponse
{
try {
$result = $loginService->login($request->validated());

return $this->successResponse($result, 'Login successful');
} catch (ValidationException $e) {
return $this->validationErrorResponse($e);
}
}

/**
* User registration.
*
* @throws ValidationException
*/
public function signup(SignupRequest $request, SignupService $signupService): JsonResponse
{
try {
$result = $signupService->signup($request->validated());

return $this->createdResponse($result, 'Registration successful');
} catch (ValidationException $e) {
return $this->validationErrorResponse($e);
}
}

/**
* User logout.
*/
public function logout(Request $request, LoginService $loginService): JsonResponse
{
$user = $request->user();

if ($user) {
$loginService->logout($user);
}

return $this->successResponse(null, 'Logout successful');
}

/**
* Get authenticated user.
*/
public function me(Request $request): JsonResponse
{
return $this->successResponse(new UserResource($request->user()), 'User data retrieved successfully');
}
}
Empty file.
43 changes: 43 additions & 0 deletions app/Http/Requests/Auth/Api/LoginRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Http\Requests\Auth\Api;

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, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'email' => 'required|email',
'password' => 'required|string', // pragma: allowlist secret
];
}

/**
* Get custom error messages for validation rules.
*
* @return array<string, string>
*/
public function messages(): array
{
return [
'email.required' => 'Email is required',
'email.email' => 'Please provide a valid email address',
'password.required' => 'Password is required',
];
}
}
51 changes: 51 additions & 0 deletions app/Http/Requests/Auth/Api/SignupRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Http\Requests\Auth\Api;

use Illuminate\Foundation\Http\FormRequest;

class SignupRequest 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' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|min:8|confirmed', // pragma: allowlist secret
'password_confirmation' => 'required|string', // pragma: allowlist secret
];
}

/**
* Get custom error messages for validation rules.
*
* @return array<string, string>
*/
public function messages(): array
{
return [
'name.required' => 'Name is required',
'name.max' => 'Name cannot exceed 255 characters',
'email.required' => 'Email is required',
'email.email' => 'Please provide a valid email address',
'email.unique' => 'This email is already registered',
'password.required' => 'Password is required',
'password.min' => 'Password must be at least 8 characters',
'password.confirmed' => 'Password confirmation does not match',
'password_confirmation.required' => 'Password confirmation is required',
];
}
}
29 changes: 29 additions & 0 deletions app/Http/Resources/Auth/Api/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Resources\Auth\Api;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @mixin User
*/
class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->resource->id,
'name' => $this->resource->name,
'email' => $this->resource->email,
'created_at' => $this->resource->created_at->toISOString(),
'updated_at' => $this->resource->updated_at->toISOString(),
];
}
}
37 changes: 37 additions & 0 deletions app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Resources;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @mixin User
*/
class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$data = [
'id' => $this->resource->id,
'name' => $this->resource->name,
'email' => $this->resource->email,
'email_verified_at' => $this->resource->email_verified_at?->toISOString(),
'created_at' => $this->resource->created_at->toISOString(),
'updated_at' => $this->resource->updated_at->toISOString(),
];

// Conditionally include sensitive data
if ($request->user()?->id === $this->resource->id) {
$data['email_verified'] = ! is_null($this->resource->email_verified_at);
}

return $data;
}
}
71 changes: 71 additions & 0 deletions app/Models/Passport/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Models\Passport;

use Laravel\Passport\Client as PassportClient;

class Client extends PassportClient
{
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'redirect_uris' => 'array',
'grant_types' => 'array',
'revoked' => 'boolean',
];

/**
* Get the redirect URIs for the client.
*
* @return array<string>
*/
public function getRedirectAttribute(mixed $value): array
{
if (is_string($value)) {
return json_decode($value, true) ?: [$value];
}

return $value ?: [];
}

/**
* Set the redirect URIs for the client.
*/
public function setRedirectAttribute(mixed $value): void
{
if (is_array($value)) {
$this->attributes['redirect'] = json_encode($value);
} else {
$this->attributes['redirect'] = json_encode([$value]);
}
}

/**
* Get the grant types for the client.
*
* @return array<string>
*/
public function getGrantTypesAttribute(mixed $value): array
{
if (is_string($value)) {
return json_decode($value, true) ?: [$value];
}

return $value ?: [];
}

/**
* Set the grant types for the client.
*/
public function setGrantTypesAttribute(mixed $value): void
{
if (is_array($value)) {
$this->attributes['grant_types'] = json_encode($value);
} else {
$this->attributes['grant_types'] = json_encode([$value]);
}
}
}
8 changes: 5 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\Contracts\OAuthenticatable;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
class User extends Authenticatable implements OAuthenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
use HasApiTokens, HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -42,7 +44,7 @@ protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'password' => 'hashed', // pragma: allowlist secret
];
}
}
Loading
Loading