简体   繁体   English

反应路由器 dom v6 Auth

[英]React router dom v6 Auth

I am having the hardest time trying to understand the react-router Auth.我很难理解 react-router Auth。 I have read the (horrible) documentation numerous times and i have followed MANY tutorials on the web.我已经多次阅读(可怕的)文档,并且我遵循了许多关于 web 的教程 Not a single one has worked.没有一个有效。 The example provided by React is written in typescript which makes it even harder to understand. React 提供的示例是用 typescript 编写的,这使得它更难理解。 Please help me figure this out.请帮我解决这个问题。

They (React Dev Tutorial) use context and providers.他们(React Dev Tutorial)使用上下文和提供者。 But i cant seem to understand that either.但我似乎也无法理解。 IMO its very very convoluted unlike the rest of React-router.与 React-router 的 rest 不同,IMO 非常复杂。

When i run the login function from auth.js it seems to work and then it just resets the hook to false and never loads the next page.当我从 auth.js 运行登录 function 时,它似乎可以工作,然后它只是将钩子重置为 false 并且永远不会加载下一页。 I feel like im so close but there is something wrong with the auth.js file?我觉得我很接近但是 auth.js 文件有问题吗?

app.js应用程序.js

function RequireAuth({children}) {
  let location = useLocation();
  const { authed } = UseAuth();

  console.log(authed);


  return authed ? children : <Navigate to="/" state={{from: location}}/>;
}
<Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <BrowserRouter location={history.location} navigator={history}>
          <AuthProvider>
          <Routes>

          <Route index element={<Login setData={setData} type={type}/>} />


              <Route path="adminDashboard" element={
                <RequireAuth redirectTo="/"> 
                  <AdminDashboard />
                </RequireAuth>
                } 
              />
              

              <Route path="userDashboard" element={
                <RequireAuth redirectTo="/"> 
                  <UserDashboard />
                </RequireAuth>
              } />
           </Routes>
          </AuthProvider>
        </BrowserRouter>
      </PersistGate>
    </Provider>

login.js登录.js

  const auth = UseAuth();
  auth.login();

auth.js the auth.js file came from a seperate tutorial auth.js auth.js 文件来自一个单独的教程

import * as React from "react";

const authContext = React.createContext({
  authed: false,
  login: () => {},
  logout: () => {}
});

export default function UseAuth() {
  return React.useContext(authContext);
}

export function AuthProvider({ children }) {
  const [authed, setAuthed] = React.useState(false);
  let auth = UseAuth();

  const login = () => setAuthed(true, console.log(authed));
  const logout = () => setAuthed(false);

  let value = { authed, login, logout };


  return (
    <authContext.Provider value={value}>
      {children}
    </authContext.Provider>
  );
}

the useAuth returns 3 things authed, login() and logout(). useAuth 返回 3 个经过身份验证的东西,login() 和 logout()。 so when u are calling let auth = useAuth() , auth is an object that contains all three of those.所以当你调用let auth = useAuth()时,auth 是一个包含所有这三个的 object。

Try deconstructing to useAuth returns like尝试解构到 useAuth 返回,例如

const { authed, login, logout } = useAuth()

and use the authed instead并改用 authed

You do appear to be close.你似乎很接近。 The useAuth hook returns an object { authed, login, logout } , but you are naming the entire object auth in your RequireAuth wrapper component and using it as the condition. useAuth挂钩返回 object { authed, login, logout } ,但您在RequireAuth包装器组件中命名整个object auth并将用作条件。 It's a defined object so it will always be truthy.它是一个已定义的 object,所以它总是真实的。 It looks like you just need to access the authed property from that object to know if a user has authenticated or not.看起来您只需要从该 object 访问authed属性即可知道用户是否已通过身份验证。

function RequireAuth({ children }) {
  const location = useLocation();
  const { authed } = useAuth();
  return auth ? children : <Navigate to="/" state={{from: location}}/>;
}

You also seem to have mixed up your auth context a bit.您似乎也混淆了您的身份验证上下文。 The state should reside in the context provider while the useAuth hook returns the provided value. state 应该驻留在上下文提供程序中,而useAuth挂钩返回提供的值。

const authContext = React.createContext({
  authed: false,
  login: () => {},
  logout: () => {}
});

export default function useAuth() {
  return React.useContext(authContext);
}

export function AuthProvider({ children }) {
  const [authed, setAuthed] = React.useState(false);

  const login = () => setAuthed(true);
  const logout = () => setAuthed(false);

  return (
    <authContext.Provider value={{ authed, login, logout }}>
      {children}
    </authContext.Provider>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM