-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.ts
More file actions
157 lines (138 loc) · 5.2 KB
/
auth.ts
File metadata and controls
157 lines (138 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { apiClient } from './client';
import { useAuthStore } from '@/store/authStore';
import type {
SendVerificationRequest,
SendVerificationResponse,
SendPasswordResetCodeRequest,
SendPasswordResetCodeResponse,
VerifyCodeRequest,
VerifyCodeResponse,
ResetPasswordRequest,
ResetPasswordResponse,
LoginRequest,
LoginResponse,
RefreshTokenRequest,
RefreshTokenResponse,
CheckNicknameRequest,
CheckNicknameResponse,
KakaoLoginRequest,
KakaoLoginResponse,
KakaoSignupRequest,
KakaoSignupResponse,
} from '@/types/api';
// 회원가입 - 인증번호 발송
export const sendVerificationCode = async (data: SendVerificationRequest): Promise<SendVerificationResponse> => {
try {
const response = await apiClient.post<SendVerificationResponse>('/auth/send-code', data);
return response.data;
} catch (error: any) {
throw error.response?.data || { message: '인증번호 발송에 실패했습니다.' };
}
};
// 비밀번호 찾기 - 인증번호 발송
export const sendPasswordResetCode = async (data: SendPasswordResetCodeRequest): Promise<SendPasswordResetCodeResponse> => {
try {
const response = await apiClient.post<SendPasswordResetCodeResponse>('/auth/password/send-code', data);
return response.data;
} catch (error: any) {
throw error.response?.data || { message: '인증번호 발송에 실패했습니다.' };
}
};
// 인증번호 확인
export const verifyCode = async (data: VerifyCodeRequest): Promise<VerifyCodeResponse> => {
try {
const response = await apiClient.post<VerifyCodeResponse>('/auth/verify-code', data);
return response.data;
} catch (error: any) {
throw error.response?.data || { message: '인증번호가 일치하지 않습니다.' };
}
};
// 비밀번호 재설정
export const resetPassword = async (data: ResetPasswordRequest): Promise<ResetPasswordResponse> => {
try {
const response = await apiClient.post<ResetPasswordResponse>('/auth/password/reset', data);
return response.data;
} catch (error: any) {
throw error.response?.data || { message: '비밀번호 재설정에 실패했습니다.' };
}
};
// 로그인 (토큰 저장 로직 유지)
export const login = async (data: LoginRequest): Promise<LoginResponse> => {
try {
const response = await apiClient.post<LoginResponse>('/auth/login', data);
// 전역 상태에 토큰 저장
if (response.data.data) {
useAuthStore.getState().setTokens(
response.data.data.accessToken,
response.data.data.refreshToken
);
}
return response.data;
} catch (error: any) {
throw error.response?.data || { message: '로그인에 실패했습니다.' };
}
};
// 토큰 재발급
export const refreshAccessToken = async (data: RefreshTokenRequest): Promise<RefreshTokenResponse> => {
try {
const response = await apiClient.post<RefreshTokenResponse>('/auth/refresh', data);
// 재발급 받은 토큰을 전역 상태에 저장
if (response.data.data) {
const { accessToken, refreshToken: newRefreshToken } = response.data.data;
useAuthStore.getState().setTokens(
accessToken,
newRefreshToken || data.refreshToken // 새 리프레시 토큰이 없으면 기존 것 유지
);
}
return response.data;
} catch (error: any) {
throw error.response?.data || { message: '토큰 재발급에 실패했습니다.' };
}
};
// 닉네임 중복 확인
export const checkNickname = async (data: CheckNicknameRequest): Promise<CheckNicknameResponse> => {
try {
const response = await apiClient.post<CheckNicknameResponse>('/auth/check-nickname', data);
return response.data;
} catch (error: any) {
throw error.response?.data || { message: '닉네임 확인에 실패했습니다.' };
}
};
// 카카오 로그인
export const kakaoLogin = async (data: KakaoLoginRequest): Promise<KakaoLoginResponse> => {
try {
const response = await apiClient.post<KakaoLoginResponse>('/auth/kakao/login', data);
// localStorage에 직접 토큰 저장
if (response.data.data) {
const { accessToken, refreshToken } = response.data.data;
if (accessToken && refreshToken) {
// localStorage에 직접 저장
if (typeof window !== 'undefined') {
localStorage.setItem('accessToken', accessToken);
localStorage.setItem('refreshToken', refreshToken);
// authStore에도 동기화
useAuthStore.getState().setTokens(accessToken, refreshToken);
}
}
}
return response.data;
} catch (error: any) {
throw error.response?.data || { message: '카카오 로그인에 실패했습니다.' };
}
};
// 카카오 회원가입
export const kakaoSignup = async (data: KakaoSignupRequest): Promise<KakaoSignupResponse> => {
try {
const response = await apiClient.post<KakaoSignupResponse>('/auth/kakao/signup', data);
// 전역 상태에 토큰 저장
if (response.data.data) {
useAuthStore.getState().setTokens(
response.data.data.accessToken,
response.data.data.refreshToken
);
}
return response.data;
} catch (error: any) {
throw error.response?.data || { message: '카카오 회원가입에 실패했습니다.' };
}
};