Skip to content

Commit

Permalink
feat: 닉네임 중복 검사, 회원가입 api 연동 (SanE-Seo#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
ej070961 committed Apr 26, 2024
1 parent 6d549ec commit 0bd3b58
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Community from './pages/Community';
import UserTrailEditor from './pages/UserTrailEditor';
import SeoulTrailsDetail from './pages/SeoulTrailsDetail';
import RedirectPage from './components/Login/RedirectPage';

import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
function App() {
return (
<>
Expand All @@ -36,6 +36,7 @@ function App() {
</Routes>
</LocationProvider>
</BrowserRouter>
<ReactQueryDevtools initialIsOpen={true} />
</>
);
}
Expand Down
7 changes: 0 additions & 7 deletions src/apis/api.tsx

This file was deleted.

36 changes: 36 additions & 0 deletions src/apis/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import axios from 'axios';

// 닉네임 중복 확인을 위한 API 응답 타입을 정의합니다.
type AxiosResponse = {
success: boolean;
message: string;
data: any;
};
export const CheckNicknameDuplicate = async (nickname: string) => {
try {
const res = await axios.get<AxiosResponse>(
`/api/member/duplicate?username=${nickname}`,
);
console.log(res);
return res;
} catch (error) {
console.log(error);
}
};

export const RegisterUser = async (
email: string,
password: string,
nickname: string,
) => {
try {
const res = await axios.post<AxiosResponse>('/api/auth/register', {
name: nickname,
password: password,
email: email,
});
return res;
} catch (error) {
console.log(error);
}
};
11 changes: 8 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import App from './App';
import reportWebVitals from './reportWebVitals';
import { ThemeProvider } from 'styled-components';
import theme from './styles/theme';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement,
);

const queryClient = new QueryClient();
root.render(
<React.StrictMode>
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>
<QueryClientProvider client={queryClient}>
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>
</QueryClientProvider>
</React.StrictMode>,
);

Expand Down
42 changes: 19 additions & 23 deletions src/pages/Signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@ import * as S from '../styles/signup.style';
import { ReactComponent as Logo } from '../assets/icons/logo.svg';
import { useForm, SubmitHandler } from 'react-hook-form';
import { emailRegex, passwordRegex } from '../utils/regex';
import axios from 'axios';

// 닉네임 중복 확인을 위한 API 응답 타입을 정의합니다.
interface CheckNicknameResponse {
success: boolean; // 사용 가능 여부
message: string; // 추가 메시지 (옵션)
data: any;
}
import { CheckNicknameDuplicate, RegisterUser } from '../apis/user';
import { useNavigate } from 'react-router-dom';
function Signup() {
//폼으로 입력받을 데이터 정의
interface FormValue {
type FormValue = {
email: string;
password: string;
pw_confirm: string;
nickname: string;
}

};
const navigate = useNavigate();
//useForm 사용하기
const {
register,
Expand All @@ -37,11 +31,6 @@ function Signup() {
},
});

//서버 api 요청 코드 추가
const onSubmitHandler: SubmitHandler<FormValue> = (data) => {
const { email, password, pw_confirm, nickname } = getValues();
console.log(email, password, pw_confirm, nickname);
};
//비밀번호 값 추적
const password_watch = watch('password');
const nickname_watch = watch('nickname');
Expand All @@ -53,13 +42,20 @@ function Signup() {

const handleCheckDuplicate = async () => {
const { nickname } = getValues();
try {
const res = await axios.get<CheckNicknameResponse>(
`/api/member/duplicate?username=${nickname}`,
);
console.log(res);
} catch (error) {
console.log(error);
const res = await CheckNicknameDuplicate(nickname);
if (res?.status == 200) {
alert('사용가능한 닉네임 입니다.');
}
};

//서버 api 요청 코드 추가
const onSubmitHandler: SubmitHandler<FormValue> = async () => {
const { email, password, pw_confirm, nickname } = getValues();
console.log(email, password, pw_confirm, nickname);
const res = await RegisterUser(email, password, nickname);
if (res?.status == 200) {
alert('회원가입 성공!');
navigate('/login');
}
};
return (
Expand Down

0 comments on commit 0bd3b58

Please sign in to comment.