132 lines
4.8 KiB
PHP
132 lines
4.8 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
function set_descope_session_cookies(array $user): void {
|
|
$expire = time() + 86400;
|
|
$cookieOptions = [
|
|
'expires' => $expire,
|
|
'path' => '/',
|
|
'samesite' => 'Lax',
|
|
];
|
|
|
|
$customAttributes = $user['customAttributes'] ?? [];
|
|
$roleNames = $user['roleNames'] ?? [];
|
|
|
|
setcookie('descope_login_id', (string)($user['loginIds'][0] ?? ''), $cookieOptions);
|
|
setcookie('descope_user_id', (string)($user['userId'] ?? ''), $cookieOptions);
|
|
setcookie('descope_user_name', (string)($user['name'] ?? ''), $cookieOptions);
|
|
setcookie('descope_user_email', (string)($user['email'] ?? ''), $cookieOptions);
|
|
setcookie('descope_user_phone', (string)($user['phone'] ?? ''), $cookieOptions);
|
|
setcookie('descope_custom_attributes', base64_encode(json_encode($customAttributes, JSON_UNESCAPED_UNICODE)), $cookieOptions);
|
|
setcookie('descope_role_names', base64_encode(json_encode($roleNames, JSON_UNESCAPED_UNICODE)), $cookieOptions);
|
|
}
|
|
|
|
$email = trim($_POST['mb_id'] ?? '');
|
|
$password = trim($_POST['mb_password'] ?? '');
|
|
|
|
|
|
if (!$email || !$password) {
|
|
echo json_encode(['status'=>'fail', 'message'=>'아이디와 비밀번호는 필수입력입니다.']);
|
|
exit;
|
|
}
|
|
|
|
$descope_project_id = "P2wON5fy1K6kyia269VpeIzYP8oP";
|
|
|
|
$api_url = "https://api.descope.com/v1/auth/password/signin";
|
|
|
|
$data = [
|
|
"loginId" => $email,
|
|
"password" => $password,
|
|
// "loginOptions" => [ ... ] // 옵션 필요시
|
|
];
|
|
$headers = [
|
|
'Content-Type: application/json',
|
|
'Authorization: Bearer ' . $descope_project_id,
|
|
];
|
|
|
|
$ch = curl_init($api_url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
$res_data = json_decode($response, true);
|
|
|
|
if ($http_code === 200 && isset($res_data['sessionJwt'])) {
|
|
// 로그인 성공 → 실패횟수 초기화
|
|
$_SESSION['login_fail_count'] = 0;
|
|
//session으로도 저장
|
|
$_SESSION['user'] = $res_data['user']; // userId, email 등
|
|
$_SESSION['sessionJwt'] = $res_data['sessionJwt'];
|
|
$_SESSION['refreshJwt'] = $res_data['refreshJwt'];
|
|
set_descope_session_cookies($res_data['user'] ?? []);
|
|
|
|
echo json_encode([
|
|
'status' => 'ok',
|
|
'sessionJwt' => $res_data['sessionJwt'] ?? '',
|
|
// 'refreshJwt' => $res_data['refreshJwt'] ?? '',
|
|
'user' => $res_data['user'] ?? [],
|
|
'session' => $_SESSION,
|
|
]);
|
|
// } else {
|
|
// // $msg = $res_data['errorDescription'] ?? '로그인 실패';
|
|
// // echo json_encode(['status'=>'fail', 'message'=>$msg]);
|
|
// // Descope 에러 메시지
|
|
// $errorMsg = $res_data['errorDescription']
|
|
// ?? $res_data['errorMessage']
|
|
// ?? '로그인에 실패했습니다.';
|
|
|
|
// // 2) 한글 매핑
|
|
// if (stripos($errorMsg, 'invalid signin credentials') !== false) {
|
|
// $msg = '비밀번호가 일치하지 않습니다.';
|
|
// } elseif (stripos($errorMsg, 'user not found') !== false) {
|
|
// $msg = "계정을 찾을 수 없습니다.\n로그인에 문제가 있으신 경우 010-8721-7453으로 문의해 주세요.";
|
|
// } else {
|
|
// $msg = $errorMsg;
|
|
// }
|
|
|
|
|
|
// echo json_encode([
|
|
// 'status' => 'fail',
|
|
// 'message' => $msg
|
|
// ]);
|
|
// }
|
|
} else {
|
|
// 실패 → 카운트 증가
|
|
if (!isset($_SESSION['login_fail_count'])) {
|
|
$_SESSION['login_fail_count'] = 0;
|
|
}
|
|
$_SESSION['login_fail_count']++;
|
|
$failCount = $_SESSION['login_fail_count'];
|
|
|
|
// 에러 메시지 가공
|
|
$errorMsg = $res_data['errorDescription']
|
|
?? $res_data['errorMessage']
|
|
?? '로그인에 실패했습니다.';
|
|
|
|
// 한글 매핑
|
|
if (stripos($errorMsg, 'Login is temporarily locked') !== false) {
|
|
$msg = "로그인이 일시적으로 잠겼습니다. 잠시 후 다시 시도해 주세요.";
|
|
} elseif (stripos($errorMsg, 'account locked') !== false) {
|
|
$msg = "로그인 시도가 너무 많아 계정이 잠겼습니다. 관리자에게 문의해 주세요.";
|
|
} elseif (stripos($errorMsg, 'invalid signin credentials') !== false) {
|
|
$msg = "비밀번호가 일치하지 않습니다.";
|
|
} elseif (stripos($errorMsg, 'user not found') !== false) {
|
|
$msg = "계정을 찾을 수 없습니다.\n로그인에 문제가 있으신 경우 02-2141-7434으로 문의해 주세요.";
|
|
} else {
|
|
$msg = $errorMsg;
|
|
}
|
|
|
|
echo json_encode([
|
|
'status' => 'fail',
|
|
'message' => $msg,
|
|
'failCount' => $failCount
|
|
]);
|
|
}
|
|
exit;
|
|
?>
|