Xác thực đăng nhập trong Reactjs

Chào các bác , hiện tại em đăng bị vấn đề xác thực đăng nhập, vấn đề của em đăng gặp phải là sau khi đăng nhập thành công nó không tự nhảy qua trang dashboard , em dùng thử các cách chuyển trang như trong code login mà chưa được , em không biết có bác nào cao kiến chỉ giúp em với ạ. Em cám ơn
Đây là code login

  login(event) {
    fetch('https:///Api/User/Login', {
      method: 'post',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        Username: this.state.Username,
        Password: this.state.Password
      })
    }).then((Response) => Response.json())
      .then((result) => {
        if (result.Status === 'Success') {
          localStorage.setItem('token', result.Message);
          alert('Đăng nhập thành công !!!');
          // this.props.history.push("/views/dashboard/Dashboard");
          // window.location.href = "/#/dashboard";  
        }
        else {
          alert('Sai thông tin tài khoản');
        }
      })
  }

Đây là code app

const cotokenhaychua = localStorage.getItem('token');
function App () {
  if(!cotokenhaychua){
    return <Login/>
  }
    return (
      <HashRouter>
          <React.Suspense fallback={loading}>
            <Switch>
              <Route exact path="/">
                <Redirect to="/Login" />
              </Route>
              
              <Route path='/Login' component={Login} exact />
              <Route path="/" name="Home" render={props => <TheLayout {...props}/>} />
            </Switch>
          </React.Suspense>
      </HashRouter>
      
    );
  }

https://www.pluralsight.com/guides/how-to-router-redirect-after-login

1 Like

Em giải quyết vấn đề bằng cách thêm

window.location.reload()

sau khi alert đăng nhập thành công

3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?