55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Services ;
|
|
|
|
use App\Models\Admin;
|
|
use App\Models\User;
|
|
use App\Services\Base\BaseService;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class AuthService extends BaseService {
|
|
|
|
public function __construct() {
|
|
parent::__construct(Admin::class);
|
|
}
|
|
|
|
public function register($data)
|
|
{
|
|
$data['password'] = Hash::make($data['password']);
|
|
$user = $this->class::create($data);
|
|
return $user;
|
|
}
|
|
public function loginAdmin(string $email, string $password): array
|
|
{
|
|
$admin = Admin::whereEmail($email)->first();
|
|
if (! $admin) {
|
|
throw new Exception('wrong_email');
|
|
}
|
|
$this->comparePassword($password, $admin->password);
|
|
|
|
return [
|
|
'admin' => [
|
|
'id' => $admin->id,
|
|
'email' => $admin->email,
|
|
'password' => $admin->password,
|
|
],
|
|
'token' => $admin->createToken('admin_login_token_name')->plainTextToken,
|
|
];
|
|
}
|
|
|
|
public function logout(): bool
|
|
{
|
|
$user = Auth::user();
|
|
// dd($user);
|
|
// $user->tokens()->delete();
|
|
return true;
|
|
}
|
|
|
|
private function comparePassword(string $password, string $hash)
|
|
{
|
|
Hash::check($password, $hash) ?: throw new Exception('invalid_credential');
|
|
}
|
|
} |