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

[비밀번호 재설정] api 연결 & 처리 #60

Open
wants to merge 4 commits into
base: feature/login
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/@type/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ declare module AuthType {
interface resetPasswordState {
email?: string;
}

type resetPasswordQuery = {
username: string;
code: string;
};
}

declare module HomeType {
Expand Down
24 changes: 23 additions & 1 deletion src/app/store/ducks/auth/authSlice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { FormState } from "./authThunk.type";
import { getUserInfo, signIn } from "./authThunk";
import {
getUserInfo,
signIn,
resetPassword,
checkCurrentURL,
signInUseCode,
} from "./authThunk";
import { setAccessTokenInAxiosHeaders } from "customAxios";

export interface AuthStateProps {
Expand Down Expand Up @@ -90,6 +96,16 @@ const authSlice = createSlice({
state.isLogin = true;
setAccessTokenInAxiosHeaders(action.payload);
})
.addCase(resetPassword.fulfilled, (state, action) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resetPassword,fulfileed에도, signInUseCode.fulfiled에도 동일한 처리를 하게 되는데 리덕스 툴킷 내에서 재사용할 수 있는 방법을 혹시 아시나요?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러게요.. 뭔가 필요하긴 하겠는데 막상 방법이 떠오르지않네요

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

찾아보니 addMatcherisAllOf 등을 사용하는 방법이 있다네요? 정확히 맞는지는 모르겠지만 한 번 확인해보세요!
스크린샷 2022-09-07 오후 5 06 44

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오오 감사합니다. 한번 찾아볼게요!

state.isLoading = false;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isLoading값으로 로딩처리해주는 부분을 깜빡했네요. 추가하겠습니다!

state.isLogin = true;
setAccessTokenInAxiosHeaders(action.payload);
})
.addCase(signInUseCode.fulfilled, (state, action) => {
state.isLoading = false;
state.isLogin = true;
setAccessTokenInAxiosHeaders(action.payload);
})
.addCase(signIn.rejected, (state, action) => {
state.isAsyncReject = true;
state.isLoading = false;
Expand All @@ -104,6 +120,12 @@ const authSlice = createSlice({
.addCase(getUserInfo.pending, (state) => {})
.addCase(getUserInfo.fulfilled, (state, action) => {
state.userInfo = action.payload;
})
.addCase(resetPassword.rejected, (state) => {
state.errorMessage = `전에 사용한 적 없는 새로운 비밀번호를 만드세요.`;
})
.addCase(checkCurrentURL.rejected, (state) => {
// 유효하지 않은 url **
Copy link
Member Author

@live-small live-small May 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

비밀번호 재설정 페이지에 들어오자마자, query의 code로 유효한 코드인지 체크해서 만료된 코드일 경우 여기로 넘어오는데요, 여기서상태값을 이용해 잘못된 url이거나 삭제된 페이지라는 화면을 보여주려고 합니다.
-> 회의때 공유해서 토의함

});
},
});
Expand Down
61 changes: 51 additions & 10 deletions src/app/store/ducks/auth/authThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,54 @@ export const getUserInfo = createAsyncThunk<AuthType.UserInfo>(
},
);

// export const resetPassword = createAsyncThunk<>(
// "auth/passwordReset",
// async (payload, ThunkOptions) => {
// try {
// // call api
// } catch (error) {
// // error handling
// }
// },
// );
export const checkCurrentURL = createAsyncThunk<
void,
{ code: string; username: string }
>("auth/checkResetPassword", async (payload, ThunkOptions) => {
try {
const config = {
params: {
code: payload.code,
username: payload.username,
},
};
const { data } = await customAxios.get(
`/accounts/password/reset`,
config,
);
console.log(data);
} catch (error) {
throw ThunkOptions.rejectWithValue(error);
}
});

export const resetPassword = createAsyncThunk<
AuthType.Token,
{ code: string; username: string; newPassword: string }
>("auth/resetPassword", async (payload, ThunkOptions) => {
try {
const { data } = await customAxios.put(`/accounts/password/reset`, {
code: payload.code,
username: payload.username,
newPassword: payload.newPassword,
});
return data.data;
} catch (error) {
throw ThunkOptions.rejectWithValue(error);
}
});

export const signInUseCode = createAsyncThunk<
AuthType.Token,
{ code: string; username: string }
>("auth/signInUseCode", async (payload) => {
try {
const { data } = await customAxios.post(`/accounts/login/recovery`, {
code: payload.code,
username: payload.username,
});
return data.data;
} catch {
// 에러나는 경우? username, code가 잘못됐을 때?
}
});
41 changes: 38 additions & 3 deletions src/components/Auth/ResetPassword/ResetPasswordForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import ContentBox from "components/Common/ContentBox";
import styled from "styled-components";
import SubmitButton from "../SubmitButton";
import useInput from "hooks/useInput";
import { useEffect, MouseEvent } from "react";
import { useAppDispatch, useAppSelector } from "app/store/Hooks";
import { checkCurrentURL, resetPassword } from "app/store/ducks/auth/authThunk";

const Container = styled.section`
background-color: #fff;
Expand Down Expand Up @@ -56,13 +59,25 @@ const Container = styled.section`
margin: 20px 52px 60px 52px;
height: 44px;
}

