Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a0ac0d6
[#56] 📦 axios install
Nov 17, 2024
4449ce6
[#56] 📦 fix node package
Nov 17, 2024
517128a
[#56] ✨ add 'useAuthStore'
Nov 17, 2024
2c04d4b
[#56] ✨ add signIn query (use 'axios')
Nov 17, 2024
f64e983
[#56] ✨ login page (for test)
Nov 17, 2024
92cfbd5
[#56] ✨ add auth Type
Nov 17, 2024
a47c6b1
[#56] ✨ root Page for test (for logout)
Nov 17, 2024
75b5d6a
[#56] 📦 typescript package fix
Nov 17, 2024
7b4ea10
[#56] 🐛 fix for debugging
Nov 17, 2024
b613443
[#56] 🐛 add 'console log' for checking 'response.result'
Nov 17, 2024
b8872a0
Merge branch 'dev' into feat/auth-login
yellowjang Nov 17, 2024
b663f14
[#56] 🔧 add base url in env file
KingNono1030 Nov 18, 2024
eedfa5b
[#56] ♻️ update auth related types
KingNono1030 Nov 18, 2024
16210dc
[#56] 🚚 rename auth api request methods file + useAuthStore
KingNono1030 Nov 18, 2024
d9868c0
[#56] ♻️ rewrite module import paths along to the names changed
KingNono1030 Nov 18, 2024
a2e5a1b
[#56] 🚚 rename module import path
KingNono1030 Nov 18, 2024
aadbe58
[#56] ♻️ rename page name + change how page component is declared wit…
KingNono1030 Nov 18, 2024
56ea4f7
[#56] ♻️ sign-in api request refactor
Nov 22, 2024
0b6711f
[#56] 🌱 pull commits
Nov 22, 2024
adfe93e
[#56] ✨ create sign-in proxy server
Nov 22, 2024
cb1a03d
[#56] ✨ create sign-out proxy server
Nov 22, 2024
7916e75
[#56] 🗑️ remove duplicated files
Nov 22, 2024
a578dc3
merge dev branch into feat/auth-login branch
KingNono1030 Nov 23, 2024
b5e0116
[#56] 💄 format all files
KingNono1030 Nov 23, 2024
fb0ed5f
[#56] 🗑️ remove unused variables and import lines
KingNono1030 Nov 23, 2024
b41b027
[#56] ♻️ turn any type into unknown
KingNono1030 Nov 23, 2024
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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_API_BASE_URL=http://43.202.50.174:8080/
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"@tanstack/react-query": "^5.59.19",
"axios": "^1.7.7",
"clsx": "^2.1.1",
"next": "^15.0.2",
"react": "^18.3.1",
Expand Down Expand Up @@ -55,7 +56,8 @@
"storybook": "^8.4.2",
"tailwindcss": "^3.4.14",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2"
"ts-node": "^10.9.2",
"typescript": "^5.6.3"
},
"scripts": {
"dev": "next dev",
Expand All @@ -76,6 +78,11 @@
"path": "./commit-config.js"
}
},
"pnpm": {
"overrides": {
"@types/node": "22.9.0"
}
},
"name": "",
"version": ""
}
47 changes: 42 additions & 5 deletions pnpm-lock.yaml

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

54 changes: 54 additions & 0 deletions src/app/(pages)/sign-in/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use client'

import React from 'react'
import { SubmitHandler, useForm } from 'react-hook-form'

import { SignInRequest } from '@/types/auth.types'

import { signIn } from '@/services/auth/auth'

export default function LoginPage(): JSX.Element {
const {
register,
handleSubmit,
formState: { errors },
} = useForm<SignInRequest>()

const onSubmit: SubmitHandler<SignInRequest> = async data => {
try {
await signIn(data) // 로그인 요청
alert('로그인 성공')
// window.location.href = '/' // 리다이렉트
} catch (error) {
console.error('로그인 실패:', error)
alert('로그인에 실패했습니다.')
}
}

return (
<div>
<h1>로그인</h1>
<form onSubmit={handleSubmit(onSubmit)}>
<div>
<label htmlFor='email'>이메일</label>
<input
id='email'
type='email'
{...register('email', { required: '이메일을 입력하세요.' })}
/>
{errors.email && <span>{errors.email.message}</span>}
</div>
<div>
<label htmlFor='password'>비밀번호</label>
<input
id='password'
type='password'
{...register('password', { required: '비밀번호를 입력하세요.' })}
/>
{errors.password && <span>{errors.password.message}</span>}
</div>
<button type='submit'>로그인</button>
</form>
</div>
)
}
33 changes: 32 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
'use client'

import { useRouter } from 'next/navigation'

import axios from 'axios'

import { useAuthStore } from '@/stores/useAuthStore'

export default function Home(): JSX.Element {
return <>홈페이지</>
const { logout } = useAuthStore()
const router = useRouter()

const handleLogout = async () => {
try {
await axios.post('/v1/auth/logout', {}, { withCredentials: true })
console.log('서버 로그아웃 성공')

logout()

router.push('/sign-in')
console.log('로그아웃 성공')
} catch (error) {
console.error('로그아웃 실패:', error)
alert('로그아웃에 실패했습니다.')
}
}

return (
<>
<div>홈페이지</div>
<button onClick={handleLogout}>로그아웃</button>
</>
)
}
30 changes: 30 additions & 0 deletions src/services/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { SignInRequest, SignInResponse } from '@/types/auth.types'
import axios from 'axios'

// 환경 변수에서 API BASE URL 가져오기
const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL

if (!BASE_URL) {
throw new Error("환경 변수 'NEXT_PUBLIC_API_BASE_URL'이 정의되지 않았습니다.")
}

// Axios 인스턴스 생성
const apiClient = axios.create({
baseURL: BASE_URL,
// withCredentials: true,
})

// 로그인 요청 처리
export const signIn = async (data: SignInRequest): Promise<SignInResponse> => {
try {
const response = await apiClient.post('/v1/auth/sign-in', data, {
// withCredentials: true,
})
console.log('로그인 성공:', response.data)
return response.data
console.log(response.data.result)
} catch (error) {
console.error('로그인 실패:', (error as Error).message)
throw error // 에러를 호출자에게 전달
}
}
15 changes: 15 additions & 0 deletions src/stores/useAuthStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { create } from 'zustand'

interface AuthState {
isAuthenticated: boolean
user: User | null
login: (user: User) => void
logout: () => void
}

export const useAuthStore = create<AuthState>(set => ({
isAuthenticated: false,
user: null,
login: user => set({ isAuthenticated: true, user }),
logout: () => set({ isAuthenticated: false, user: null }),
}))
6 changes: 3 additions & 3 deletions src/types/apiResponse.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export interface ApiResponse<T = unknown> {
interface ApiResponse<T = unknown> {
isSuccess: SuccessResponse
code: StatusCode
message: Message
result?: T
}
export type ApiResponseObject = ApiResponse<Record<string, never>>
export type ApiResponseString = ApiResponse<string>
type ApiResponseObject = ApiResponse<Record<string, never>>
type ApiResponseString = ApiResponse<string>
11 changes: 9 additions & 2 deletions src/types/auth.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ export interface SignUpRequest {
}

export interface SignInRequest {
email?: Email
password?: Password
email: Email
password: Password
}

export interface SignInResponseResult extends User {
accessToken: Token
refreshToken: Token
}

export type SignInResponse = ApiResponse<SignInResponseResult>
14 changes: 11 additions & 3 deletions src/types/global.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
type Tag = string
type Id = number
type TechStack = string
type Title = string
type Content = string
type SuccessResponse = boolean
type StatusCode = string
type Message = string
type URL = string
type Token = string

type Email = string
type Password = string
type Name = string
type Nickname = string
type GitHub = string

interface User {
id: Id
email: Email
name: Name
nickname: Nickname
imageUrl: URL
}
Loading