• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    미해결

로그인 성공시 /workspace/channel 로 이동이 안되고 있습니다.

23.04.28 09:59 작성 조회수 436

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

Min Uk Jo님의 프로필

Min Uk Jo

질문자

2023.04.28

말씀 주신 부분을 수정후에는 URL로 강제 진입은 성공했습니다.

그러고 쿠키를 삭제하고 난 뒤에 로그인 버튼을 눌렀을 때 응답은 잘 오고 있는데 redirect가 반응을 하지 않아 화면 전환이 되지 않고 있습니다..

data를 console.log 찍어보세요

fetcher에 return 빠뜨리신건 아닌가요?

Min Uk Jo님의 프로필

Min Uk Jo

질문자

2023.04.28

감사합니다ㅠㅠ 해결되었습니다 !!

0

일단 콘솔 탭에 에러는 없나요?

localhost:3090/signup 입력하면 회원가입 페이지는 뜨나요?

Min Uk Jo님의 프로필

Min Uk Jo

질문자

2023.04.28

회원가입 페이지는 잘 뜨고 있습니다.

그리고 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

이러한 에러가 표시되고 있습니다.

index.html에서 주소를 ./dist/app.js 하신 것 같은데 /dist/app.js 하세요