- AuthService: 토큰 발급/갱신 통합 관리 - RefreshController: POST /api/v1/refresh 엔드포인트 추가 - 액세스 토큰 2시간, 리프레시 토큰 7일 (.env 설정) - TOKEN_EXPIRED 에러 코드로 프론트엔드 자동 리프레시 지원 - 리프레시 토큰 일회성 사용 (보안 강화) - Swagger 문서 Auth 태그로 통합
109 lines
3.5 KiB
PHP
109 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Members\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ApiController extends Controller
|
|
{
|
|
public function debugApikey()
|
|
{
|
|
$message = 'API Key 인증 성공';
|
|
|
|
return response()->json(['message' => $message]);
|
|
}
|
|
|
|
public function login(Request $request)
|
|
{
|
|
$userId = $request->input('user_id');
|
|
$userPwd = $request->input('user_pwd');
|
|
|
|
if (! $userId || ! $userPwd) {
|
|
return response()->json(['error' => '아이디 또는 비밀번호 누락'], 400);
|
|
}
|
|
|
|
$user = User::where('user_id', $userId)->first();
|
|
|
|
if (! $user) {
|
|
return response()->json(['error' => '사용자를 찾을 수 없습니다.'], 404);
|
|
}
|
|
|
|
$isValid = false;
|
|
|
|
if (Str::startsWith($user->password, '$2y$')) {
|
|
// bcrypt로 해싱된 경우
|
|
$isValid = Hash::check($userPwd, $user->password);
|
|
} else {
|
|
// sha256으로 해싱된 경우
|
|
$isValid = strtoupper(hash('sha256', $userPwd)) === strtoupper($user->password);
|
|
}
|
|
|
|
if (! $isValid) {
|
|
return response()->json(['error' => '아이디 또는 비밀번호가 올바르지 않습니다.'], 401);
|
|
}
|
|
|
|
// 액세스 + 리프레시 토큰 발급
|
|
$tokens = \App\Services\AuthService::issueTokens($user);
|
|
|
|
// 사용자 정보 조회 (테넌트 + 메뉴 포함)
|
|
$loginInfo = \App\Services\MemberService::getUserInfoForLogin($user->id);
|
|
|
|
return response()->json([
|
|
'message' => '로그인 성공',
|
|
'access_token' => $tokens['access_token'],
|
|
'refresh_token' => $tokens['refresh_token'],
|
|
'token_type' => $tokens['token_type'],
|
|
'expires_in' => $tokens['expires_in'],
|
|
'expires_at' => $tokens['expires_at'],
|
|
'user' => $loginInfo['user'],
|
|
'tenant' => $loginInfo['tenant'],
|
|
'menus' => $loginInfo['menus'],
|
|
]);
|
|
}
|
|
|
|
public function logout(Request $request)
|
|
{
|
|
// 인증토큰 삭제
|
|
$request->user()->currentAccessToken()->delete();
|
|
|
|
return response()->json(['message' => '로그아웃 완료']);
|
|
}
|
|
|
|
public function signup(Request $request)
|
|
{
|
|
// 신규 회원 생성 + 역할 부여 지원
|
|
$v = Validator::make($request->all(), [
|
|
'user_id' => 'required|string|max:255|unique:users,user_id',
|
|
'name' => 'required|string|max:255',
|
|
'email' => 'required|email|max:100|unique:users,email',
|
|
'phone' => 'nullable|string|max:30',
|
|
'password' => 'required|string|min:8|max:64',
|
|
]);
|
|
|
|
if ($v->fails()) {
|
|
return response()->json(['error' => $v->errors()->first()], 422);
|
|
}
|
|
|
|
$payload = $v->validated();
|
|
|
|
return DB::transaction(function () use ($payload) {
|
|
// 신규 사용자 생성
|
|
$user = User::create([
|
|
'user_id' => $payload['user_id'],
|
|
'name' => $payload['name'],
|
|
'email' => $payload['email'],
|
|
'phone' => $payload['phone'] ?? null,
|
|
'password' => $payload['password'], // 캐스트가 알아서 해싱
|
|
]);
|
|
|
|
return ['user' => $user->only(['id', 'user_id', 'name', 'email', 'phone'])];
|
|
});
|
|
}
|
|
}
|