Skip to content
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
6 changes: 5 additions & 1 deletion src/apis/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axiosHelper from '@/utils/network/axiosHelper';
import { LoginFormData, LoginResponse } from './types';
import { LoginFormData, LoginResponse, PutPasswordFormData, PutPasswordResponse } from './types';
import { isAxiosError } from 'axios';
import { isError } from 'es-toolkit/compat';

Expand All @@ -12,3 +12,7 @@ export const login = async (loginFormData: LoginFormData): LoginResponse => {
return { message: isError(error) ? error.message : String(error) };
}
};

export const putPassword = async (putPasswordFormData: PutPasswordFormData): PutPasswordResponse => {
await axiosHelper.put('/auth/password', putPasswordFormData);
};
25 changes: 21 additions & 4 deletions src/apis/auth/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { z } from 'zod';
import { LOGIN_FORM_VALID_LENGTH, LOGIN_FORM_ERROR_MESSAGE } from '@/constants/auth';
import { LOGIN_FORM_VALID_LENGTH, LOGIN_FORM_ERROR_MESSAGE, PASSWORD_PUT_FORM_VALID_LENGTH, PASSWORD_PUT_FORM_ERROR_MESSAGE } from '@/constants/auth';
import { User } from '@/apis/users/types';

interface FailResponse {
message: string;
}

export const loginSchema = z.object({
email: z.string().min(LOGIN_FORM_VALID_LENGTH.EMAIL.MIN, LOGIN_FORM_ERROR_MESSAGE.EMAIL.MIN).email(LOGIN_FORM_ERROR_MESSAGE.EMAIL.NOT_FORM),
password: z.string().min(LOGIN_FORM_VALID_LENGTH.PASSWORD.MIN, LOGIN_FORM_ERROR_MESSAGE.PASSWORD.MIN),
Expand All @@ -14,8 +18,21 @@ export interface LoginSuccessResponse {
accessToken: string;
}

export interface LoginFailResponse {
message: string;
}
export type LoginFailResponse = FailResponse;

export type LoginResponse = Promise<LoginSuccessResponse | LoginFailResponse>;

export const passwordSchema = z
.object({
password: z.string().min(PASSWORD_PUT_FORM_VALID_LENGTH.PASSWORD.MIN, PASSWORD_PUT_FORM_ERROR_MESSAGE.PASSWORD.MIN),
newPassword: z.string().min(PASSWORD_PUT_FORM_VALID_LENGTH.NEW_PASSWORD.MIN, PASSWORD_PUT_FORM_ERROR_MESSAGE.NEW_PASSWORD.MIN),
newPasswordConfrim: z.string(),
})
.refine((check) => check.newPassword === check.newPasswordConfrim, {
message: PASSWORD_PUT_FORM_ERROR_MESSAGE.NEW_PASSWORD_CONFRIM.NOT_MATCH,
path: ['newPasswordConfirm'],
});

export type PutPasswordFormData = z.infer<typeof passwordSchema>;

export type PutPasswordResponse = Promise<void>;
21 changes: 20 additions & 1 deletion src/apis/users/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axiosHelper from '@/utils/network/axiosHelper';
import { SignupFormData, SignupResponse } from './types';
import { CreateProfileImageForm, CreateProfileImageSuccessResponse, GetUserResponse, SignupFormData, SignupResponse, UpdateUserForm, User } from './types';
import { isAxiosError } from 'axios';
import { isError } from 'es-toolkit/compat';

Expand All @@ -14,3 +14,22 @@ export const signup = async (signupFormData: SignupFormData): SignupResponse =>
};
}
};

export const getUser = async (): GetUserResponse => {
const response = await axiosHelper.get('/users/me');
return response.data;
};

export const updateUser = async (updateUserForm: UpdateUserForm) => {
const response = await axiosHelper.put<User>('/users/me', updateUserForm);
return response.data;
};

