58 lines
1.5 KiB
PHP
58 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Api\Client;
|
|
|
|
use App\Repositories\Api\Client\ClientRepositoryInterface;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class ClientAuthService
|
|
{
|
|
protected $clientRepository;
|
|
|
|
public function __construct(ClientRepositoryInterface $clientRepository)
|
|
{
|
|
$this->clientRepository = $clientRepository;
|
|
}
|
|
|
|
/**
|
|
* Xác thực khách hàng và tạo token
|
|
*/
|
|
public function authenticateClient(array $credentials): array
|
|
{
|
|
if (!Auth::guard('client')->attempt($credentials)) {
|
|
throw ValidationException::withMessages([
|
|
'email' => ['Thông tin đăng nhập không chính xác.'],
|
|
]);
|
|
}
|
|
|
|
$client = Auth::guard('client')->user();
|
|
$token = $client->createToken('client-token')->plainTextToken;
|
|
|
|
return [
|
|
'client' => $client,
|
|
'token' => $token,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Đăng ký khách hàng mới và tạo token
|
|
*/
|
|
public function registerClient(array $data): array
|
|
{
|
|
$client = $this->clientRepository->create([
|
|
'fullname' => $data['fullname'],
|
|
'phone' => $data['phone'],
|
|
'email' => $data['email'],
|
|
'password' => bcrypt($data['password']),
|
|
]);
|
|
|
|
$token = $client->createToken('client-token')->plainTextToken;
|
|
|
|
return [
|
|
'client' => $client,
|
|
'token' => $token,
|
|
];
|
|
}
|
|
}
|