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

feat: api-instance 수정 및 로그아웃 api 연결 #56

Merged
merged 3 commits into from
Nov 7, 2023
Merged
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
11 changes: 0 additions & 11 deletions src/api/baseInstance.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/api/service/UserService.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import apiV1Instance from '../api-instance';
import baseInstance from '../baseInstance';

export class UserService {
public static login = async (
email: string,
password: string
): Promise<void> => {
const response = await baseInstance.post('/auth/login', {
const response = await apiV1Instance.post('/auth/login', {
email,
password,
});
Expand All @@ -18,7 +17,7 @@ export class UserService {
password: string,
username: string
): Promise<void> => {
await baseInstance.post('/auth/signup', { email, password, username });
await apiV1Instance.post('/auth/signup', { email, password, username });
};

public static logout = async (): Promise<void> => {
Expand Down
5 changes: 3 additions & 2 deletions src/assets/data/token.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const token =
'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJrYW5nQGdtYWlsLmNvbSIsImF1dGgiOiJVU0VSIiwiaWF0IjoxNjk3NjM1OTIzLCJleHAiOjE2OTc2MzY1MjN9.ciSIhzjz4hv0MlVclVkIwvEs9ZzH7_RhcLpCr6fv1LK7EOEBDdN-Bs1nQKDMzNBVVhWkOG8ERCVzOR4aJDGqhA';
import useUserTokenStore from 'src/utils/zustand/user/UserTokenStore';

export const token = useUserTokenStore.getState().userToken;
4 changes: 3 additions & 1 deletion src/components/User/LoginBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import router from 'next/router';
import { useState } from 'react';
import useCheckAuth from 'src/api/hook/CheckAuthHook';
import { useLogin } from 'src/api/hook/UserHook';
import useUserTokenStore from 'src/utils/zustand/user/UserTokenStore';

import logoUrl from '../../../public/title-logo.png';
import InputBox from './InputBox';
Expand All @@ -14,6 +15,7 @@ import UserLabel from './UserLabel';
export default function LoginBox() {
const [email, setEmail] = useState<string>('');
const [pw, setPw] = useState<string>('');
const { setUserToken } = useUserTokenStore();

useCheckAuth();

Expand All @@ -25,9 +27,9 @@ export default function LoginBox() {
}

const { mutate: login } = useLogin(email, pw, (response) => {
// document.cookie = `token = ${response.data.accessToken}`;
setCookie('token', response.data.accessToken, 1);
router.push('/');
setUserToken(response.data.accessToken);
});

const handleSubmit = async () => {
Expand Down
8 changes: 7 additions & 1 deletion src/components/common/AvatarModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Button, Dialog, DialogContent, Divider } from '@mui/material';
import React, { useState } from 'react';
import { useLogout } from 'src/api/hook/UserHook';
import useUserTokenStore from 'src/utils/zustand/user/UserTokenStore';

interface AvatarModalProps {
open: boolean;
Expand All @@ -9,9 +11,13 @@ interface AvatarModalProps {
function AvatarModal({ open, onClose }: AvatarModalProps) {
const [logoutOpen, setLogoutOpen] = useState(false);
const [settingsOpen, setSettingsOpen] = useState(false);
const { setUserToken } = useUserTokenStore();

const { mutate: logout } = useLogout();

const handleLogoutOpen = () => {
setLogoutOpen(true);
setUserToken('');
logout();
};

const handleSettingsOpen = () => {
Expand Down
21 changes: 21 additions & 0 deletions src/utils/zustand/user/UserTokenStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { create } from 'zustand';
import { persist } from 'zustand/middleware';

interface UserTokenState {
userToken: string;
setUserToken: (token: string) => void;
}

const useUserTokenStore = create<UserTokenState>()(
persist(
(set) => ({
userToken: '',
setUserToken: (token: string) => set(() => ({ userToken: token })),
}),
{
name: 'userTokenStore',
}
)
);

export default useUserTokenStore;
Loading