-
Notifications
You must be signed in to change notification settings - Fork 1
[feat] integrate user api for mypage #14
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
Changes from 3 commits
bf1424e
84291cf
8a1331e
82b0048
4b69065
46be792
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,36 @@ | ||
| import { useEffect } from 'react'; | ||
| import { ChangeEvent, useState } from 'react'; | ||
| import Image from 'next/image'; | ||
| import { UseFormSetValue } from 'react-hook-form'; | ||
| import { FormValues } from './ProfileForm'; | ||
| import { UseFormSetValue, FieldValues, Path, PathValue } from 'react-hook-form'; | ||
| import styles from './FileInput.module.css'; | ||
|
|
||
| interface FileInputProps { | ||
| name: 'imgFile'; | ||
| setValue: UseFormSetValue<FormValues>; | ||
| interface FileInputProps<T extends FieldValues> { | ||
| name: Path<T>; | ||
| setValue: UseFormSetValue<T>; | ||
| url?: string | null; | ||
| } | ||
|
|
||
| export default function FileInput({ name, setValue }: FileInputProps) { | ||
| const [preview, setPreview] = useState(''); | ||
| export default function FileInput<T extends FieldValues>({ | ||
| name, | ||
| setValue, | ||
| url, | ||
| }: FileInputProps<T>) { | ||
| const [preview, setPreview] = useState<string | null>(null); | ||
|
|
||
| const handleChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
| const file = event.target.files?.[0]; | ||
| if (file) { | ||
| setPreview(URL.createObjectURL(file)); | ||
| setValue('image', file); | ||
| setValue(name, file as PathValue<T, Path<T>>); | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use client μ΅μμμ μ μΈ μν΄μ€λ λμνλ보ꡰμ?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λΆλͺ¨μ»΄ν¬λνΈκ° 'use client' λ‘ λμ΄μμΌλ©΄ νμ μκΈ΄ νλ°... |
||
| if (url) { | ||
| setPreview(url); | ||
| } | ||
| }, [url]); | ||
|
|
||
| return ( | ||
| <> | ||
| <label className={styles.label} htmlFor={name}> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| } | ||
|
|
||
| .button { | ||
| line-height: 24px; | ||
| margin-top: 24px; | ||
| padding: 15px 113.5px; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| } | ||
|
|
||
| .input:read-only { | ||
| color: var(--gray-400); | ||
| outline: none; | ||
| } | ||
|
|
||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export const ERROR_MESSAGES = { | ||
| CURRENT_PASSWORD_INCORRECT: 'νμ¬ λΉλ°λ²νΈκ° νλ Έμ΅λλ€.', | ||
| PASSWORDS_MATCH: 'λΉλ°λ²νΈκ° μΌμΉνμ§ μμ΅λλ€.', | ||
| SAME_AS_OLD_PASSWORD: 'κΈ°μ‘΄ λΉλ°λ²νΈμ λμΌν©λλ€.', | ||
| PASSWORD_TOO_SHORT: 'λΉλ°λ²νΈλ₯Ό 8μ μ΄μ μ λ ₯ν΄μ£ΌμΈμ.', | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import useAuthStore from '../store/authStore'; | ||
| import axios from '../lib/axios'; | ||
|
|
||
| const useAuth = () => { | ||
| const { user, accessToken, setUser, setAccessToken } = useAuthStore(); | ||
| const isAuthenticated = !!accessToken; | ||
|
|
||
| const getMe = async () => { | ||
| try { | ||
| const response = await axios.get('/users/me'); | ||
| console.log('user:', response.data); | ||
| setUser(response.data); | ||
| } catch (error) { | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| const login = async () => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ΄κ±΄ κ°λ° μμμ©μΈκ±ΈκΉμ?! |
||
| try { | ||
| const response = await axios.post('/auth/login', { | ||
| email: 'bono@example.com', | ||
| password: '12341234', | ||
| }); | ||
| setAccessToken(response.data.accessToken); | ||
| } catch (error) { | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| const logout = async () => { | ||
| const setAccessToken = useAuthStore.getState().setAccessToken; | ||
|
|
||
| setAccessToken(null); | ||
| }; | ||
|
|
||
| return { | ||
| user, | ||
| accessToken, | ||
| isAuthenticated, | ||
| getMe, | ||
| login, | ||
| logout, | ||
| }; | ||
| }; | ||
|
|
||
| export default useAuth; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ¬κΈ°μ νν΄μ§λ λ΄μ©μ보면 profileμ΄λ λΉλ°λ²νΈ μ
λ°μ΄νΈ νλ apiνΈμΆμ νλκ² κ°μλ°
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. νμΌλͺ μλν΄ νλ² λ μκ°ν΄λ³΄κ² μ΅λλ€.. μ΄λ ΅λ€μ π€ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import axios from './axios'; | ||
| import { UseFormSetError } from 'react-hook-form'; | ||
| import useAuthStore from '../store/authStore'; | ||
| import { ERROR_MESSAGES } from '../constants/message'; | ||
| import { ProfileFormValues } from '../components/ProfileForm'; | ||
| import { PasswordFormValues } from '../components/PasswordForm'; | ||
|
|
||
| export const updateProfile = async (data: ProfileFormValues) => { | ||
| const { image, nickname } = data; | ||
| const setUser = useAuthStore.getState().setUser; | ||
|
|
||
| let url = null; | ||
| try { | ||
| if (image) { | ||
| const formData = new FormData(); | ||
| formData.append('image', image); | ||
| const response = await axios.post('/users/me/image', formData, { | ||
| headers: { | ||
| 'Content-Type': 'multipart/form-data', | ||
| }, | ||
| }); | ||
| url = response.data.profileImageUrl; | ||
| } | ||
| const response = await axios.put('/users/me', { | ||
| nickname, | ||
| ...(url && { profileImageUrl: url }), | ||
| }); | ||
| setUser(response.data); | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| } | ||
|
Comment on lines
+30
to
+31
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μλ΄μ©μ μμ§ μμ±μμ§κ±°μ£ ?γ γ |
||
| } | ||
| }; | ||
|
|
||
| export const updatePassword = async ( | ||
| data: PasswordFormValues, | ||
| reset: () => void, | ||
| setError: UseFormSetError<PasswordFormValues> | ||
| ) => { | ||
| try { | ||
| await axios.put('/auth/password', { | ||
| password: data.currentPassword, | ||
| newPassword: data.newPassword, | ||
| }); | ||
| reset(); | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| if (error.message === ERROR_MESSAGES.CURRENT_PASSWORD_INCORRECT) { | ||
| setError('currentPassword', { | ||
| type: 'manual', | ||
| message: error.message, | ||
| }); | ||
| } | ||
| if (error.message === ERROR_MESSAGES.SAME_AS_OLD_PASSWORD) { | ||
| setError('newPassword', { | ||
| type: 'manual', | ||
| message: error.message, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΄κ±° λμ΄ κ°μ΄ μμΌλ©΄ μΆ©λλμ κ²½κ³ λ°μ€μ΄ μ΄μνκ² λ¨λλΌκ³ μ
μ ν¬ μ΄μ°¨νΌ typescriptλ§ μ¬μ©νλκΉ μμ건 μ§μ μ΅λλ€.