亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查用戶是否在 PrivateRoute 組件 React 上登錄

檢查用戶是否在 PrivateRoute 組件 React 上登錄

開心每一天1111 2021-11-04 15:35:29
我正在使用我的 Rails Api(DoorKeeper/Devise)上的用戶身份驗證制作我的第一個反應應用程序。我設置了expires_at反應local_storage并嘗試在私有路由組件中進行驗證。在這種情況下,我想驗證用戶令牌以讓他訪問路由“/”。我無法聯系到 userSignedIn。如何從 PrivateRoute 組件訪問此功能?// PrivateRoute.jsimport React from 'react';import { Route, Redirect } from 'react-router-dom'import userSignedIn from '../auth'const PrivateRoute = ({ component: Component, ...rest }) => (  <Route {...rest} render={(props) => (    userSignedIn === true      ? <Component {...props} />      : <Redirect to={{          pathname: '/login',          state: { from: props.location }        }} />  )} />)export default PrivateRoute;// auth.jsconst userSignedIn = () => {  if (localStorage.getItem('expires_at') !== null) {    return new Date() < new Date(localStorage.getItem('expires_at'))  } else {    return false  }}export default userSignedIn;謝謝
查看完整描述

1 回答

?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

還沒有時間測試這個,但是


const PrivateRoute = ({ component: Component, ...rest }) => (

  <Route {...rest} render={(props) => (

    userSignedIn() === true

      ? <Component {...props} />

      : <Redirect to={{

          pathname: '/login',

          state: { from: props.location }

        }} />

  )} />

)

您需要調用該函數,userSignedIn()因為它是返回值的內容true/false(這是此處唯一的更改)userSignedIn === true->userSignedIn() === true


查看完整回答
反對 回復 2021-11-04
  • 1 回答
  • 0 關注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號