Skip to content

Commit

Permalink
feat: 로그인, 회원가입 api 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
pipisebastian committed Nov 18, 2024
1 parent 551149b commit 840de82
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
35 changes: 35 additions & 0 deletions client/src/apis/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useMutation } from "@tanstack/react-query";
import axios from "axios";

export const useSignupMutation = () => {
const fetcher = ({
name,
email,
password,
}: {
name: string;
email: string;
password: string;
}) => {
return axios.post("/auth/register", { name, email, password });
};

return useMutation({
mutationFn: fetcher,
});
};

export const useLoginMutation = () => {
const fetcher = ({ email, password }: { email: string; password: string }) => {
return axios.post("/auth/login", { email, password });
};

return useMutation({
mutationFn: fetcher,
// TODO 성공했을 경우 accessToken 저장 (zustand? localStorage? cookie?)
// accessToken: cookie (쿠기 다 때려넣기...) / localStorage / zustand (번거로움..귀찮음.. 안해봤음..)
// refreshToken: cookie,
// onSuccess: (data) => {
// },
});
};
11 changes: 10 additions & 1 deletion client/src/features/auth/AuthModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Lock from "@assets/icons/lock.svg?react";
import Mail from "@assets/icons/mail.svg?react";
import User from "@assets/icons/user.svg?react";
import { Modal } from "@components/modal/modal";
import { useLoginMutation, useSignupMutation } from "@src/apis/auth";
import { InputField } from "@src/components/inputField/InputField";
import { container, formContainer, title, toggleButton } from "./AuthModal.style";

Expand All @@ -20,6 +21,9 @@ export const AuthModal = ({ isOpen, onClose }: AuthModalProps) => {
password: "",
});

const { mutate: signUp } = useSignupMutation();
const { mutate: login } = useLoginMutation();

const toggleMode = () => {
setMode(mode === "login" ? "register" : "login");
setFormData({ email: "", password: "", name: "" });
Expand All @@ -38,7 +42,12 @@ export const AuthModal = ({ isOpen, onClose }: AuthModalProps) => {

const handleSubmitButtonClick = () => {
// TODO API 연결
closeModal();
if (mode === "register") {
signUp(formData);
} else {
login(formData);
}
// closeModal();
};

return (
Expand Down
17 changes: 16 additions & 1 deletion client/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import axios from "axios";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import "./index.css";
import App from "./App.tsx";

axios.defaults.baseURL = import.meta.env.VITE_API_URL;
axios.defaults.withCredentials = true;

const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
},
},
});

createRoot(document.getElementById("root")!).render(
<StrictMode>
<App />
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
</StrictMode>,
);
4 changes: 4 additions & 0 deletions server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { AppModule } from "./app.module";

const bootstrap = async () => {
const app = await NestFactory.create(AppModule);
app.enableCors({
origin: true, // 개발 중에는 모든 origin 허용
credentials: true, // 쿠키 전송을 위해 필수
});
await app.listen(process.env.PORT ?? 3000);
};
bootstrap();

0 comments on commit 840de82

Please sign in to comment.