Files
lucent-laravel/src/Account/AuthService.php
T

172 lines
4.0 KiB
PHP
Raw Normal View History

2023-10-02 23:10:49 +03:00
<?php
namespace Lucent\Account;
use Carbon\Carbon;
use Illuminate\Contracts\Session\Session;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Lucent\Channel\ChannelService;
use Lucent\LucentException;
use Lucent\Mail\LoginMail;
readonly class AuthService
{
public function __construct(
private ChannelService $channelService,
private UserRepo $userRepo,
public Session $session,
)
{
}
public function currentUserId(): ?string
{
return $this->session->get("user.id");
}
public function isLoggedIn(): bool
{
return !empty($this->currentUserId());
}
/**
* @throws LucentException
*/
public function login(string $email, string $token): void
{
$user = $this->userRepo->findByEmail(new Email($email));
if ($user->isEmpty()) {
throw new LucentException("You account was not found");
}
if ($user->get()->role === Role::REMOVED) {
throw new LucentException("Your account is not active");
}
if ($user->get()->mailToken !== $token) {
throw new LucentException("Token has expired or is invalid");
}
if (Carbon::parse($user->get()->loggedInAt)->lte(Carbon::now()->subHours(1))) {
throw new LucentException("Token has expired.");
}
$newUser = $user->get();
$newUser->updatedAt = Carbon::now()->toJson();
$newUser->mailToken = null;
$this->userRepo->update($newUser);
$this->session->put(["user" => $user->get()->safe()]);
}
public function create(string $name, string $email, string $role): User
{
$user = new User(
id: (string)Str::uuid(),
name: new Name($name),
email: new Email($email),
role: Role::from($role),
createdAt: Carbon::now()->toJson(),
updatedAt: Carbon::now()->toJson(),
loggedInAt: Carbon::now()->toJson(),
mailToken: Token::new(32),
);
$this->userRepo->insert($user);
return $user;
}
/**
* @throws LucentException
*/
public function sendLoginEmail(string $email): void
{
$emailAddress = (new Email($email));
$user = $this->userRepo->findByEmail($emailAddress);
if ($user->isEmpty()) {
throw new LucentException("User not found");
}
if ($user->get()->role === Role::REMOVED) {
throw new LucentException("Cannot reset email if the user is not active");
}
$newToken = $this->userRepo->updateLoginToken($user->get()->id);
Mail::to($email)->send(
new LoginMail(
$email,
$newToken,
$this->channelService->channel->lucentUrl
)
);
}
/**
* @throws LucentException
*/
public function changeRole(string $userId, string $newRole): void
{
$user = $this->userRepo->findById($userId);
if ($user->isEmpty()) {
throw new LucentException("User not found");
}
$newUser = $user->get();
$newUser->role = Role::from($newRole);
$newUser->updatedAt = Carbon::now()->toJson();
$this->userRepo->update($newUser);
}
/**
* @throws LucentException
*/
public function updateName(string $userId, string $name): void
{
$name = (new Name($name));
$this->userRepo->updateName($userId, $name);
}
/**
* @throws LucentException
*/
public function invite(
string $name,
string $email,
string $role
): User
{
$user = $this->create($name, $email, $role);
$this->sendLoginEmail($user->email);
return $user;
}
2023-10-04 13:32:30 +03:00
/**
* @throws LucentException
*/
public function registerAdmin(
string $name,
string $email
): User
{
$user = $this->invite($name, $email, "admin");
$this->sendLoginEmail($user->email);
return $user;
}
2023-10-02 23:10:49 +03:00
}