작성
·
514
0
App.tsx
import React, { FC } from 'react';
import loadable from '@loadable/component';
import { Switch, Route, Redirect } from 'react-router';
const Login = loadable(() => import('@pages/Login'));
const SignUp = loadable(() => import('@pages/SignUp'));
const Channel = loadable(() => import('@pages/Channel'));
const App: FC = () => {
return (
<Switch>
<Redirect exact path="/" to="/login" />
<Route path="/login" component={Login} />
<Route path="/signUp" component={SignUp} />
<Route path="/workspace/channel" component={Channel} />
</Switch>
);
};
export default App;
Login/ index.tsx
import React, { useState, useCallback } from 'react';
import useInput from '@pages/hooks/useinput';
import axios from 'axios';
import { Error, Form, Label, Input, LinkContainer, Button, Header } from '@pages/SignUp/styles';
import { Link, Redirect } from 'react-router-dom';
import useSWR from 'swr';
import fetcher from '@utils/fetcher';
const Login = () => {
const { data, error, revalidate } = useSWR('http://localhost:3095/api/users', fetcher);
//주소를 fetcher로 옮겨주고 실제로 주소를 어떻게 처리할지 정해줌
const [logInError, setLogInError] = useState(false);
const [email, onChangeEmail] = useInput('');
const [password, onChangePassword] = useInput('');
const onSubmit = useCallback(
(e) => {
e.preventDefault();
setLogInError(false);
axios
.post(
'http://localhost:3095/api/users/login',
{ email, password },
{
withCredentials: true,
},
)
.then(() => {
revalidate();
})
.catch((error) => {
setLogInError(error.response?.data?.statusCode === 401);
});
},
[email, password],
);
if (data) {
return <Redirect to="/workspace/channel" />;
}
Channel / index.tsx
import Workspace from '@layouts/Workspace';
import React from 'react';
const Channel = () => {
return (
<Workspace>
<div>로그인을 축하합니다.</div>
</Workspace>
);
};
export default Channel;
Workspace.tsx
import React, { FC, useCallback } from 'react';
import useSWR from 'swr';
import fetcher from '@utils/fetcher';
import axios from 'axios';
import { Redirect } from 'react-router';
const Workspace: FC = ({ children }) => {
const { data, error, revalidate } = useSWR('http://localhost:3095/api/users', fetcher);
const onLogout = useCallback(() => {
axios
.post('http://localhost:3095/api/users/logout', null, {
withCredentials: true,
})
.then(() => {
revalidate();
});
}, []);
if (!data) {
return <Redirect to="/login" />;
}
return (
<div>
<button onClick={onLogout}>로그아웃</button>
{children}
</div>
);
};
export default Workspace;
현재 코드에서 로그인 성공까지 되는데 페이지 이동이 없습니다... 하나하나 빠짐없이 확인 해봤는데 제가 찾지 못하는거 같습니다 ㅠㅠ
더군다나 URl 주소를 다이렉트로 localhost:3090/workspace/channel 입력해도 아무런 창이 뜨지 않고 있어서 어떤 문제가 있는지 확인이 어렵습니다.. 도움을 부탁드려요
답변 2
0
회원가입 페이지는 잘 뜨고 있습니다.
그리고 workspace/channel 의 콘솔창에는
p://localhost:3090/workspace/dist/app.js net::ERR_ABORTED 404 (Not Found)
channel:1 Refused to execute script from 'http://localhost:3090/workspace/dist/app.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
channel:40 GET https://a.slack-edge.com/bv1-8/slack-icons-v2-40cccfd.woff2 net::ERR_ABORTED 403
channel:33 GET https://a.slack-edge.com/bv1-8/client-boot-styles.57e47b5.css 403
이러한 에러가 표시되고 있습니다.
data를 console.log 찍어보세요