Fix login and registration issues
Addresses issues preventing successful login and registration.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
|
||||
import { useState } from "react";
|
||||
import { useState, useEffect } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useToast } from "@/hooks/useToast.wrapper";
|
||||
import { useAuth } from "@/contexts/auth";
|
||||
@@ -7,6 +7,7 @@ import { useTableSetup } from "@/hooks/useTableSetup";
|
||||
import {
|
||||
getLoginErrorMessage,
|
||||
showLoginErrorToast,
|
||||
showLoginSuccessToast,
|
||||
isCorsOrJsonError
|
||||
} from "@/utils/auth/loginUtils";
|
||||
|
||||
@@ -16,12 +17,45 @@ export function useLogin() {
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [loginError, setLoginError] = useState<string | null>(null);
|
||||
const [isOfflineMode, setIsOfflineMode] = useState(() =>
|
||||
localStorage.getItem('offline_mode') === 'true' || !navigator.onLine
|
||||
);
|
||||
|
||||
const navigate = useNavigate();
|
||||
const { toast } = useToast();
|
||||
const { signIn } = useAuth();
|
||||
const { isSettingUpTables, setupTables } = useTableSetup();
|
||||
|
||||
// 네트워크 상태 감지
|
||||
useEffect(() => {
|
||||
const handleOnline = () => {
|
||||
if (isOfflineMode && navigator.onLine) {
|
||||
toast({
|
||||
title: "네트워크 연결됨",
|
||||
description: "인터넷에 다시 연결되었습니다. 온라인 모드로 전환할 수 있습니다.",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const handleOffline = () => {
|
||||
if (!isOfflineMode) {
|
||||
toast({
|
||||
title: "네트워크 연결 끊김",
|
||||
description: "인터넷 연결이 끊겼습니다. 오프라인 모드로 전환할 수 있습니다.",
|
||||
variant: "destructive"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('online', handleOnline);
|
||||
window.addEventListener('offline', handleOffline);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('online', handleOnline);
|
||||
window.removeEventListener('offline', handleOffline);
|
||||
};
|
||||
}, [isOfflineMode, toast]);
|
||||
|
||||
const handleLogin = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setLoginError(null);
|
||||
@@ -38,6 +72,11 @@ export function useLogin() {
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
// 오프라인 모드를 위한 환경 설정
|
||||
if (isOfflineMode) {
|
||||
localStorage.setItem('offline_mode', 'true');
|
||||
}
|
||||
|
||||
const { error, user } = await signIn(email, password);
|
||||
|
||||
if (error) {
|
||||
@@ -46,8 +85,14 @@ export function useLogin() {
|
||||
setLoginError(errorMessage);
|
||||
showLoginErrorToast(errorMessage);
|
||||
} else if (user) {
|
||||
// 로그인 성공 시 필요한 테이블 설정
|
||||
await setupTables();
|
||||
// 로그인 성공
|
||||
showLoginSuccessToast();
|
||||
|
||||
// 온라인 모드에서만 테이블 설정
|
||||
if (!isOfflineMode) {
|
||||
await setupTables();
|
||||
}
|
||||
|
||||
navigate("/");
|
||||
} else {
|
||||
// user가 없지만 error도 없는 경우 (드문 경우)
|
||||
@@ -85,6 +130,8 @@ export function useLogin() {
|
||||
isSettingUpTables,
|
||||
loginError,
|
||||
setLoginError,
|
||||
isOfflineMode,
|
||||
setIsOfflineMode,
|
||||
handleLogin,
|
||||
isCorsOrJsonError: (msg: string | null) => isCorsOrJsonError(msg)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user