Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrignatushin committed Oct 26, 2024
2 parents 2b3bc00 + 2842793 commit 047bd07
Show file tree
Hide file tree
Showing 15 changed files with 2,100 additions and 56 deletions.
33 changes: 33 additions & 0 deletions app/DTO/Api/Auth/Request/UserAuthDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\DTO\Api\Auth\Request;

use Spatie\LaravelData\Support\Validation\ValidationContext;
use Spatie\LaravelData\Data;

class UserAuthDTO extends Data
{
public function __construct(
public string $email,
public string $password,
) {
}

public static function rules(ValidationContext $context): array
{
return [
'email' => [
'required',
'max:255',
'email',
],
'password' => [
'required',
'max:255',
'min:8',
],
];
}
}
50 changes: 50 additions & 0 deletions app/DTO/Api/Auth/Request/UserRegisterDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace App\DTO\Api\Auth\Request;

use Illuminate\Validation\Rule;
use Spatie\LaravelData\Support\Validation\ValidationContext;
use Spatie\LaravelData\Data;

class UserRegisterDTO extends Data
{
public function __construct(
public string $name,
public string $surname,
public string $email,
public string $password,
public string $patronymic,
) {
}

public static function rules(ValidationContext $context): array
{
return [
'name' => [
'required',
'max:255',
],
'surname' => [
'required',
'max:255',
],
'email' => [
'required',
'max:255',
'email',
Rule::unique('users', 'email'),
],
'password' => [
'required',
'max:255',
'min:8',
],
'patronymic' => [
'required',
'max:255',
]
];
}
}
25 changes: 25 additions & 0 deletions app/DTO/Api/Auth/Response/UserAuthShowDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\DTO\Api\Auth\Response;

use App\Models\User;
use Spatie\LaravelData\Data;

class UserAuthShowDTO extends Data
{
public function __construct(
public User $user,
public string $token,
) {
}

public static function fromMultiple(User $user, string $token): self
{
return new self(
$user,
$token
);
}
}
25 changes: 25 additions & 0 deletions app/DTO/Api/Auth/Response/UserRegisterShowDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\DTO\Api\Auth\Response;

use App\Models\User;
use Spatie\LaravelData\Data;

class UserRegisterShowDTO extends Data
{
public function __construct(
public User $user,
public string $token,
) {
}

public static function fromMultiple(User $user, string $token): self
{
return new self(
$user,
$token
);
}
}
31 changes: 31 additions & 0 deletions app/Http/Controllers/Api/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\DTO\Api\Auth\Request\UserAuthDTO;
use App\DTO\Api\Auth\Request\UserRegisterDTO;
use App\Models\User;
use App\Services\Api\AuthService;
use Illuminate\Http\JsonResponse;

class AuthController extends Controller
{
public function __construct(
protected AuthService $authService
) {
}

public function auth(UserAuthDTO $userAuthDTO): array|JsonResponse
{
$user = User::query()->where('email', $userAuthDTO->email)->firstOrFail();

return $this->authService->auth($user, $userAuthDTO);
}

public function register(UserRegisterDTO $userRegisterDTO): array
{
return $this->authService->register($userRegisterDTO);
}
}
15 changes: 15 additions & 0 deletions app/Http/Controllers/Api/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
use AuthorizesRequests;
use ValidatesRequests;
}
10 changes: 7 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

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

/**
* The attributes that are mass assignable.
Expand All @@ -24,13 +27,14 @@ class User extends Authenticatable
protected $fillable = [
'name',
'surname',
'patronymic',
'email',
'password',
'about',
'image_id',
'is_admin',
'is_confirmed'
'is_confirmed',
'is_admin',
'patronymic',
];

public function challenges(): BelongsToMany
Expand Down
35 changes: 35 additions & 0 deletions app/Services/Api/AuthService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Services\Api;

use App\DTO\Api\Auth\Request\UserAuthDTO;
use App\DTO\Api\Auth\Request\UserRegisterDTO;
use App\DTO\Api\Auth\Response\UserAuthShowDTO;
use App\DTO\Api\Auth\Response\UserRegisterShowDTO;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Hash;
use App\Models\User;

class AuthService
{
public function auth(User $user, UserAuthDTO $userAuthDTO): array|JsonResponse
{
if(Hash::check($userAuthDTO->password, $user->password)) {
$token = $user->createToken('token')->plainTextToken;

return UserAuthShowDTO::from($user, $token)->toArray();
}

return response()->json(['message' => 'bad creds'], 403);
}

public function register(UserRegisterDTO $userRegisterDTO): array
{
$user = User::query()->create($userRegisterDTO->toArray());
$token = $user->createToken('token')->plainTextToken;

return UserRegisterShowDTO::from($user, $token)->toArray();
}
}
1 change: 1 addition & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"php": "^8.2",
"filament/filament": "^3.2",
"laravel/framework": "^11.9",
"laravel/tinker": "^2.9"
"laravel/sanctum": "^4.0",
"laravel/tinker": "^2.9",
"spatie/laravel-data": "^4.11"
},
"require-dev": {
"fakerphp/faker": "^1.23",
Expand Down
Loading

0 comments on commit 047bd07

Please sign in to comment.