Skip to content

Commit

Permalink
fix: resolve 401 error
Browse files Browse the repository at this point in the history
  • Loading branch information
jungwoo3490 committed Aug 12, 2024
1 parent d04aa25 commit 1dc382d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Login/hooks/usePostLoginToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const usePostLoginToken = () => {
const { tokenDto, nickname } = data;

if (nickname === null) {
navigate('/register', { state: tokenDto.accessToken });
navigate('/register', { state: { tempToken: tokenDto.accessToken } });
} else {
sessionStorage.setItem('token', tokenDto.accessToken);
sessionStorage.setItem('nickname', nickname);
Expand Down
3 changes: 2 additions & 1 deletion src/Register/api/patchNickname.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { api } from '../../libs/api';

export const patchNickname = async (nickname: string) => {
export const patchNickname = async (nickname: string, tempToken: string) => {
const response = await api().patch(
'/api/nickname',
{ nickname: nickname },
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${tempToken}`,
},
},
);
Expand Down
5 changes: 3 additions & 2 deletions src/Register/hooks/usePatchNickname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import {
const usePatchNickname = (props: usePatchNicknameProps) => {
const { handleSetIsValid, handleSetIsActive, nickname } = props;
const { state } = useLocation();
const tempToken = state.tempToken;

const navigate = useNavigate();

const mutation = useMutation({
mutationFn: async ({ nickname }: patchNicknameProps) => {
return await patchNickname(nickname);
return await patchNickname(nickname, tempToken);
},
onError: (err: AxiosError) => {
const code = err.response?.status;
Expand All @@ -32,7 +33,7 @@ const usePatchNickname = (props: usePatchNicknameProps) => {
}
},
onSuccess: () => {
sessionStorage.setItem('token', state);
sessionStorage.setItem('token', state.tempToken);
sessionStorage.setItem('nickname', nickname);
navigate('/', { state: { step: 1 } });
},
Expand Down

0 comments on commit 1dc382d

Please sign in to comment.