Header.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use client'
  2. import { BsCaretDownFill } from "react-icons/bs";
  3. import { useSession, signIn, signOut } from "next-auth/react"
  4. import { createRef, useEffect, useState } from "react";
  5. import { useQuery } from "@tanstack/react-query";
  6. import { trpc } from "@/lib/trpc";
  7. import Cookies from 'js-cookie';
  8. export default function Header() {
  9. const { data: session, status } = useSession()
  10. const [org, setOrg] = useState('')
  11. const [username, setUsername] = useState('')
  12. const [password, setPassword] = useState('')
  13. const [isLogInFail, setIsLogInFail] = useState(false)
  14. const orgQuery = trpc.org.bySlug.useQuery({ mode: org })
  15. const logOut = async () => {
  16. const res = await fetch(" beta.api.cocorobo.cn/api/logout", {
  17. method: "GET",
  18. headers: {
  19. Origin: "https://edu.cocorobo.cn"
  20. },
  21. });
  22. await signOut({ redirect: false })
  23. }
  24. const logIn = async () => {
  25. const loginUsername = orgQuery.data?.mail ? `${username}@${orgQuery.data?.mail}` : `${username}@cocorobo.cc`
  26. const loginPassword = btoa(password)
  27. const res = await signIn('credentials', { redirect: false, loginUsername, loginPassword })
  28. console.log(res)
  29. if (!res.ok) {
  30. setIsLogInFail(true)
  31. }
  32. }
  33. // useEffect(() => {
  34. // setIsLogInFail(false)
  35. // }, [username, password, org])
  36. /*
  37. 用户登陆判断
  38. */
  39. useEffect(() => {
  40. const checkLoginStatus = async (intervalId) => {
  41. // 检查名为 'authToken' 的 cookie 是否存在
  42. const authToken = Cookies.get('cocorobo');
  43. console.log(authToken)
  44. const tf = authToken ? true : false;
  45. if (status !== 'authenticated') {//tf &&
  46. const cookie = await fetch("https://beta.api.cocorobo.cn/api/getcookieuserid", {
  47. method: "GET",
  48. credentials: 'include',
  49. });
  50. try {
  51. const cookiejson = await cookie.json();
  52. console.log(cookiejson);
  53. const user = cookiejson?.[0]?.[0];
  54. if (cookie.ok && user) {
  55. const res = await signIn('credentials', { redirect: false, userid: user.userid })
  56. setIsLogInFail(true);
  57. clearInterval(intervalId);
  58. }
  59. }
  60. catch (e) {
  61. setIsLogInFail(false)
  62. }
  63. }
  64. setIsLogInFail(authToken); // 如果存在 authToken,则用户已登录
  65. };
  66. console.log("start")
  67. const intervalId = setInterval(async () => {
  68. await checkLoginStatus(intervalId);
  69. }, 5000);
  70. return () => {
  71. clearInterval(intervalId)
  72. }
  73. }, []);
  74. return (
  75. <div className="navbar shrink-0 bg-base-100 shadow-xl rounded-box justify-center min-h-4 relative">
  76. <div className="dropdown">
  77. <div tabIndex={0} role="button" className="btn btn-sm btn-wide btn-ghost">选择对话<BsCaretDownFill /></div>
  78. <ul tabIndex={0} className="dropdown-content menu bg-base-100 rounded-box z-[2] w-52 p-2 shadow">
  79. <li><a>Item 1</a></li>
  80. <li><a>Item 2</a></li>
  81. </ul>
  82. </div>
  83. {status === 'authenticated'
  84. ? (
  85. <div className="absolute right-4 flex gap-2">
  86. <div>
  87. <p>Hi, {session.user?.name}</p>
  88. </div>
  89. <button className='btn btn-sm' onClick={logOut}>退出登录</button>
  90. </div>
  91. ) : (
  92. <>
  93. <dialog className="modal modal-close" onCancel={event => event.preventDefault()}>
  94. <div className="modal-box">
  95. <iframe src="https://edu.cocorobo.cn/course/login?type=2"
  96. style={{ border: "0px", width: "450px", height: "480px" }}></iframe>
  97. </div>
  98. <div className="modal-box" style={{ display: "none" }} >
  99. <h3 className="font-bold text-lg" >您需要先登录</h3>
  100. <div className="w-full flex flex-col items-center gap-2 py-2">
  101. {isLogInFail && <div role="alert" className="alert alert-error">
  102. <span>账号或密码错误</span>
  103. </div>}
  104. <label className="form-control w-full max-w-xs">
  105. <div className="label">
  106. <span className="label-text">组织(选填)</span>
  107. {orgQuery?.data?.name && <span className="label-text-alt text-indigo-400">{orgQuery.data.name}</span>}
  108. </div>
  109. <input type="text" placeholder="" className="input input-bordered w-full max-w-xs" onChange={e => setOrg(e.target.value)} />
  110. </label>
  111. <label className="form-control w-full max-w-xs">
  112. <div className="label">
  113. <span className="label-text">用户名</span>
  114. </div>
  115. <input type="text" placeholder="" className="input input-bordered w-full max-w-xs" onChange={e => setUsername(e.target.value)} />
  116. </label>
  117. <label className="form-control w-full max-w-xs">
  118. <div className="label">
  119. <span className="label-text">密码</span>
  120. </div>
  121. <input type="password" placeholder="" className="input input-bordered w-full max-w-xs" onChange={e => setPassword(e.target.value)} />
  122. </label>
  123. <button className='btn btn-wide' disabled={!username || !password} onClick={logIn}>登录</button>
  124. </div>
  125. </div>
  126. </dialog>
  127. </>
  128. )
  129. }
  130. </div >
  131. )
  132. }