-
Notifications
You must be signed in to change notification settings - Fork 3
Feat/101/login signup api #106
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 all commits
7343b4b
950aefe
e92d30f
efea2dd
90b90c0
ad5a1db
ebe4eff
41be5f2
25feb82
5b4097e
541da83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,27 +2,33 @@ import { fetchApi } from '@/src/utils/api'; | |
| import { LoginRequest, LoginResponse, SignupRequest, SignupResponse, User } from '@/src/types/auth'; | ||
|
|
||
| export function signupUser(data: SignupRequest): Promise<{ data: SignupResponse }> { | ||
| return fetchApi<{ data: SignupResponse }>('/signup', { | ||
| return fetchApi<{ data: SignupResponse; headers: Headers }>('/auths/signup', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(data), | ||
| }).then((response) => { | ||
| const token = response.headers.get('Authorization'); | ||
| return { data: { token } }; | ||
| }); | ||
| } | ||
|
|
||
| export function loginUser(data: LoginRequest): Promise<{ data: LoginResponse }> { | ||
| return fetchApi<{ data: LoginResponse }>('/login', { | ||
| return fetchApi<{ data: LoginResponse; headers: Headers }>('/auths/login', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(data), | ||
| }).then((response) => { | ||
| const token = response.headers.get('Authorization'); | ||
| return { data: { token } }; | ||
|
Comment on lines
+18
to
+26
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. π οΈ Refactor suggestion μ½λ μ€λ³΅μ μ κ±°νκ³ μ€λ₯ μ²λ¦¬λ₯Ό κ°μ ν΄μΌ ν©λλ€
λ€μκ³Ό κ°μ κ°μ μ μ μν©λλ€:
const extractAuthToken = (headers: Headers) => {
const token = headers.get('Authorization');
if (!token) {
throw new Error('μΈμ¦ ν ν°μ΄ μμ΅λλ€');
}
return token;
};
}).then((response) => {
- const token = response.headers.get('Authorization');
+ const token = extractAuthToken(response.headers);
return { data: { token } };
}); |
||
| }); | ||
| } | ||
|
|
||
| export function getUser(): Promise<{ data: User }> { | ||
| return fetchApi<{ data: User }>('/user/1', { | ||
| return fetchApi<{ data: User }>('/auths/user', { | ||
| method: 'GET', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,18 +14,21 @@ export default function LoginPage() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { mutate: postSignup } = usePostSignupQuery(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const handleSubmit = async (data: SignupFormValues) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| postSignup(data, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { confirmPassword, ...requestData } = data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| postSignup(requestData, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onSuccess: () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| router.push('/'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onError: (error) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (error.statusCode === 400) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: parameter μ²λ¦¬ ν message μ²λ¦¬ νμΈ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // const { parameter } = error.parameter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // setError(parameter, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // type: 'manual', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // message: error.message, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (error.status === 400) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { validationErrors } = error.detail; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Object.keys(validationErrors).forEach((key) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setError(key as 'email', { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: 'manual', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message: validationErrors[key], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+24
to
+31
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. π οΈ Refactor suggestion μλ¬ μ²λ¦¬ λ‘μ§ κ°μ μ΄ νμν©λλ€ νμ¬ κ΅¬νλ μλ¬ μ²λ¦¬μ λͺ κ°μ§ κ°μ μ΄ νμν©λλ€:
λ€μκ³Ό κ°μ΄ κ°μ νλ κ²μ μ μν©λλ€: if (error.status === 400) {
const { validationErrors } = error.detail;
- Object.keys(validationErrors).forEach((key) => {
- setError(key as 'email', {
- type: 'manual',
- message: validationErrors[key],
- });
- });
+ try {
+ Object.entries(validationErrors).forEach(([key, message]) => {
+ if (key in formMethods.getValues()) {
+ setError(key as keyof SignupFormValues, {
+ type: 'manual',
+ message: String(message),
+ });
+ }
+ });
+ } catch (e) {
+ console.error('μ ν¨μ± κ²μ¬ μλ¬ μ²λ¦¬ μ€ λ¬Έμ κ° λ°μνμ΅λλ€:', e);
+ setError('root', {
+ type: 'manual',
+ message: 'νμκ°μ
μ²λ¦¬ μ€ λ¬Έμ κ° λ°μνμ΅λλ€. λ€μ μλν΄ μ£ΌμΈμ.',
+ });
+ }
+} else {
+ setError('root', {
+ type: 'manual',
+ message: 'μλ² μ€λ₯κ° λ°μνμ΅λλ€. μ μ ν λ€μ μλν΄ μ£ΌμΈμ.',
+ });
}π Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ const meta: Meta = { | |
|
|
||
| export default meta; | ||
| function Template() { | ||
| const { isAuth, login, logout } = useAuthStore(); | ||
| const { isAuth, login, logout, setUser } = useAuthStore(); | ||
|
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. μΈμ¦ λ‘μ§ κ°μ μ΄ νμν©λλ€. λ‘κ·ΈμΈ νλ‘μΈμ€κ° λ λ¨κ³λ‘ λΆλ¦¬λμ΄ μμ΄ μ μ¬μ μΈ λ¬Έμ κ° λ°μν μ μμ΅λλ€. λ€μκ³Ό κ°μ΄ κ°μ νλ κ²μ μ μλ립λλ€: - login(testToken);
- setUser(testUser);
+ await login(testToken);
+ await setUser(testUser);λλ λ λμ λ°©λ²μΌλ‘, μΈμ¦ λ‘μ§μ νλμ ν¨μλ‘ ν΅ν©νλ κ²μ κ³ λ €ν΄λ³΄μΈμ: - login(testToken);
- setUser(testUser);
+ await loginWithUser(testToken, testUser);Also applies to: 37-38 |
||
| const testToken = 'test token'; | ||
| const testUser = { | ||
| id: 1, | ||
|
|
@@ -34,7 +34,8 @@ function Template() { | |
| if (isAuth) { | ||
| logout(); | ||
| } else { | ||
| login(testUser, testToken); | ||
| login(testToken); | ||
| setUser(testUser); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
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.
ν ν° μΆμΆ μ μ€λ₯ μ²λ¦¬ λ‘μ§μ΄ νμν©λλ€
νμ¬ κ΅¬νμμλ λ€μκ³Ό κ°μ μ μ¬μ μΈ λ¬Έμ κ° μμ΅λλ€:
λ€μκ³Ό κ°μ΄ κ°μ νλ κ²μ μ μν©λλ€:
}).then((response) => { const token = response.headers.get('Authorization'); + if (!token) { + throw new Error('μΈμ¦ ν ν°μ΄ μμ΅λλ€'); + } return { data: { token } }; });π Committable suggestion