Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 회원가입 페이지 API 연동 (추가 정보 페이지 미 포함) #36

Merged
merged 15 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions src/api/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { defaultInstance } from './httpRequest';

interface AuthTokenType {
accessToken: string;
refreshToken: string;
}

const postLogin = async (loginInfo: { email: string; password: string }) => {
const result = await defaultInstance<AuthTokenType>({
method: 'POST',
url: '/login',
data: loginInfo,
});

return result;
};

const requestLogout = async () => {
const result = await defaultInstance<AuthTokenType>({
method: 'GET',
url: '/logout/success',
});

return result;
};

export { postLogin, requestLogout };
10 changes: 7 additions & 3 deletions src/api/httpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios';

type HttpResponseType = { isSuccess: boolean; code: number; message: string };
type HttpSuccessType<T> = HttpResponseType & { result?: T };
export type HttpResponseType = {
isSuccess: boolean;
code: number;
message: string;
};
export type HttpSuccessType<T> = HttpResponseType & { result?: T };

const BASE_URL = '/api';

Expand All @@ -16,7 +20,7 @@ authAxios.interceptors.response.use((config) => {
return config;
});

class ResponseError extends Error {
export class ResponseError extends Error {
isSuccess: boolean;

code: number;
Expand Down
33 changes: 33 additions & 0 deletions src/api/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { defaultInstance } from './httpRequest';

const postSocialAccount = async (account: { socialAccountUrl: string }) => {
const url = `/social-accounts`;

const result = await defaultInstance({
method: 'POST',
url,
data: account,
});

return result;
};

const postInterest = async ({
userId,
interestName,
}: {
userId: number;
interestName: string;
}) => {
const url = `/users/${userId}/interests`;

const result = await defaultInstance({
method: 'POST',
url,
data: { userId, interestName },
});

return result;
};

export { postSocialAccount, postInterest };
18 changes: 18 additions & 0 deletions src/api/mutations/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { UseMutationOptions, useMutation } from '@tanstack/react-query';
import { postLogin } from '../auth';
import { HttpSuccessType, ResponseError } from '../httpRequest';

const useLogin = async (
options: UseMutationOptions<
HttpSuccessType<{ accessToken: string; refreshToken: string }>,
ResponseError,
{ email: string; password: string }
>,
) => {
return useMutation({
mutationFn: (loginInfo) => postLogin(loginInfo),
...options,
});
};

export { useLogin };
31 changes: 31 additions & 0 deletions src/api/mutations/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { UseMutationOptions, useMutation } from '@tanstack/react-query';
import { HttpSuccessType, ResponseError } from '../httpRequest';
import { postInterest, postSocialAccount } from '../info';

const useAddSocialAccountMutation = (
options: UseMutationOptions<
HttpSuccessType<unknown>,
ResponseError,
{ socialAccountUrl: string }
>,
) => {
return useMutation({
mutationFn: postSocialAccount,
...options,
});
};

const useAddInterest = (
options: UseMutationOptions<
HttpSuccessType<unknown>,
ResponseError,
{ userId: number; interestName: string }
>,
) => {
return useMutation({
mutationFn: postInterest,
...options,
});
};

export { useAddSocialAccountMutation, useAddInterest };
56 changes: 56 additions & 0 deletions src/api/mutations/sign-up.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { UseMutationOptions, useMutation } from '@tanstack/react-query';
import {
UserSignUpType,
postConfirmCode,
postConfirmCodeByEamil,
postConfirmUsernameDuplicate,
postSignupUser,
} from '../sign-up';
import { HttpSuccessType, ResponseError } from '../httpRequest';

export const useSelfSignUpMutation = (
options: UseMutationOptions<HttpSuccessType<unknown>, ResponseError, string>,
) =>
useMutation({
mutationFn: postConfirmCodeByEamil,
...options,
});

export const useEamilConfirmMutation = (
options: UseMutationOptions<
HttpSuccessType<{ emailToken: string }>,
ResponseError,
{ email: string; code: string }
> = {},
) => {
return useMutation({
mutationFn: postConfirmCode,
...options,
});
};

export const useConfirmUsernameDuplicate = (
options: UseMutationOptions<
HttpSuccessType<{ usernameToken: string }>,
ResponseError,
string
> = {},
) => {
return useMutation({
mutationFn: postConfirmUsernameDuplicate,
...options,
});
};

export const useSignupUser = (
options: UseMutationOptions<
HttpSuccessType<unknown>,
ResponseError,
UserSignUpType
> = {},
) => {
return useMutation({
mutationFn: postSignupUser,
...options,
});
};
69 changes: 69 additions & 0 deletions src/api/sign-up.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { defaultInstance } from './httpRequest';

type UserSignUpType = {
email: string;
password: string;
username: string;
emailToken: string;
usernameToken: string;
};

const postConfirmCodeByEamil = async (email: string) => {
const url = `/verification/email?email=${email}`;

const result = await defaultInstance({
method: 'POST',
url,
});

return result;
};

const postConfirmCode = async ({
email,
code,
}: {
email: string;
code: string;
}) => {
const url = `/verification/email/${email}?code=${code}`;

const result = await defaultInstance<{ emailToken: string }>({
method: 'POST',
url,
});

return result;
};

const postConfirmUsernameDuplicate = async (username: string) => {
const url = `/verification/username?username=${username}`;

const result = await defaultInstance<{ usernameToken: string }>({
method: 'POST',
url,
});

return result;
};

const postSignupUser = async (userInfo: UserSignUpType) => {
const url = `/users`;

const result = await defaultInstance({
method: 'POST',
url,
data: userInfo,
});

return result;
};

export {
postConfirmCodeByEamil,
postConfirmCode,
postConfirmUsernameDuplicate,
postSignupUser,
};

export type { UserSignUpType };
Loading
Loading