.error-message {
color: #ed4956;
font-size: 14px;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

프로젝트 기본 font-size가 14px 아니었나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// globalStyle.ts
  body, input,textarea ,button {
        font-size: 14px;
        line-height: 18px;
        padding:0;
        margin:0;
    }

globalStyle보니까 그렇네요..!!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네네 저 부분은 중복되서 삭제해도 좋을 것 같아요!

line-height: 18px;
text-align: center;
margin: 10px 40px;
}
}
}
`;

export default function ResetPasswordForm() {
const { search } = useLocation();
const { username, code } = queryString.parse(search);
const { username, code } = queryString.parse(
search,
) as AuthType.resetPasswordQuery;
const dispatch = useAppDispatch();
const { errorMessage } = useAppSelector((state) => state.auth);

const [newPasswordInputProps, newPasswordIsValid] = useInput(
"",
Expand All @@ -76,8 +91,22 @@ export default function ResetPasswordForm() {
(value) => newPasswordInputProps.value === value,
);

// submit api 연결
// 들어오자마자, code유효한지 체크 -> 그 전까지, loding (using useEffect)
useEffect(() => {
dispatch(checkCurrentURL({ code, username }));
}, []);

const resetPasswordClickHandler = (
event: MouseEvent<HTMLButtonElement>,
) => {
event.preventDefault();
dispatch(
resetPassword({
code,
username,
newPassword: newPasswordInputProps.value,
}),
);
};

return (
<Container>
Expand Down Expand Up @@ -123,9 +152,15 @@ export default function ResetPasswordForm() {
reEnterPasswordIsValid
)
}
onClick={resetPasswordClickHandler}
>
비밀번호 재설정
</SubmitButton>
{errorMessage && (
<div className="error-message">
<p>{errorMessage}</p>
</div>
)}
</form>
</ContentBox>
</div>
Expand Down
11 changes: 11 additions & 0 deletions src/pages/Auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { useAppDispatch } from "app/store/Hooks";
import Form from "components/Auth/Form";
import { Footer } from "components/Common/Footer/Footer";
import { useEffect } from "react";
import { useLocation } from "react-router-dom";
import styled from "styled-components";
import queryString from "query-string";
import { signInUseCode } from "app/store/ducks/auth/authThunk";

const Section = styled.section`
flex-shrink: 0;
Expand All @@ -19,7 +22,15 @@ const Section = styled.section`

export default function AuthPage(props: { router: "signIn" | "signUp" }) {
const dispatch = useAppDispatch();
const { search } = useLocation();
const { username, code } = queryString.parse(
search,
) as AuthType.resetPasswordQuery;

useEffect(() => {
if (props.router === "signIn") {
dispatch(signInUseCode({ username, code }));
}
dispatch(authAction.changeFormState(props.router));
}, []);

Expand Down