2025-07-17 10:05:47 +09:00
|
|
|
<?php
|
|
|
|
|
|
2025-07-18 11:37:07 +09:00
|
|
|
namespace App\Http\Controllers\Api\V1;
|
2025-07-17 10:05:47 +09:00
|
|
|
|
2025-08-18 16:37:02 +09:00
|
|
|
use App\Helpers\ApiResponse;
|
2025-07-29 13:00:25 +09:00
|
|
|
use App\Http\Controllers\Controller;
|
2025-07-29 16:04:28 +09:00
|
|
|
use App\Models\Members\User;
|
2025-07-17 10:05:47 +09:00
|
|
|
use Illuminate\Http\Request;
|
2025-08-18 16:37:02 +09:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2025-07-17 10:05:47 +09:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2025-08-18 16:37:02 +09:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2025-07-17 10:05:47 +09:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ApiController extends Controller
|
|
|
|
|
{
|
|
|
|
|
|
2025-08-01 23:36:47 +09:00
|
|
|
|
2025-07-17 10:05:47 +09:00
|
|
|
public function debugApikey()
|
|
|
|
|
{
|
2025-07-17 16:28:48 +09:00
|
|
|
$message = 'API Key 인증 성공';
|
|
|
|
|
return response()->json(['message' => $message]);
|
2025-07-17 10:05:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-08-01 23:36:47 +09:00
|
|
|
|
2025-07-17 10:05:47 +09:00
|
|
|
public function login(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$userId = $request->input('user_id');
|
|
|
|
|
$userPwd = $request->input('user_pwd');
|
|
|
|
|
|
|
|
|
|
if (!$userId || !$userPwd) {
|
|
|
|
|
return response()->json(['error' => '아이디 또는 비밀번호 누락'], 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-07-26 14:23:13 +09:00
|
|
|
$user = User::where('user_id', $userId)->first();
|
2025-07-17 10:05:47 +09:00
|
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
|
return response()->json(['error' => '사용자를 찾을 수 없습니다.'], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$isValid = false;
|
|
|
|
|
|
2025-07-26 14:23:13 +09:00
|
|
|
if (Str::startsWith($user->password, '$2y$')) {
|
2025-07-17 10:05:47 +09:00
|
|
|
// bcrypt로 해싱된 경우
|
2025-07-26 14:23:13 +09:00
|
|
|
$isValid = Hash::check($userPwd, $user->password);
|
2025-07-17 10:05:47 +09:00
|
|
|
} else {
|
|
|
|
|
// sha256으로 해싱된 경우
|
2025-07-26 14:23:13 +09:00
|
|
|
$isValid = strtoupper(hash('sha256', $userPwd)) === strtoupper($user->password);
|
2025-07-17 10:05:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$isValid) {
|
|
|
|
|
return response()->json(['error' => '아이디 또는 비밀번호가 올바르지 않습니다.'], 401);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 16:44:26 +09:00
|
|
|
//인증토큰 생성
|
|
|
|
|
$token = $user->createToken('front-app')->plainTextToken;
|
|
|
|
|
|
2025-07-17 10:05:47 +09:00
|
|
|
// 선택: DB에 신규 token 저장
|
|
|
|
|
$USER_TOKEN = hash('sha256', $user->mb_id.date('YmdHis'));
|
|
|
|
|
$user->remember_token = $USER_TOKEN;
|
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'message' => '로그인 성공',
|
2025-07-22 16:44:26 +09:00
|
|
|
'user_token' => $token,
|
2025-07-17 10:05:47 +09:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function logout(Request $request)
|
|
|
|
|
{
|
2025-07-28 11:44:07 +09:00
|
|
|
//인증토큰 삭제
|
|
|
|
|
$request->user()->currentAccessToken()->delete();
|
2025-07-17 10:05:47 +09:00
|
|
|
|
|
|
|
|
return response()->json(['message' => '로그아웃 완료']);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 16:37:02 +09:00
|
|
|
|
|
|
|
|
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()) {
|
2025-08-20 17:01:32 +09:00
|
|
|
return response()->json(['error' => $v->errors()->first()], 422);
|
2025-08-18 16:37:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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'], // 캐스트가 알아서 해싱
|
|
|
|
|
]);
|
|
|
|
|
|
2025-08-20 17:01:32 +09:00
|
|
|
return ['user' => $user->only(['id','user_id','name','email','phone'])];
|
2025-08-18 16:37:02 +09:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-17 10:05:47 +09:00
|
|
|
}
|