/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/Services/Auth/BaseAuthService.php
74 строки
2 KB
vivanenko
add auth
21 май 2025, 16:49
21 май 2025, 16:49
408bda1
Код
Авторство
О чём код?
<?php namespace App\Services\Auth; use App\Contracts\Auth\AuthenticationService; use App\DTOs\Auth\LoginData; use App\DTOs\Auth\RegisterUserData; use App\Models\User; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use RuntimeException; class BaseAuthService implements AuthenticationService { public function register(RegisterUserData $data): User { $user = User::create([ 'name' => $data->name, 'email' => $data->email, 'password' => Hash::make($data->password), 'telegram_id' => $data->telegramId, 'telegram_username' => $data->telegramUsername, 'is_telegram_verified' => !empty($data->telegramId), ]); return $user; } public function login(LoginData $data): string { if (!Auth::attempt([ 'email' => $data->email, 'password' => $data->password ], $data->remember)) { throw new RuntimeException('Invalid credentials'); } $user = $this->getCurrentUser(); if (!$user) { throw new RuntimeException('User not found'); } return $user->createToken('auth_token')->plainTextToken; } public function logout(): void { $user = $this->getCurrentUser(); if ($user) { $user->currentAccessToken()->delete(); } Auth::logout(); } public function getCurrentUser(): ?User { return Auth::user(); } protected function validateEmail(string $email): void { if (User::where('email', $email)->exists()) { throw new RuntimeException('Email already exists'); } } protected function validateTelegramId(?string $telegramId): void { if ($telegramId && User::where('telegram_id', $telegramId)->exists()) { throw new RuntimeException('Telegram account already linked to another user'); } } }