export const createProfileImage = async (createProfileImageForm: CreateProfileImageForm) => {
const response = await axiosHelper.post<CreateProfileImageSuccessResponse>('/users/me/image', createProfileImageForm, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
return response.data;
};
47 changes: 39 additions & 8 deletions src/apis/users/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { z } from 'zod';
import { SINGUP_FORM_VALID_LENGTH, SIGNUP_FORM_ERROR_MESSAGE } from '@/constants/auth';
import { SIGNUP_FORM_VALID_LENGTH, SIGNUP_FORM_ERROR_MESSAGE } from '@/constants/auth';

interface FailResponse {
message: string;
}

export const signupSchema = z
.object({
email: z.string().min(SINGUP_FORM_VALID_LENGTH.EMAIL.MIN, SIGNUP_FORM_ERROR_MESSAGE.EMAIL.MIN).email(SIGNUP_FORM_ERROR_MESSAGE.EMAIL.NOT_FORM),
nickname: z.string().min(SINGUP_FORM_VALID_LENGTH.NICKNAME.MIN, SIGNUP_FORM_ERROR_MESSAGE.NICKNAME.MIN).max(SINGUP_FORM_VALID_LENGTH.NICKNAME.MAX, SIGNUP_FORM_ERROR_MESSAGE.NICKNAME.MAX),
password: z.string().min(SINGUP_FORM_VALID_LENGTH.PASSWORD.MIN, SIGNUP_FORM_ERROR_MESSAGE.PASSWORD.MIN),
email: z.string().min(SIGNUP_FORM_VALID_LENGTH.EMAIL.MIN, SIGNUP_FORM_ERROR_MESSAGE.EMAIL.MIN).email(SIGNUP_FORM_ERROR_MESSAGE.EMAIL.NOT_FORM),
nickname: z.string().min(SIGNUP_FORM_VALID_LENGTH.NICKNAME.MIN, SIGNUP_FORM_ERROR_MESSAGE.NICKNAME.MIN).max(SIGNUP_FORM_VALID_LENGTH.NICKNAME.MAX, SIGNUP_FORM_ERROR_MESSAGE.NICKNAME.MAX),
password: z.string().min(SIGNUP_FORM_VALID_LENGTH.PASSWORD.MIN, SIGNUP_FORM_ERROR_MESSAGE.PASSWORD.MIN),
passwordConfirm: z.string(),
terms: z.boolean(),
})
Expand All @@ -24,15 +28,42 @@ export interface User {
id: number;
email: string;
nickname: string;
profileImageUrl: string | null;
profileImageUrl: string | null | URL;
createdAt: string | Date;
updatedAt: string | Date;
}

export type SignupSuccessResponse = User;

export interface SignupFailResponse {
message: string;
}
export type SignupFailResponse = FailResponse;

export type SignupResponse = Promise<SignupSuccessResponse | SignupFailResponse>;

export type GetUserResponse = Promise<User>;

type ProfileImageUrl = string | URL | null;

export const updateUserFormSchema = z.object({
nickname: z.string(),
profileImageUrl: z.string(),
});

export type UpdateUserForm = Omit<z.infer<typeof updateUserFormSchema>, 'profileImageUrl'> & {
profileImageUrl: ProfileImageUrl;
};

const profileImageUrlSchema = z.instanceof(File).refine((file) => ['image/jpeg', 'image/jpg', 'image/png', 'image/svg+xml'].includes(file.type), {
message: '지원되지 않는 이미지 파일입니다.',
});

export const createProfileImageFormSchema = z.object({
image: profileImageUrlSchema,
});

export interface CreateProfileImageForm {
image: File;
}

export type CreateProfileImageSuccessResponse = {
profileImageUrl: Exclude<ProfileImageUrl, null>;
};
23 changes: 22 additions & 1 deletion src/constants/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const SIGNUP_FORM_PLACEHOLDER = {
PASSWORD_CONFIRM: '비밀번호를 한번 더 입력해 주세요',
} as const;

export const SINGUP_FORM_VALID_LENGTH = {
export const SIGNUP_FORM_VALID_LENGTH = {
EMAIL: {
MIN: 1,
},
Expand Down Expand Up @@ -61,3 +61,24 @@ export const LOGIN_FORM_ERROR_MESSAGE = {
MIN: '8자 이상 작성해 주세요',
},
} as const;

export const PASSWORD_PUT_FORM_VALID_LENGTH = {
PASSWORD: {
MIN: 8,
},
NEW_PASSWORD: {
MIN: 8,
},
} as const;

export const PASSWORD_PUT_FORM_ERROR_MESSAGE = {
PASSWORD: {
MIN: '8자 이상 입력해 주세요',
},
NEW_PASSWORD: {
MIN: '8자 이상 입력해 주세요',
},
NEW_PASSWORD_CONFRIM: {
NOT_MATCH: '비밀번호가 일치하지 않습니다',
},
} as const;
Loading