Skip to content

Commit

Permalink
Merge pull request #197 from space-401/feat/auth
Browse files Browse the repository at this point in the history
๐Ÿ“fix : auth, space ์—๋Ÿฌ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.
  • Loading branch information
Zero-1016 authored Dec 29, 2023
2 parents 869c30b + 667e081 commit f27282a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
30 changes: 19 additions & 11 deletions src/apis/user/getLogin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { axiosInstance } from '@/apis';
import { PATH } from '@/constants';
import type { SocialType, UserTokenType } from '@/types';
import type { SocialType } from '@/types';
import { UserUnknownTokenType } from '@/types';
import { toastColorMessage } from '@/utils';

export interface ILoginProps {
code: string;
Expand All @@ -10,15 +12,21 @@ export interface ILoginProps {

export const getLogin = async (props: ILoginProps) => {
const { state = false, socialType, code } = props;
const response = await axiosInstance.get<UserTokenType>(
PATH.SOCIAL_LOGIN(socialType),
{
params: {
code,
state,
},
useAuth: false,
try {
const response = await axiosInstance.get<UserUnknownTokenType>(
PATH.SOCIAL_LOGIN(socialType),
{
params: {
code,
state,
},
useAuth: false,
}
);
return response.data;
} catch (error) {
if (error instanceof Error) {
toastColorMessage(error.message);
}
);
return response.data;
}
};
2 changes: 1 addition & 1 deletion src/constants/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const BASE_URL = import.meta.env.VITE_BASE_URL;

export const AXIOS_BASE_URL = import.meta.env.VITE_BACK_URL;

export const END_POINTS = {
export const END_POINTS = {
USER: '/user',
TOKEN: '/user/refreshToken',
MY_POST_LIST: `/user/mypost`,
Expand Down
10 changes: 8 additions & 2 deletions src/hooks/api/space/useSpaceUserDeleteMutation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { deleteSpaceUser } from '@/apis';
import { END_POINTS } from '@/constants';
import type { ApiResponseType } from '@/types';
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import type { AxiosError } from 'axios';

type deleteSpaceType = {
Expand All @@ -9,10 +10,15 @@ type deleteSpaceType = {
};

export const useSpaceUserDeleteMutation = () => {
const queryClient = useQueryClient();
const { mutate: deleteSpaceAction } = useMutation<
ApiResponseType,
AxiosError,
deleteSpaceType
>((deleteInfo) => deleteSpaceUser(deleteInfo.spaceId, deleteInfo.spaceId));
>((deleteInfo) => deleteSpaceUser(deleteInfo.spaceId, deleteInfo.spaceId), {
onSuccess: async () => {
await queryClient.invalidateQueries([END_POINTS.SPACE_LIST]);
},
});
return { deleteSpaceAction };
};
15 changes: 9 additions & 6 deletions src/hooks/api/user/useLoginQuery.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { ILoginProps, getLogin } from '@/apis';
import { UserTokenType } from '@/types';
import { UserUnknownTokenType } from '@/types';
import { useQuery } from '@tanstack/react-query';
import { AxiosError } from 'axios';

export const useLoginQuery = (props: ILoginProps) => {
const { state, code, socialType } = props;
const { data: LoginResponse, isSuccess } = useQuery<
UserTokenType,
AxiosError
>([code, socialType], () => getLogin({ code, socialType, state }));
const {
data: LoginResponse,
isSuccess,
isError,
} = useQuery<UserUnknownTokenType, AxiosError>([code, socialType], () =>
getLogin({ code, socialType, state })
);

return { LoginResponse, isSuccess };
return { LoginResponse, isSuccess, isError };
};
8 changes: 7 additions & 1 deletion src/pages/Auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ const Auth = () => {
const code = new URL(window.location.href).searchParams.get('code')!;
const state = new URL(window.location.href).searchParams.get('state')!;

const { LoginResponse, isSuccess } = useLoginQuery({
const { LoginResponse, isSuccess, isError } = useLoginQuery({
code,
socialType,
state: Boolean(state),
});

useEffect(() => {
if (isError) {
navigate(PATH.HOME);
}
}, [isError, navigate]);

useEffect(() => {
if (isSuccess) {
const { accessToken, refreshToken } = LoginResponse as UserTokenType;
Expand Down
2 changes: 2 additions & 0 deletions src/types/user.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ export type UserTokenType = {
accessToken: string;
refreshToken: string;
};

export type UserUnknownTokenType = UserTokenType | unknown;

0 comments on commit f27282a

Please sign in to comment.