feat: Implement new admin API endpoints for product, customer, vCenter, and authentication, and add vCenter resource management UI.
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\Admin\AdminUserResource;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Tymon\JWTAuth\Facades\JWTAuth;
|
||||
use Tymon\JWTAuth\Exceptions\JWTException;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
public function login(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'username' => 'required|string',
|
||||
'password' => 'required|string',
|
||||
]);
|
||||
|
||||
$token = Auth::guard('admins-api')->attempt($request->only('username', 'password'));
|
||||
|
||||
if (!$token) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Thông tin đăng nhập không chính xác.',
|
||||
], 401);
|
||||
}
|
||||
|
||||
$user = Auth::guard('admins-api')->user();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'token' => $token,
|
||||
'token_type' => 'bearer',
|
||||
'expires_in' => config('jwt.ttl') * 60,
|
||||
'user' => new AdminUserResource($user),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function me(): JsonResponse
|
||||
{
|
||||
$user = Auth::guard('admins-api')->user();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => ['user' => new AdminUserResource($user)],
|
||||
]);
|
||||
}
|
||||
|
||||
public function refresh(): JsonResponse
|
||||
{
|
||||
try {
|
||||
$newToken = JWTAuth::parseToken()->refresh();
|
||||
$user = JWTAuth::setToken($newToken)->toUser();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'token' => $newToken,
|
||||
'token_type' => 'bearer',
|
||||
'expires_in' => config('jwt.ttl') * 60,
|
||||
'user' => new AdminUserResource($user),
|
||||
],
|
||||
]);
|
||||
} catch (JWTException) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Không thể làm mới phiên đăng nhập. Vui lòng đăng nhập lại.',
|
||||
], 401);
|
||||
}
|
||||
}
|
||||
|
||||
public function logout(): JsonResponse
|
||||
{
|
||||
Auth::guard('admins-api')->logout();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã đăng xuất thành công.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user