useEffect钩子用于通过本地存储登录。

huangapple go评论79阅读模式
英文:

useEffect hook for login via local storage

问题

//模块
import { useState, useEffect } from "react";
import { useRouter } from "next/navigation";

//样式
import styles from "./page.module.sass";

//组件
import NewCV from "./components/newCv";
import Cvs from "./components/cvs";
import PersonalInfo from "./components/personalInfo";
import LayoutPicker from "./components/LayoutPicker";

//帮助器
import { vaildToken } from "@/app/authorization";

export default function MyPage() {
  const router = useRouter();
  const [catType, setCatType] = useState(0);
  const cat = (type) => setCatType(type);

  const [valid, setValid] = useState(false);
  const [role, setRole] = useState('')

  useEffect(() => {
    const res = vaildToken(localStorage.getItem("token"));
    setValid(res[0]);
    setRole(res[1]);

    if (valid !== true && role !== "USER") {
      router.push("/login");
    }
  }, [valid, role]);

  console.log(valid, role)

  return (
    <main className={styles.main}>
      {valid !== true && role !== "USER" ? (
        <div className={styles.container}>
          <LayoutPicker cat={cat} />
          {catType == 1 ? <Cvs /> : catType == 2 ? <NewCV /> : <PersonalInfo />}
        </div>
      ) : (
        <></>
      )}
    </main>
  );
}
英文:

I'm creating a React + Next.js app with authorisation and I need to get token from local storage. If I do this without useEffect, there is an error of undefined localstorage, but with use effect my redirects don't work, maybe you know where am I wrong?

&quot;use client&quot;;
//modules
import { useState, useEffect } from &quot;react&quot;;
import { useRouter } from &quot;next/navigation&quot;;
//styles
import styles from &quot;./page.module.sass&quot;;
//components
import NewCV from &quot;./components/newCv&quot;;
import Cvs from &quot;./components/cvs&quot;;
import PersonalInfo from &quot;./components/personalInfo&quot;;
import LayoutPicker from &quot;./components/LayoutPicker&quot;;
//helpers
import { vaildToken } from &quot;@/app/authorization&quot;;
export default function MyPage() {
const router = useRouter();
const [catType, setCatType] = useState(0);
const cat = (type) =&gt; setCatType(type);
const [valid, setValid] = useState(false);
const [role, setRole] = useState(&#39;&#39;)
useEffect(() =&gt; {
const res = vaildToken(localStorage.getItem(&quot;token&quot;));
setValid(res[0]);
setRole(res[1]);
if (valid !== true &amp;&amp; role !== &quot;USER&quot;) {
router.push(&quot;/login&quot;);
}
}, [valid, role]);
console.log(valid, role)
return (
&lt;main className={styles.main}&gt;
{valid !== true &amp;&amp; role !== &quot;USER&quot; ? (
&lt;div className={styles.container}&gt;
&lt;LayoutPicker cat={cat} /&gt;
{catType == 1 ? &lt;Cvs /&gt; : catType == 2 ? &lt;NewCV /&gt; : &lt;PersonalInfo /&gt;}
&lt;/div&gt;
) : (
&lt;&gt;&lt;/&gt;
)}
&lt;/main&gt;
);
}

答案1

得分: 1

更改条件如下,因为您不会立即获得状态值。

if (res[0] !== true && res[1] !== "USER") {
  router.push("/login");
}
希望它能工作
英文:

Change the condition as follows cause you won't get the state value immediately.

if (res[0] !== true &amp;&amp; res[1] !== &quot;USER&quot;) {
router.push(&quot;/login&quot;);
}

Hope, it'll work.

huangapple
  • 本文由 发表于 2023年7月13日 23:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定