1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
'use client';
import { useState, useEffect } from "react";
import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import { Card, CardContent } from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import { SendIcon, Loader2, GlobeIcon, ChevronDownIcon, Ship } from "lucide-react";
import { useToast } from "@/hooks/use-toast";
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem } from "@/components/ui/dropdown-menu"
import { useTranslation } from '@/i18n/client'
import { useRouter, useParams, usePathname, useSearchParams } from 'next/navigation';
import {
InputOTP,
InputOTPGroup,
InputOTPSlot,
} from "@/components/ui/input-otp"
import { signIn } from 'next-auth/react';
import { sendOtpAction } from "@/lib/users/send-otp";
import { verifyTokenAction } from "@/lib/users/verifyToken";
import { buttonVariants } from "@/components/ui/button"
import Link from "next/link"
import Image from 'next/image'; // 추가: Image 컴포넌트 import
import { KnoxSSOButton } from './saml-login-button'; // SAML 로그인 버튼 import
export function LoginFormSHI({
className,
...props
}: React.ComponentProps<"div">) {
const params = useParams() || {};
const pathname = usePathname() || '';
const router = useRouter();
const searchParams = useSearchParams();
const token = searchParams?.get('token') || null;
const lng = params.lng as string;
const { t, i18n } = useTranslation(lng, 'login');
const { toast } = useToast();
const handleChangeLanguage = (lang: string) => {
const segments = pathname.split('/');
segments[1] = lang;
router.push(segments.join('/'));
};
const currentLanguageText = i18n.language === 'ko' ? t('languages.korean') : t('languages.english');
const [email, setEmail] = useState('');
const [otpSent, setOtpSent] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [otp, setOtp] = useState('');
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setIsLoading(true);
try {
const result = await sendOtpAction(email, lng);
if (result.success) {
setOtpSent(true);
toast({
title: t('otpSentTitle'),
description: t('otpSentMessage'),
});
} else {
// Handle specific error types
let errorMessage = t('defaultErrorMessage');
// You can handle different error types differently
if (result.error === 'userNotFound') {
errorMessage = t('userNotFoundMessage');
}
toast({
title: t('errorTitle'),
description: result.message || errorMessage,
variant: 'destructive',
});
}
} catch (error) {
// This will catch network errors or other unexpected issues
console.error(error);
toast({
title: t('errorTitle'),
description: t('networkErrorMessage'),
variant: 'destructive',
});
} finally {
setIsLoading(false);
}
};
async function handleOtpSubmit(e: React.FormEvent) {
e.preventDefault();
setIsLoading(true);
try {
// next-auth의 Credentials Provider로 로그인 시도
const result = await signIn('credentials-otp', {
email,
code: otp,
redirect: false, // 커스텀 처리 위해 redirect: false
});
if (result?.ok) {
// 토스트 메시지 표시
toast({
title: t('loginSuccess'),
description: t('youAreLoggedIn'),
});
const callbackUrlParam = searchParams?.get('callbackUrl');
if (callbackUrlParam) {
try {
// URL 객체로 파싱
const callbackUrl = new URL(callbackUrlParam);
// pathname + search만 사용 (호스트 제거)
const relativeUrl = callbackUrl.pathname + callbackUrl.search;
router.push(relativeUrl);
} catch (e) {
// 유효하지 않은 URL이면 그대로 사용 (이미 상대 경로일 수 있음)
router.push(callbackUrlParam);
}
} else {
// callbackUrl이 없으면 기본 대시보드로 리다이렉트
router.push(`/${lng}/evcp/report`);
}
} else {
toast({
title: t('errorTitle'),
description: t('defaultErrorMessage'),
variant: 'destructive',
});
}
} catch (error) {
console.error('Login error:', error);
toast({
title: t('errorTitle'),
description: t('defaultErrorMessage'),
variant: 'destructive',
});
} finally {
setIsLoading(false);
}
}
useEffect(() => {
const verifyToken = async () => {
if (!token) return;
setIsLoading(true);
try {
const data = await verifyTokenAction(token);
if (data.valid) {
setOtpSent(true);
setEmail(data.email ?? '');
} else {
toast({
title: t('errorTitle'),
description: t('invalidToken'),
variant: 'destructive',
});
}
} catch (error) {
toast({
title: t('errorTitle'),
description: t('defaultErrorMessage'),
variant: 'destructive',
});
} finally {
setIsLoading(false);
}
};
verifyToken();
}, [token, toast, t]);
return (
<div className="container relative flex h-screen flex-col items-center justify-center md:grid lg:max-w-none lg:grid-cols-2 lg:px-0">
{/* Left Content */}
<div className="flex flex-col w-full h-screen lg:p-2">
{/* Top bar with Logo + eVCP (left) and "Request Vendor Repository" (right) */}
<div className="flex items-center justify-between">
<div className="flex items-center space-x-2">
{/* <img
src="/images/logo.png"
alt="logo"
className="h-8 w-auto"
/> */}
<Ship className="w-4 h-4" />
<span className="text-md font-bold">eVCP</span>
</div>
</div>
{/* Content section that occupies remaining space, centered vertically */}
<div className="flex-1 flex items-center justify-center">
{/* Your form container */}
<div className="mx-auto w-full flex flex-col space-y-6 sm:w-[350px]">
{/* Here's your existing login/OTP forms: */}
{/* {!otpSent ? ( */}
<form onSubmit={handleOtpSubmit} className="p-6 md:p-8">
{/* <form onSubmit={handleOtpSubmit} className="p-6 md:p-8"> */}
<div className="flex flex-col gap-6">
<div className="flex flex-col items-center text-center">
<h1 className="text-2xl font-bold">{t('loginMessage')}</h1>
</div>
<div className="grid gap-2">
<Input
id="email"
type="email"
placeholder="test@samsung.com"
required
className="h-10"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</div>
<Button type="submit" className="w-full" variant="samsung" disabled={isLoading}>
{isLoading ? t('sending') : t('ContinueWithEmail')}
</Button>
{/* 구분선과 "Or continue with" 섹션 추가 */}
<div className="relative">
<div className="absolute inset-0 flex items-center">
<span className="w-full border-t"></span>
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-background px-2 text-muted-foreground">
{t('orContinueWith')}
</span>
</div>
</div>
{/* SAML 로그인 버튼 - 로직 분리 */}
<KnoxSSOButton />
{/* 언어 선택 드롭다운 */}
<div className="text-center text-sm mx-auto">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="flex items-center gap-2">
<GlobeIcon className="h-4 w-4" />
<span>{currentLanguageText}</span>
<ChevronDownIcon className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuRadioGroup
value={i18n.language}
onValueChange={(value) => handleChangeLanguage(value)}
>
<DropdownMenuRadioItem value="en">
{t('languages.english')}
</DropdownMenuRadioItem>
<DropdownMenuRadioItem value="ko">
{t('languages.korean')}
</DropdownMenuRadioItem>
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>
</form>
{/* )
: (
<form onSubmit={handleOtpSubmit} className="flex flex-col gap-4 p-6 md:p-8">
<div className="flex flex-col gap-6">
<div className="flex flex-col items-center text-center">
<h1 className="text-2xl font-bold">{t('loginMessage')}</h1>
</div>
<div className="grid gap-2 justify-center">
<InputOTP
maxLength={6}
value={otp}
onChange={(value) => setOtp(value)}
>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>
</div>
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? t('verifying') : t('verifyOtp')}
</Button>
<div className="mx-auto">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="flex items-center gap-2">
<GlobeIcon className="h-4 w-4" />
<span>{currentLanguageText}</span>
<ChevronDownIcon className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuRadioGroup
value={i18n.language}
onValueChange={(value) => handleChangeLanguage(value)}
>
<DropdownMenuRadioItem value="en">
{t('languages.english')}
</DropdownMenuRadioItem>
<DropdownMenuRadioItem value="ko">
{t('languages.korean')}
</DropdownMenuRadioItem>
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>
</form>
)} */}
<div className="text-balance text-center text-xs text-muted-foreground [&_a]:underline [&_a]:underline-offset-4 hover:[&_a]:text-primary">
{t('termsMessage')} <a href="#">{t('termsOfService')}</a> {t('and')}
<a href="#">{t('privacyPolicy')}</a>.
</div>
</div>
</div>
</div>
{/* Right BG 이미지 영역 - Image 컴포넌트로 수정 */}
<div className="relative hidden h-full flex-col p-10 text-white dark:border-r md:flex">
{/* Image 컴포넌트로 대체 */}
<div className="absolute inset-0">
<Image
src="/images/02.jpg"
alt="Background image"
fill
priority
sizes="(max-width: 1024px) 100vw, 50vw"
className="object-cover"
/>
</div>
<div className="relative z-10 mt-auto">
<blockquote className="space-y-2">
<p className="text-sm">“{t("blockquote")}”</p>
{/* <footer className="text-sm">SHI</footer> */}
</blockquote>
</div>
</div>
</div>
)
}
|