Skip to content

Commit a5d1674

Browse files
authored
πŸ‘½ Use the API for authorisation & co. (#32)
### πŸ“ μž‘μ—… λ‚΄μš© - μ—”λ“œν¬μΈνŠΈ `/users`, `/auth`에 κ΄€λ ¨λœ νƒ€μž…μ„ μ •μ˜ν•˜μ˜€μŠ΅λ‹ˆλ‹€. - 둜그인, νšŒμ›κ°€μž… ν™”λ©΄κ³Ό 헀더, `EventCardView` μ»΄ν¬λ„ŒνŠΈμ— μžˆμ—ˆλ˜ μ˜ˆμ‹œ 데이터λ₯Ό μ§€μš°κ³  API와 ν†΅μ‹ ν•˜λ„λ‘ μ½”λ“œλ₯Ό μˆ˜μ •ν•˜μ˜€μŠ΅λ‹ˆλ‹€. ### πŸ“Έ μŠ€ν¬λ¦°μƒ· μ—†μŒ ### πŸš€ 기타 - CORS 문제둜 μ‹€μ œ 톡신이 잘 μ΄λ£¨μ–΄μ§€λŠ”μ§€λŠ” μ‹œν—˜ν•΄ 보지 λͺ»ν–ˆλŠ”λ°μš”, AWS둜 λ°°ν¬ν•˜κ±°λ‚˜ λ°±μ—”λ“œ κ°œλ°œμžλΆ„λ“€κ»˜ μš”μ²­λ“œλ¦¬λ©΄ 큰 문제 없이 해결될 λ“―ν•©λ‹ˆλ‹€.
1 parent 676011d commit a5d1674

File tree

19 files changed

+139
-209
lines changed

19 files changed

+139
-209
lines changed

β€Žsrc/api/auth/login.tsβ€Ž

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import type { AuthResponse, LoginRequest } from '../../types/auth';
2-
import apiClient from '../apiClient';
1+
import apiClient from '@/api/apiClient';
2+
import type { LoginRequest, LoginResponse } from '@/types/auth';
33

4-
export default async function login(data: LoginRequest): Promise<AuthResponse> {
5-
const response = await apiClient.post<AuthResponse>('/auth/login', data);
4+
export default async function login(
5+
data: LoginRequest
6+
): Promise<LoginResponse> {
7+
const response = await apiClient.post<LoginResponse>('/auth/login', data);
68
return response.data;
79
}

β€Žsrc/api/auth/logout.tsβ€Ž

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import apiClient from '@/api/apiClient';
2+
import type { LogoutResponse } from '@/types/auth';
3+
4+
export default async function logout(): Promise<LogoutResponse> {
5+
const response = await apiClient.post<LogoutResponse>('/auth/logout');
6+
return response.data;
7+
}

β€Žsrc/api/auth/me.tsβ€Ž

Lines changed: 0 additions & 7 deletions
This file was deleted.

β€Žsrc/api/auth/signup.tsβ€Ž

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
import type { AuthResponse, SignUpRequest } from '../../types/auth';
2-
import apiClient from '../apiClient';
1+
import apiClient from '@/api/apiClient';
2+
import type { SignUpRequest, SignUpResponse } from '@/types/auth';
33

44
export default async function signup(
55
data: SignUpRequest
6-
): Promise<AuthResponse> {
6+
): Promise<SignUpResponse> {
77
const formData = new FormData();
88

9-
formData.append('username', data.username);
10-
formData.append('password', data.password);
9+
formData.append('email', data.email);
1110
formData.append('name', data.name);
11+
formData.append('password', data.password);
1212

13-
if (data.photo) {
14-
formData.append('photo', data.photo); // λ°±μ—”λ“œ ν•„λ“œλͺ…κ³Ό 'photo' μΌμΉ˜μ‹œν‚€κΈ°
13+
if (data.profileImage) {
14+
formData.append('profileImage', data.profileImage);
1515
}
1616

17-
const response = await apiClient.post<AuthResponse>('/auth/signup', formData);
17+
const response = await apiClient.post<SignUpResponse>(
18+
'/auth/signup',
19+
formData
20+
);
21+
1822
return response.data;
1923
}

β€Žsrc/api/auth/social.tsβ€Ž

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import type { AuthResponse, SocialLoginRequest } from '../../types/auth';
1+
import type { SocialLoginRequest, SocialLoginResponse } from '../../types/auth';
22
import apiClient from '../apiClient';
33

44
export default async function social(
55
data: SocialLoginRequest
6-
): Promise<AuthResponse> {
7-
const response = await apiClient.post<AuthResponse>('/auth/social', data);
6+
): Promise<SocialLoginResponse> {
7+
const response = await apiClient.post<SocialLoginResponse>(
8+
'/auth/social',
9+
data
10+
);
811
return response.data;
912
}

β€Žsrc/api/users/me.tsβ€Ž

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import apiClient from '@/api/apiClient';
2+
import type { GetMeResponse } from '@/types/users';
3+
4+
export default async function getMe(): Promise<GetMeResponse> {
5+
const response = await apiClient.get('/auth/me');
6+
return response.data;
7+
}

β€Žsrc/components/EventCardView.tsxβ€Ž

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,29 @@ import {
77
ItemMedia,
88
ItemTitle,
99
} from '@/components/ui/item';
10-
import type { Events } from '@/types/schema';
10+
import type { Event } from '@/types/event';
1111
import { getShortenedDate } from '@/utils/date';
1212
import { ChevronRightIcon, Plus } from 'lucide-react';
1313
import { Link } from 'react-router';
1414
import { Button } from './ui/button';
1515

16-
type Event = Events & { guests: number; registration_start: string | null };
17-
1816
function getDateLabel(
19-
startDateString: string | null,
20-
endDateString: string | null
17+
startEpoch: number | undefined,
18+
endEpoch: number | undefined
2119
) {
22-
if (!endDateString) {
20+
if (!endEpoch) {
2321
return 'μƒμ‹œ λͺ¨μ§‘ 쀑';
2422
}
2523

2624
const now = new Date();
27-
const endDate = new Date(endDateString!);
25+
const endDate = new Date(endEpoch);
2826

29-
if (startDateString && now < new Date(startDateString)) {
30-
return `${getShortenedDate(startDateString)}λΆ€ν„° λͺ¨μ§‘`;
27+
if (startEpoch && now < new Date(startEpoch)) {
28+
return `${getShortenedDate(startEpoch)}λΆ€ν„° λͺ¨μ§‘`;
3129
}
3230

3331
if (now <= endDate) {
34-
return `${getShortenedDate(endDateString)}κΉŒμ§€ λͺ¨μ§‘`;
32+
return `${getShortenedDate(endEpoch)}κΉŒμ§€ λͺ¨μ§‘`;
3533
}
3634

3735
return 'λͺ¨μ§‘ 마감';
@@ -84,10 +82,7 @@ export default function EventCardView({ events }: { events: Event[] }) {
8482
</h2>
8583
</ItemTitle>
8684
<ItemDescription>
87-
{getDateLabel(
88-
event.registration_start,
89-
event.registration_deadline
90-
)}
85+
{getDateLabel(undefined, event.registrationDeadline)}
9186
</ItemDescription>
9287
</ItemContent>
9388
<ItemActions>

β€Žsrc/components/Header.tsxβ€Ž

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,8 @@ import ProfileButton from '@/components/ProfileButton';
22
import useAuth from '@/hooks/useAuth';
33
import { Link, NavLink } from 'react-router';
44

5-
const userExample = {
6-
id: 1,
7-
8-
name: 'λ°•μ€€μ˜',
9-
profileImage: 'https://github.com/shadcn.png',
10-
createdAt: '2023-01-01T00:00:00.000Z',
11-
updatedAt: '2023-01-01T00:00:00.000Z',
12-
};
13-
145
export default function Header() {
15-
const { isLoggedIn, handleLogout } = useAuth();
6+
const { user, isLoggedIn, handleLogout } = useAuth();
167

178
const linkClassName = (isActive: boolean) => `
189
h-[42px] items-center px-4 py-2 rounded-md text-black font-semibold
@@ -26,7 +17,7 @@ export default function Header() {
2617
λͺ¨μ΄μƒ€
2718
</Link>
2819
<div className="items-center space-x-2">
29-
{!isLoggedIn ? (
20+
{!isLoggedIn || !user ? (
3021
<>
3122
<NavLink
3223
to="/login"
@@ -42,7 +33,7 @@ export default function Header() {
4233
</NavLink>
4334
</>
4435
) : (
45-
<ProfileButton user={userExample} handleLogout={handleLogout} />
36+
<ProfileButton user={user} handleLogout={handleLogout} />
4637
)}
4738
</div>
4839
</div>

β€Žsrc/components/ProfileButton.tsxβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
DropdownMenuSeparator,
99
DropdownMenuTrigger,
1010
} from '@/components/ui/dropdown-menu';
11-
import type { User } from '@/types/auth';
11+
import type { User } from '@/types/user';
1212

1313
interface ProfileButtonProps {
1414
user: User;

β€Žsrc/hooks/useAuth.tsβ€Ž

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { useNavigate } from 'react-router';
2-
import loginApi from '../api/auth/login';
3-
import getMeApi from '../api/auth/me';
4-
import signupApi from '../api/auth/signup';
5-
import socialApi from '../api/auth/social';
1+
import loginApi from '@/api/auth/login';
2+
import logoutApi from '@/api/auth/logout';
3+
import signupApi from '@/api/auth/signup';
4+
import socialApi from '@/api/auth/social';
5+
import getMeApi from '@/api/users/me';
6+
import useAuthStore from '@/hooks/useAuthStore';
67
import type {
78
LoginRequest,
89
SignUpRequest,
910
SocialLoginRequest,
10-
} from '../types/auth';
11-
import useAuthStore from './useAuthStore';
11+
} from '@/types/auth';
12+
import { useNavigate } from 'react-router';
1213

1314
export default function useAuth() {
1415
const navigate = useNavigate();
@@ -20,7 +21,8 @@ export default function useAuth() {
2021
// 1. 이메일 둜그인 둜직
2122
const handleLogin = async (data: LoginRequest) => {
2223
try {
23-
const { user, token } = await loginApi(data);
24+
const { token } = await loginApi(data);
25+
const user = await getMeApi();
2426
login(user, token); // Zustand μŠ€ν† μ–΄ μ—…λ°μ΄νŠΈ
2527
navigate('/'); // 메인 νŽ˜μ΄μ§€λ‘œ 이동
2628
} catch (error) {
@@ -32,9 +34,8 @@ export default function useAuth() {
3234
// 2. 이메일 νšŒμ›κ°€μž… 둜직
3335
const handleSignUp = async (data: SignUpRequest) => {
3436
try {
35-
const { user, token } = await signupApi(data);
36-
login(user, token);
37-
navigate('/');
37+
await signupApi(data);
38+
navigate('/login');
3839
} catch (error) {
3940
console.error('Signup failed:', error);
4041
alert('νšŒμ›κ°€μž… 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.');
@@ -44,7 +45,7 @@ export default function useAuth() {
4445
// 3. μ†Œμ…œ 둜그인 둜직
4546
const handleSocialLogin = async (data: SocialLoginRequest) => {
4647
try {
47-
const { user, token } = await socialApi(data);
48+
const { token, user } = await socialApi(data);
4849
login(user, token);
4950
navigate('/');
5051
} catch (error) {
@@ -68,16 +69,12 @@ export default function useAuth() {
6869
};
6970

7071
// 5. λ‘œκ·Έμ•„μ›ƒ 둜직
71-
const handleLogout = () => {
72+
const handleLogout = async () => {
73+
await logoutApi();
7274
logout(); // Zustand μƒνƒœ μ΄ˆκΈ°ν™”
7375
navigate('/login');
7476
};
7577

76-
// 6. κ΄€λ¦¬μž 확인
77-
const isAdmin = (ownerId: number | undefined) => {
78-
return isLoggedIn && user?.id === ownerId;
79-
};
80-
8178
return {
8279
user,
8380
isLoggedIn,
@@ -86,6 +83,5 @@ export default function useAuth() {
8683
handleSocialLogin,
8784
refreshUser,
8885
handleLogout,
89-
isAdmin,
9086
};
9187
}

0 commit comments

Comments
Β (0)