forked from eatvio/eatvio-infra
66 lines
2.0 KiB
PHP
Executable File
66 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Mobile;
|
|
|
|
use App\Http\Requests\Auth\RegisterRequest;
|
|
use App\Services\Auth\RegisterService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Laravel\Fortify\Http\Requests\LoginRequest;
|
|
|
|
class MobileRegisterController
|
|
{
|
|
private RegisterService $service;
|
|
|
|
public function __construct(RegisterService $service)
|
|
{
|
|
$this->service = $service;
|
|
}
|
|
|
|
public function register(RegisterRequest $request): JsonResponse
|
|
{
|
|
$this->service->registerCommand($request);
|
|
|
|
if (! $token = $this->attemptLoginAndCreateToken($request)) {
|
|
return response()->json(
|
|
['error' => 'Упс! Произошла ошибка при регистрации. Проверьте введенные данные'],
|
|
Response::HTTP_UNPROCESSABLE_ENTITY
|
|
);
|
|
}
|
|
|
|
return response()->json(['token' => $token], Response::HTTP_OK);
|
|
|
|
// $this->login($request->only('email', 'password'));
|
|
}
|
|
|
|
public function login(LoginRequest $request): JsonResponse
|
|
{
|
|
if (! $token = $this->attemptLoginAndCreateToken($request)) {
|
|
return response()->json(
|
|
['error' => 'Упс! Произошла ошибка при аутентификации. Проверьте введенные данные'],
|
|
Response::HTTP_UNAUTHORIZED
|
|
);
|
|
}
|
|
|
|
return response()->json(['token' => $token], Response::HTTP_OK);
|
|
}
|
|
|
|
private function attemptLoginAndCreateToken(RegisterRequest|LoginRequest $request): ?string
|
|
{
|
|
if (Auth::attempt($request->only(['email', 'password']))) {
|
|
return $request->user()->createToken('mobile')->plainTextToken;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// public function logout(Request $request)
|
|
// {
|
|
// auth()->user()->tokens()->delete();
|
|
// return [
|
|
// 'message' => 'Logged out'
|
|
// ];
|
|
// }
|
|
}
|