Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 6 additions & 30 deletions src/apis/authService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import httpClient from './httpClient';
import { AxiosError } from 'axios';
import createHttpClient from './createHttpClient';

const httpClient = createHttpClient();

interface UserData {
email: string;
Expand All @@ -12,32 +13,7 @@ interface Credentials {
password: string;
}

// 회원가입
export const registerUser = async (userData: UserData) => {
try {
const response = await httpClient.post('/users', userData);
return response.data;
} catch (error) {
const typedError = error as AxiosError; // 타입 단언
if (typedError.response) {
throw typedError.response.data; // 오류 응답 바디 접근
} else {
throw new Error('An unknown error occurred');
}
}
};

// 로그인
export const login = async (credentials: Credentials) => {
try {
const response = await httpClient.post('/auth/login', credentials);
return response.data;
} catch (error) {
const typedError = error as AxiosError;
if (typedError.response) {
throw typedError.response.data || 'Login failed';
} else {
throw new Error('Network error');
}
}
export const authService = {
registerUser: async (userData: UserData) => await httpClient.post('/users', userData),
login: async (credentials: Credentials) => await httpClient.post('/auth/login', credentials),
};
4 changes: 2 additions & 2 deletions src/pages/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import styles from './Login.module.scss';
import Input from '@/src/components/common/Input';
import BaseButton from '@/src/components/common/Button/BaseButton';
import React, { useState, useEffect } from 'react';
import { login } from '@/src/apis/authService';
import { authService } from '@/src/apis/authService';
import { saveTokenToLocalStorage } from '@/src/utils/authUtils';
import Logo from '@/src/assets/images/Logo.png';
import Title from '@/src/assets/images/Title.png';
Expand Down Expand Up @@ -81,7 +81,7 @@ export default function Login() {
e.preventDefault();
if (!isSubmitEnabled) return;
try {
const data = await login(formData);
const data = (await authService.login(formData)) as any;
console.log('로그인 성공', data);
saveTokenToLocalStorage(data.accessToken);
window.location.href = '/Mydashboard';
Expand Down