-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/dev' into dev
# Conflicts: # app/Models/User.php
- Loading branch information
Showing
20 changed files
with
2,216 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.