2023-10-02 23:10:49 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Lucent\Http\Controller;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2023-10-04 13:32:30 +03:00
|
|
|
use Illuminate\Contracts\Session\Session;
|
2023-10-02 23:10:49 +03:00
|
|
|
use Illuminate\Contracts\View\View;
|
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\Response;
|
2023-10-04 13:32:30 +03:00
|
|
|
use Lucent\Account\AccountService;
|
2023-10-02 23:10:49 +03:00
|
|
|
use Lucent\Account\AuthService;
|
2023-10-04 13:32:30 +03:00
|
|
|
use Lucent\Channel\ChannelService;
|
2023-10-02 23:10:49 +03:00
|
|
|
use Lucent\LucentException;
|
|
|
|
|
use Lucent\Svelte\Svelte;
|
|
|
|
|
use function Lucent\Response\fail;
|
|
|
|
|
use function Lucent\Response\ok;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AuthController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
2023-10-04 13:32:30 +03:00
|
|
|
private readonly AuthService $authService,
|
|
|
|
|
private readonly AccountService $accountService,
|
|
|
|
|
private readonly ChannelService $channelService,
|
|
|
|
|
private readonly Session $session,
|
|
|
|
|
private readonly Svelte $svelte,
|
2023-10-02 23:10:49 +03:00
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 13:32:30 +03:00
|
|
|
public function register(Request $request): View|RedirectResponse
|
2023-10-02 23:10:49 +03:00
|
|
|
{
|
2023-10-04 13:32:30 +03:00
|
|
|
if ($this->accountService->countUsers() > 0) {
|
|
|
|
|
return redirect($this->channelService->channel->lucentUrl . "/login");
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
|
2023-10-04 13:32:30 +03:00
|
|
|
return $this->svelte->render(
|
2023-10-02 23:10:49 +03:00
|
|
|
layout: "account",
|
|
|
|
|
view: "register",
|
|
|
|
|
title: "Create an account",
|
2023-10-04 13:32:30 +03:00
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function postRegister(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
|
2023-10-04 13:32:30 +03:00
|
|
|
if ($this->accountService->countUsers() > 0) {
|
|
|
|
|
abort(400);
|
|
|
|
|
}
|
2023-10-02 23:10:49 +03:00
|
|
|
|
2023-10-04 13:32:30 +03:00
|
|
|
try {
|
|
|
|
|
$this->authService->registerAdmin(
|
|
|
|
|
name: $request->input("name"),
|
|
|
|
|
email: $request->input("email"),
|
|
|
|
|
);
|
2023-10-02 23:10:49 +03:00
|
|
|
} catch (LucentException $th) {
|
|
|
|
|
return fail($th);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ok();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 13:32:30 +03:00
|
|
|
public function login(): View|RedirectResponse
|
2023-10-02 23:10:49 +03:00
|
|
|
{
|
2023-10-04 13:32:30 +03:00
|
|
|
if ($this->accountService->countUsers() == 0) {
|
|
|
|
|
return redirect($this->channelService->channel->lucentUrl . "/register");
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
return $this->svelte->render(
|
|
|
|
|
layout: "account",
|
|
|
|
|
view: "login",
|
|
|
|
|
title: "Log in"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function postLogin(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->authService->sendLoginEmail($request->input("email"));
|
|
|
|
|
} catch (LucentException $th) {
|
|
|
|
|
return fail($th);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function verify(Request $request): View
|
|
|
|
|
{
|
|
|
|
|
return $this->svelte->render(
|
|
|
|
|
layout: "account",
|
|
|
|
|
view: "verify",
|
|
|
|
|
title: "Verify and enter",
|
|
|
|
|
data: [
|
|
|
|
|
"email" => $request->input("email"),
|
|
|
|
|
"token" => $request->input("token"),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function postVerify(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->authService->login($request->input("email"), $request->input("token"));
|
|
|
|
|
} catch (LucentException $th) {
|
|
|
|
|
return fail($th);
|
|
|
|
|
}
|
|
|
|
|
return ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function logout(): RedirectResponse
|
|
|
|
|
{
|
2023-10-04 13:32:30 +03:00
|
|
|
$this->session->flush();
|
|
|
|
|
return redirect($this->channelService->channel->lucentUrl . "/login");
|
2023-10-02 23:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|