|
1 | 1 | import { GoogleLogin } from "@react-oauth/google"; |
2 | 2 | import { Alert, AlertDescription } from "@/components/ui/alert"; |
| 3 | +import { Loader2 } from "lucide-react"; |
3 | 4 | import { useState } from "react"; |
| 5 | +import { useNavigate } from "react-router-dom"; |
| 6 | +import { postLogin } from "@/api/auth"; |
| 7 | +import { toast } from "sonner"; |
4 | 8 |
|
5 | 9 | interface GoogleStepProps { |
6 | 10 | onComplete: (googleToken: string) => void; |
7 | 11 | } |
8 | 12 |
|
9 | 13 | export function GoogleStep({ onComplete }: GoogleStepProps) { |
10 | 14 | const [error, setError] = useState(false); |
| 15 | + const [checking, setChecking] = useState(false); |
| 16 | + const navigate = useNavigate(); |
| 17 | + |
| 18 | + const handleSuccess = async (credential: string) => { |
| 19 | + setChecking(true); |
| 20 | + setError(false); |
| 21 | + try { |
| 22 | + await postLogin({ google_token: credential }); |
| 23 | + // 로그인 성공 = 이미 가입된 계정 |
| 24 | + toast.info("이미 가입된 계정입니다. 로그인되었습니다."); |
| 25 | + navigate("/members"); |
| 26 | + } catch { |
| 27 | + // 로그인 실패 = 미가입 계정 → 회원가입 진행 |
| 28 | + onComplete(credential); |
| 29 | + } finally { |
| 30 | + setChecking(false); |
| 31 | + } |
| 32 | + }; |
11 | 33 |
|
12 | 34 | return ( |
13 | 35 | <div className="space-y-4"> |
14 | 36 | <p className="text-center text-sm text-muted-foreground"> |
15 | 37 | Google 계정으로 인증해주세요. |
16 | 38 | </p> |
17 | 39 | <div className="flex justify-center"> |
18 | | - <GoogleLogin |
19 | | - onSuccess={(response) => { |
20 | | - if (response.credential) { |
21 | | - onComplete(response.credential); |
22 | | - } |
23 | | - }} |
24 | | - onError={() => setError(true)} |
25 | | - size="large" |
26 | | - width="300" |
27 | | - /> |
| 40 | + {checking ? ( |
| 41 | + <div className="flex items-center gap-2 py-2 text-sm text-muted-foreground"> |
| 42 | + <Loader2 className="h-4 w-4 animate-spin" /> |
| 43 | + 확인 중... |
| 44 | + </div> |
| 45 | + ) : ( |
| 46 | + <GoogleLogin |
| 47 | + onSuccess={(response) => { |
| 48 | + if (response.credential) { |
| 49 | + handleSuccess(response.credential); |
| 50 | + } |
| 51 | + }} |
| 52 | + onError={() => setError(true)} |
| 53 | + size="large" |
| 54 | + width="300" |
| 55 | + /> |
| 56 | + )} |
28 | 57 | </div> |
29 | 58 | {error && ( |
30 | 59 | <Alert variant="destructive"> |
|
0 commit comments