Skip to content

Commit

Permalink
Refactor : 로그인/아웃 뮤테이션 책임분리
Browse files Browse the repository at this point in the history
  • Loading branch information
jobkaeHenry committed Dec 3, 2023
1 parent 8ac5044 commit dbb67d0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
13 changes: 11 additions & 2 deletions client/src/app/(protectedRoute)/user/setting/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import {
} from "@mui/material";
import PostSeeMoreIcon from "@/assets/icons/PostSeeMoreIcon.svg";
import useLogoutMutation from "@/queries/auth/useLogoutMutation";
import { useRouter } from "next/navigation";
import HOME from "@/const/clientPath";

const SettingPage = () => {
const { data: myInfo } = useMyInfoQuery();
const { mutate: logoutHandler } = useLogoutMutation();
const { mutateAsync: logoutHandler } = useLogoutMutation();
const router = useRouter();

return (
<>
Expand Down Expand Up @@ -67,7 +70,13 @@ const SettingPage = () => {
</PaddingPaper>
<PaddingPaper>
<Typography variant="subtitle2">계정</Typography>
<Button color="secondary" onClick={() => logoutHandler()}>
<Button
color="secondary"
onClick={async() => {
await logoutHandler();
router.push(HOME);
}}
>
로그아웃
</Button>
</PaddingPaper>
Expand Down
11 changes: 7 additions & 4 deletions client/src/components/user/signin/SigninForm.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
"use client";
import useLoginMutation from "@/queries/auth/useLoginMutation";
import { Box, Button, TextField, Typography } from "@mui/material";

import { useRouter } from "next/navigation";
import { useState } from "react";
import HOME from "@/const/clientPath";

const SigninForm = () => {
const [id, setId] = useState("");
const [password, setPassword] = useState("");
const { mutate: loginMutation, isError } = useLoginMutation();
const { mutateAsync: loginMutation, isError } = useLoginMutation();
const router = useRouter();

return (
<Box
component="form"
onSubmit={(event) => {
onSubmit={async (event) => {
event.preventDefault();
if (!id || !password) {
return;
}
loginMutation({ id, password });
await loginMutation({ id, password });
router.push(HOME)
}}
sx={{ mt: 1 }}
>
Expand Down
3 changes: 1 addition & 2 deletions client/src/queries/auth/useLoginMutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { SigninRequirement } from "@/types/auth/signinRequirement";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { MyInfoQueryKeys } from "./useMyInfoQuery";
import { useRouter } from "next/navigation";
import HOME from "@/const/clientPath";
import { useErrorHandler } from "@/utils/errorHandler";
import { useGlobalLoadingStore } from "@/store/useGlobalLoadingStore";

Expand All @@ -26,7 +25,7 @@ const useLoginMutation = () => {
onSuccess: async ({ token }) => {
localStorage?.setItem("accessToken", token);
queryClient.invalidateQueries({ queryKey: MyInfoQueryKeys.all });
router.push(HOME);
router.refresh();
},
onError: (error) => errorHandler(error),
onSettled: () => {
Expand Down
7 changes: 1 addition & 6 deletions client/src/queries/auth/useLogoutMutation.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import HOME from "@/const/clientPath";
import { LOGOUT_BFF } from "@/const/serverPath";
import { axiosBff } from "@/libs/axios";
import { useGlobalSnackbarStore } from "@/store/useGlobalSnackbarStore";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useRouter } from "next/navigation";

const useLogoutMutation = () => {
const router = useRouter();
const queryClient = useQueryClient();

const fireToast = useGlobalSnackbarStore((state) => state.fireToast);

return useMutation({
mutationFn: logoutFn,
onSuccess: () => {
localStorage.removeItem("accessToken");
queryClient.removeQueries();
router.push(HOME);
fireToast("로그아웃이 완료되었습니다");
router.refresh();
},
});
};
Expand Down

0 comments on commit dbb67d0

Please sign in to comment.