99 lines
2.9 KiB
TypeScript
99 lines
2.9 KiB
TypeScript
import axios from 'src/axios/axios';
|
|
import { jwtDecode } from 'jwt-decode';
|
|
|
|
export const getAccessTokenFromCookie = (): string | null => {
|
|
const name = 'access_token';
|
|
const value = `; ${document.cookie}`;
|
|
const parts = value.split(`; ${name}=`);
|
|
if (parts.length === 2) return parts.pop()?.split(';').shift() || null;
|
|
return null;
|
|
};
|
|
|
|
export const getRefreshTokenFromCookie = (): string | null => {
|
|
const name = 'refresh_token';
|
|
const value = `; ${document.cookie}`;
|
|
const parts = value.split(`; ${name}=`);
|
|
if (parts.length === 2) return parts.pop()?.split(';').shift() || null;
|
|
return null;
|
|
};
|
|
|
|
export interface LoginCredentials {
|
|
memberId: string;
|
|
password: string;
|
|
}
|
|
|
|
export interface RegisterData {
|
|
username: string;
|
|
email: string;
|
|
password: string;
|
|
}
|
|
|
|
export interface AuthResponse {
|
|
resultCode: string;
|
|
resultMessage: string;
|
|
resultData: string;
|
|
}
|
|
|
|
export interface DecodedToken {
|
|
encryptedPayload: string;
|
|
memberId: string;
|
|
memberName: string;
|
|
exp: number;
|
|
}
|
|
|
|
// 로그인 API
|
|
export const login = async (credentials: LoginCredentials): Promise<AuthResponse> => {
|
|
const response = await axios.post<AuthResponse>('/auth/login', credentials);
|
|
return response.data;
|
|
};
|
|
|
|
// 회원가입 API
|
|
export const register = async (data: RegisterData): Promise<{ message: string }> => {
|
|
const response = await axios.post<{ message: string }>('/auth/register', data);
|
|
return response.data;
|
|
};
|
|
|
|
// 로그아웃 API
|
|
export const logout = async (): Promise<void> => {
|
|
await axios.post('/auth/logout');
|
|
localStorage.removeItem('accessToken');
|
|
localStorage.removeItem('refreshToken');
|
|
document.cookie = 'access_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/';
|
|
document.cookie = 'refresh_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/';
|
|
};
|
|
|
|
// 토큰이 유효한지 확인
|
|
export const isTokenValid = (token: string): boolean => {
|
|
try {
|
|
const decoded = jwtDecode<DecodedToken>(token);
|
|
const currentTime = Date.now() / 1000;
|
|
|
|
if (!decoded.exp) return false; // 만료 정보가 없으면 일단 유효하다고 판단하거나, 정책에 따라 변경 가능
|
|
|
|
return decoded.exp > currentTime;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
// 토큰에서 사용자 정보 추출
|
|
export const getUserFromToken = (token: string): DecodedToken | null => {
|
|
try {
|
|
const decoded = jwtDecode<DecodedToken>(token);
|
|
return decoded;
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
// Access Token 갱신 API
|
|
export const renewAccessToken = async (refreshToken: string): Promise<AuthResponse> => {
|
|
// Refresh Token을 Header에 담아 전송 (Authorization: Bearer <token>)
|
|
const response = await axios.post<AuthResponse>('/auth/renewAccessToken', null, {
|
|
headers: {
|
|
'Authorization': `Bearer ${refreshToken}`
|
|
}
|
|
});
|
|
return response.data;
|
|
};
|