114 lines
2.8 KiB
PHP
114 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Lucent\Http\Controller;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Contracts\Session\Session;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Lucent\Account\AccountService;
|
|
use Lucent\Account\AuthService;
|
|
use Lucent\Channel\ChannelService;
|
|
use Lucent\LucentException;
|
|
use Lucent\Svelte\Svelte;
|
|
use Lucent\Util\Form\FormException;
|
|
use Lucent\Util\Form\ResponseFormError;
|
|
use function Lucent\Response\fail;
|
|
use function Lucent\Response\ok;
|
|
|
|
|
|
class AuthController
|
|
{
|
|
public function __construct(
|
|
private readonly AuthService $authService,
|
|
private readonly AccountService $accountService,
|
|
private readonly ChannelService $channelService,
|
|
private readonly Session $session,
|
|
private readonly Svelte $svelte,
|
|
)
|
|
{
|
|
|
|
}
|
|
|
|
public function register(Request $request): View|RedirectResponse
|
|
{
|
|
if ($this->accountService->countUsers() > 0) {
|
|
return redirect($this->channelService->channel->lucentUrl . "/login");
|
|
}
|
|
|
|
|
|
return $this->svelte->render(
|
|
layout: "account",
|
|
view: "register",
|
|
title: "Create an account",
|
|
|
|
);
|
|
}
|
|
|
|
|
|
public function postRegister(Request $request): Response
|
|
{
|
|
|
|
if ($this->accountService->countUsers() > 0) {
|
|
abort(400);
|
|
}
|
|
|
|
try {
|
|
$this->authService->registerAdmin(
|
|
name: $request->input("name"),
|
|
email: $request->input("email"),
|
|
);
|
|
} catch (LucentException $th) {
|
|
return fail($th);
|
|
}
|
|
|
|
return ok();
|
|
}
|
|
|
|
public function login()
|
|
{
|
|
if ($this->accountService->countUsers() == 0) {
|
|
return redirect($this->channelService->channel->lucentUrl . "/register");
|
|
}
|
|
|
|
return view("lucent::auth.login");
|
|
}
|
|
|
|
public function postLogin(Request $request)
|
|
{
|
|
$this->authService->sendLoginEmail($request->input("email"));
|
|
|
|
return view("lucent::auth.login-success");
|
|
}
|
|
|
|
public function verify(Request $request): View
|
|
{
|
|
return view("lucent::auth.verify", [
|
|
"email" => $request->input("email"),
|
|
"token" => $request->input("token"),
|
|
]);
|
|
|
|
}
|
|
|
|
public function postVerify(Request $request)
|
|
{
|
|
try {
|
|
$this->authService->login($request->input("email"), $request->input("token"));
|
|
} catch (LucentException $th) {
|
|
return ResponseFormError::fromException($th);
|
|
}
|
|
return [];
|
|
}
|
|
|
|
|
|
public function logout(): RedirectResponse
|
|
{
|
|
$this->session->flush();
|
|
return redirect($this->channelService->channel->lucentUrl . "/login");
|
|
}
|
|
|
|
|
|
}
|