Skip to content

Commit

Permalink
refactor: userId 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
smb0123 committed Dec 29, 2024
1 parent c40b24a commit b59fced
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/components/common/SideBar/SideBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import firebase from 'firebase/compat/app';
import Alarm from '@/icons/alarm.svg';
import AlarmOff from '@/icons/alarm_off.svg';
import 'firebase/compat/messaging';
import { useAlarm, useStoreId, useUserId } from '@/store/useStoreId';
import { useAlarm, useStoreId } from '@/store/useStoreId';
import { useMutation } from '@tanstack/react-query';
import postFcm from './apis/postFcm';
import postUserFcm from './apis/postUserFcm';
Expand All @@ -39,12 +39,12 @@ export default function SideBar() {
const [canPlaySound, setCanPlaySound] = useState(true);
const pathName = usePathname();
const { setAlarm } = useAlarm();
const { userId } = useUserId();
const token = localStorage.getItem('token');
const { storeId } = useStoreId();

const userFcmMutation = useMutation({
// @ts-ignore
mutationFn: () => postUserFcm(userId, storeId),
mutationFn: () => postUserFcm(token, storeId),
});

const fcmMutation = useMutation({
Expand Down Expand Up @@ -75,9 +75,9 @@ export default function SideBar() {
console.log('Notification permission granted.');
messaging
.getToken()
.then((token) => {
.then((fcmToken) => {
// @ts-ignore
fcmMutation.mutate({ userId: userId, fcmToken: token });
fcmMutation.mutate({ token: token, fcmToken: fcmToken });
})
.catch((error) => {
console.error('Error getting token:', error);
Expand Down
17 changes: 12 additions & 5 deletions src/components/common/SideBar/apis/postFcm.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import axiosInstance from '@/apis/axiosInstance';

export default async function postFcm({ userId, fcmToken }) {
const { data } = await axiosInstance.post('fcm', {
userId: userId,
fcmToken: fcmToken,
});
export default async function postFcm({ token, fcmToken }) {
const { data } = await axiosInstance.post(
'fcm',
{
fcmToken: fcmToken,
},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
return data;
}
12 changes: 10 additions & 2 deletions src/components/common/SideBar/apis/postUserFcm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import axiosInstance from '@/apis/axiosInstance';

export default async function postUserFcm(userId, storeId) {
const { data } = await axiosInstance.post(`reservation-new/fcm?storeId=${storeId}&userId=${userId}`);
export default async function postUserFcm(token, storeId) {
const { data } = await axiosInstance.post(
`reservation-new/fcm?storeId=${storeId}`,
{},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
return data;
}
9 changes: 5 additions & 4 deletions src/components/page-layout/MainPageLayout/MainPageLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import { useRouter } from 'next/navigation';
import { useState, useEffect, useCallback } from 'react';
import axiosInstance from '@/apis/axiosInstance';
import ROUTE from '@/constants/route';
import { useStoreId, useUserId } from '@/store/useStoreId';
import { useStoreId } from '@/store/useStoreId';

const cn = classNames.bind(styles);

export default function MainPageLayout() {
const { setStoreId } = useStoreId();
const { setUserId } = useUserId();
const router = useRouter();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
Expand All @@ -48,7 +47,6 @@ export default function MainPageLayout() {
}

setStoreId(userData.storeRef);
setUserId(userData.userid);

alert('로그인에 성공하였습니다.');
router.push(ROUTE.In_Progress);
Expand All @@ -57,7 +55,7 @@ export default function MainPageLayout() {
console.error('로그인 조건 검증 실패:', error);
}
},
[router, setStoreId, setUserId]
[router, setStoreId]
);

useEffect(() => {
Expand All @@ -82,6 +80,7 @@ export default function MainPageLayout() {
await setPersistence(auth, browserLocalPersistence);
const result = await signInWithPopup(auth, provider);
const token = await result.user.getIdToken();
localStorage.setItem('token', token);
await checkUserPermissions(token);
} catch (error) {
alert('Google 로그인에 실패하였습니다. 다시 시도해주세요.');
Expand All @@ -98,6 +97,7 @@ export default function MainPageLayout() {
await setPersistence(auth, browserLocalPersistence);
const result = await signInWithPopup(auth, provider);
const token = await result.user.getIdToken();
localStorage.setItem('token', token);
await checkUserPermissions(token);
} catch (error) {
alert('Apple 로그인에 실패하였습니다. 다시 시도해주세요.');
Expand All @@ -111,6 +111,7 @@ export default function MainPageLayout() {
await setPersistence(auth, browserLocalPersistence);
const result = await signInWithEmailAndPassword(auth, email, password);
const token = await result.user.getIdToken();
localStorage.setItem('token', token);
await checkUserPermissions(token);
} catch (error) {
alert('로그인에 실패하였습니다. 이메일과 비밀번호를 확인해주세요.');
Expand Down
14 changes: 1 addition & 13 deletions src/store/useStoreId.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ const useStoreId = create(
)
);

const useUserId = create(
persist(
(set) => ({
userId: null,
setUserId: (userId) => set({ userId: userId }),
}),
{
name: 'userId',
}
)
);

const useAlarm = create(
persist(
(set) => ({
Expand All @@ -37,4 +25,4 @@ const useAlarm = create(
)
);

export { useStoreId, useUserId, useAlarm };
export { useStoreId, useAlarm };

0 comments on commit b59fced

Please sign in to comment.