Skip to content

Commit 894fba9

Browse files
committed
feat: 비밀번호 변경 커스텀 훅 개발
1 parent c7d368a commit 894fba9

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

src/apis/authApis.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ export const signIn = async ({ email, password }: SignInParams) => {
1515

1616
return data;
1717
};
18+
19+
export const isLogIn = async () => {
20+
const { data, error } = await supabase.auth.getSession();
21+
if (error) {
22+
throw error;
23+
}
24+
return data;
25+
};

src/react-queries/queryKeys.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { createQueryKeyStore } from '@lukemorales/query-key-factory';
22

3-
export const queries = createQueryKeyStore({});
3+
export const queries = createQueryKeyStore({
4+
auth: {
5+
isLogin: null,
6+
},
7+
});

src/react-queries/useUpdateUser.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { isLogIn } from '@/apis/authApis';
2+
import updateUser from '@/apis/updateUserApi';
3+
import { useChangePasswordState } from '@/stores/changePasswordStore';
4+
import supabase from '@/supabase';
5+
import { LooseValidation, ValidateProcessor } from '@/utils/authUtils';
6+
import { useMutation } from '@tanstack/react-query';
7+
import { useRef, useState } from 'react';
8+
import { useNavigate } from 'react-router-dom';
9+
10+
interface DialogElement {
11+
openModal: () => void;
12+
closeModal: () => void;
13+
}
14+
15+
const useUpdateUser = () => {
16+
const { mutate, isPending } = useMutation({
17+
mutationKey: ['updateUser'],
18+
mutationFn: updateUser,
19+
});
20+
const [dialogMessage, setDialogMessage] = useState('');
21+
const { password } = useChangePasswordState();
22+
const dialogRef = useRef<DialogElement | null>(null);
23+
const navigate = useNavigate();
24+
25+
const validator = new ValidateProcessor(new LooseValidation());
26+
27+
const onClick = () => {
28+
if (!validator.isValidPassword(password)) {
29+
setDialogMessage('유효하지 않은 패스워드 입니다.');
30+
dialogRef.current?.openModal();
31+
}
32+
33+
mutate(password, {
34+
onSuccess: async () => {
35+
const { session } = await isLogIn();
36+
if (session) {
37+
await supabase.auth.signOut();
38+
}
39+
navigate('/login');
40+
},
41+
onError: (e) => {
42+
setDialogMessage(e.message);
43+
dialogRef.current?.openModal();
44+
},
45+
});
46+
};
47+
return {
48+
isPending,
49+
onClick,
50+
dialogRef,
51+
dialogMessage,
52+
};
53+
};
54+
55+
export default useUpdateUser;

0 commit comments

Comments
 (0)