Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
"lint": "eslint"
},
"dependencies": {
"js-cookie": "^3.0.5",
"next": "16.0.1",
"react": "19.2.0",
"react-dom": "19.2.0"
"react-dom": "19.2.0",
"react-hot-toast": "^2.6.0",
"zustand": "^5.0.10"
},
"devDependencies": {
"@tailwindcss/cli": "^4.1.17",
"@tailwindcss/postcss": "^4",
"@types/js-cookie": "^3.0.6",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
Expand Down
69 changes: 69 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import type { Metadata } from 'next';
import { Geist, Geist_Mono } from 'next/font/google';
import './globals.css';
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import { Toaster } from "react-hot-toast";
import { AuthProvider } from "@/components/auth/AuthProvider";
import "@/app/globals.css";

const geistSans = Geist({
variable: '--font-geist-sans',
subsets: ['latin'],
variable: "--font-geist-sans",
subsets: ["latin"],
});

const geistMono = Geist_Mono({
variable: '--font-geist-mono',
subsets: ['latin'],
variable: "--font-geist-mono",
subsets: ["latin"],
});

export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
Expand All @@ -26,7 +29,10 @@ export default function RootLayout({
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
{children}
<AuthProvider>
{children}
<Toaster position="top-center" />
</AuthProvider>
</body>
</html>
);
Expand Down
24 changes: 24 additions & 0 deletions src/components/auth/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use client";

import { useEffect } from "react";
import Cookies from "js-cookie";
import { useAuthStore } from "@/store/useAuthStore";

export function AuthProvider({ children }: { children: React.ReactNode }) {
const login = useAuthStore((state) => state.login);

useEffect(() => {
// Hydration Logic: Check for token on app load
const token = Cookies.get("accessToken");
if (token) {
// If token exists, restore session state.
// Note: Since we don't store user details in the cookie,
// we pass a placeholder or partial data to set isLoggedIn to true.
// TODO: Call /api/auth/me here to fetch full user profile and validate token.
// If fetch fails (401), the interceptor in apiClient will handle logout.
login({ email: "" });
}
}, [login]);

return <>{children}</>;
}
27 changes: 11 additions & 16 deletions src/components/base-ui/Login/LoginModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@

import Image from "next/image";
import React, { useEffect } from "react";
import LoginLogo from "./LoginLogo";
import styles from "./LoginModal.module.css";
import useLoginForm from "./useLoginForm";
import LoginLogo from "@/components/base-ui/Login/LoginLogo";
import styles from "@/components/base-ui/Login/LoginModal.module.css";
import useLoginForm from "@/components/base-ui/Login/useLoginForm";
import { SOCIAL_LOGINS } from "@/constants/auth";

type Props = {
onClose: () => void;
onFindAccount?: () => void;
onSignUp?: () => void;
};

const SOCIAL_LOGINS = [
{ name: "google", icon: "/googleLogo.svg", alt: "๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ" },
{ name: "naver", icon: "/naverLogo.svg", alt: "๋„ค์ด๋ฒ„ ๋กœ๊ทธ์ธ" },
{ name: "kakao", icon: "/kakaoLogo.svg", alt: "์นด์นด์˜ค ๋กœ๊ทธ์ธ" },
];

export default function LoginModal({
onClose,
onFindAccount,
Expand All @@ -31,7 +26,7 @@ export default function LoginModal({
handleLogin,
handleSocialLogin,
handleKeyDown,
} = useLoginForm();
} = useLoginForm(onClose);

// Body scroll lock
useEffect(() => {
Expand Down Expand Up @@ -61,19 +56,19 @@ export default function LoginModal({
{/* ์ธํ’‹ ํ•„๋“œ */}
<div className={styles.inputWrapper}>
<input
name="id"
name="email"
type="text"
value={form.id}
value={form.email}
onChange={handleChange}
placeholder="์•„์ด๋””"
placeholder="์ด๋ฉ”์ผ"
className={`${styles.input} ${
errors?.id ? styles.inputError : ""
errors?.email ? styles.inputError : ""
}`}
onKeyDown={handleKeyDown}
disabled={isLoading}
/>
{errors?.id && (
<span className={styles.errorMessage}>{errors.id}</span>
{errors?.email && (
<span className={styles.errorMessage}>{errors.email}</span>
)}
<input
name="password"
Expand Down
1 change: 1 addition & 0 deletions src/components/base-ui/Login/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as LoginModal } from "./LoginModal";
55 changes: 38 additions & 17 deletions src/components/base-ui/Login/useLoginForm.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { useState } from "react";
import { useRouter } from "next/navigation";
import Cookies from "js-cookie";
import toast from "react-hot-toast";
import { useAuthStore } from "@/store/useAuthStore";
import { LoginForm } from "@/types/auth";
import { authService } from "@/services/authService";
import { ApiError } from "@/lib/api/ApiError";

export interface LoginForm {
id: string;
password: string;
}
export default function useLoginForm(onSuccess?: () => void) {
const router = useRouter();
const login = useAuthStore((state) => state.login);
const [form, setForm] = useState<LoginForm>({ email: "", password: "" });

export default function useLoginForm() {
const [form, setForm] = useState<LoginForm>({ id: "", password: "" });
const [errors, setErrors] = useState<Partial<LoginForm>>({});
const [isLoading, setIsLoading] = useState(false);

Expand All @@ -23,7 +28,7 @@ export default function useLoginForm() {
const handleLogin = async () => {
// 1. Validation (Inline Error)
const newErrors: Partial<LoginForm> = {};
if (!form.id) newErrors.id = "์•„์ด๋””๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.";
if (!form.email) newErrors.email = "์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.";
if (!form.password) newErrors.password = "๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.";

if (Object.keys(newErrors).length > 0) {
Expand All @@ -35,20 +40,36 @@ export default function useLoginForm() {

// 2. Submission Logic
setIsLoading(true);
setErrors({});

try {
console.log("๋กœ๊ทธ์ธ ์‹œ๋„:", form);
// TODO: API Call here
// await api.login(form);
// Service Layer ํ˜ธ์ถœ
const data = await authService.login(form);

console.log("๋กœ๊ทธ์ธ ์„ฑ๊ณต:", data);
// 1. Token Storage (Secure Cookie)
if (data.isSuccess && data.result?.accessToken) {
Cookies.set("accessToken", data.result.accessToken, {
secure: true,
sameSite: "strict",
});
}

// Simulate delay
await new Promise((resolve) => setTimeout(resolve, 1000));
// 2. Global State Update
login({ email: form.email });

// Success handling (e.g., close modal, redirect)
// onClose();
// 3. Navigation & UI Feedback
toast.success("๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค!");
if (onSuccess) onSuccess();
router.push("/");
} catch (error) {
console.error("๋กœ๊ทธ์ธ ์‹คํŒจ:", error);
// ๋กœ๊ทธ์ธ ์‹คํŒจ ์‹œ ์ „์ฒด ์—๋Ÿฌ ์ฒ˜๋ฆฌ ๋“ฑ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
alert("๋กœ๊ทธ์ธ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.");
if (error instanceof ApiError) {
// ๋น„์ฆˆ๋‹ˆ์Šค ์—๋Ÿฌ (์˜ˆ: ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ถˆ์ผ์น˜)๋Š” ์ธ๋ผ์ธ ์—๋Ÿฌ๋กœ ์ฒ˜๋ฆฌ
setErrors({ email: error.message });
} else {
console.error("๋กœ๊ทธ์ธ ์‹คํŒจ:", error);
toast.error("๋กœ๊ทธ์ธ ์š”์ฒญ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.");
}
} finally {
setIsLoading(false);
}
Expand Down
5 changes: 5 additions & 0 deletions src/constants/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const SOCIAL_LOGINS = [
{ name: "google", icon: "/googleLogo.svg", alt: "๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ" },
{ name: "naver", icon: "/naverLogo.svg", alt: "๋„ค์ด๋ฒ„ ๋กœ๊ทธ์ธ" },
{ name: "kakao", icon: "/kakaoLogo.svg", alt: "์นด์นด์˜ค ๋กœ๊ทธ์ธ" },
];
Loading