154 lines
5.2 KiB
TypeScript
154 lines
5.2 KiB
TypeScript
import { useState } from "react";
|
|
import { ACCESS_TOKEN, AUTH_USER_ID, REFRESH_TOKEN, CLAIM_NAME } from "@/shared/config";
|
|
import { tokenManager } from "@/entities/auth/lib/token-manager";
|
|
import {
|
|
Button,
|
|
Dialog,
|
|
DialogContent,
|
|
} from "@common/components";
|
|
|
|
interface OtpModalProps {
|
|
onClose: () => void;
|
|
onSuccess: () => void;
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
}
|
|
|
|
export default function OtpModal({ onClose, onSuccess, open, onOpenChange }: OtpModalProps) {
|
|
const [otp, setOtp] = useState("");
|
|
const [error, setError] = useState("");
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const handleSubmit = async () => {
|
|
try {
|
|
setIsLoading(true);
|
|
setError("");
|
|
|
|
const tempToken = localStorage.getItem("tempToken");
|
|
console.log("tempToken from localStorage:", tempToken);
|
|
|
|
if (!tempToken) {
|
|
setError("임시 토큰이 없습니다. 로그인을 다시 시도해주세요.");
|
|
return;
|
|
}
|
|
|
|
// Proxy를 통해 OTP 인증 처리
|
|
const response = await fetch(`/api/proxy/user-service/api/v1/auth/2fa/verify`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
otpCode: otp,
|
|
tempToken
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
let errorMsg = errorData.message || 'OTP 인증 실패';
|
|
if (Array.isArray(errorData.errors) && errorData.errors.length > 0) {
|
|
errorMsg = errorData.errors.map((e: { message: string }) => e.message).join(', ');
|
|
}
|
|
throw new Error(errorMsg);
|
|
}
|
|
|
|
const data = await response.json();
|
|
console.log("OTP verification response:", data);
|
|
console.log("Response headers:", Object.fromEntries(response.headers.entries()));
|
|
console.log("Response data keys:", Object.keys(data));
|
|
console.log("Full response data:", JSON.stringify(data, null, 2));
|
|
|
|
// 2FA 응답에서는 access-token만 있고 token-id는 없을 수 있음
|
|
const accessToken = data[ACCESS_TOKEN] || data.accessToken || data.access_token || data.token || data['ss-token'];
|
|
|
|
console.log("Extracted accessToken:", !!accessToken);
|
|
console.log("Token value - accessToken:", accessToken?.substring(0, 20) + "...");
|
|
|
|
// access-token이 있는 경우 저장
|
|
if (accessToken) {
|
|
console.log("Saving access token from OTP verification");
|
|
tokenManager.setToken(CLAIM_NAME, accessToken); // Authorization으로 저장
|
|
|
|
// 리프레시 토큰도 확인
|
|
const refreshToken = data[REFRESH_TOKEN] || data.refreshToken || data.refresh_token;
|
|
if (refreshToken) {
|
|
console.log("Saving refresh token");
|
|
tokenManager.setToken(REFRESH_TOKEN, refreshToken);
|
|
}
|
|
|
|
// userId는 원래 로그인 응답에서 가져온 값 사용
|
|
const originalUserId = data.userId;
|
|
if (originalUserId) {
|
|
console.log("Saving userId from original login response");
|
|
tokenManager.setToken(AUTH_USER_ID, originalUserId);
|
|
}
|
|
|
|
console.log("All tokens saved successfully");
|
|
} else {
|
|
console.log("No access token in OTP response");
|
|
console.log("Available data keys:", Object.keys(data));
|
|
console.log("Data values:", Object.entries(data).map(([k, v]) => `${k}: ${typeof v === 'string' ? v.substring(0, 20) + '...' : v}`));
|
|
}
|
|
|
|
onSuccess();
|
|
} catch (error: unknown) {
|
|
console.error("OTP verification error:", error);
|
|
if (error instanceof Error) {
|
|
setError(error.message);
|
|
} else {
|
|
setError("OTP 인증 실패");
|
|
}
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleClose = () => {
|
|
onOpenChange(false);
|
|
onClose();
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="max-w-md">
|
|
<div className="pt-8 text-center">
|
|
<h2 className="text-[1.3rem] font-semibold mb-6">OTP 인증</h2>
|
|
|
|
<div className="space-y-4">
|
|
<input
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent text-lg"
|
|
placeholder="OTP 코드를 입력하세요"
|
|
value={otp}
|
|
onChange={(e) => setOtp(e.target.value)}
|
|
disabled={isLoading}
|
|
/>
|
|
|
|
{error && (
|
|
<div className="text-red-500 text-sm text-center">
|
|
{error}
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex space-x-2 pt-4">
|
|
<Button
|
|
onClick={handleSubmit}
|
|
disabled={isLoading || !otp.trim()}
|
|
className="flex-1"
|
|
>
|
|
{isLoading ? "인증 중..." : "인증"}
|
|
</Button>
|
|
<Button
|
|
variant="outline"
|
|
onClick={handleClose}
|
|
disabled={isLoading}
|
|
className="flex-1"
|
|
>
|
|
닫기
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|