Header.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. headers: {
  50. Origin: "https://edu.cocorobo.cn"
  51. },
  52. });
  53. try {
  54. const cookiejson = await cookie.json();
  55. console.log(cookiejson);
  56. const user = cookiejson?.[0]?.[0];
  57. if (cookie.ok && user) {
  58. const res = await signIn('credentials', { redirect: false, userid: user.userid })
  59. setIsLogInFail(true);
  60. clearInterval(intervalId);
  61. }
  62. }
  63. catch (e) {
  64. setIsLogInFail(false)
  65. }
  66. }
  67. setIsLogInFail(authToken); // 如果存在 authToken,则用户已登录
  68. };
  69. console.log("start")
  70. const intervalId = setInterval(async () => {
  71. await checkLoginStatus(intervalId);
  72. }, 5000);
  73. return () => {
  74. clearInterval(intervalId)
  75. }
  76. }, []);
  77. return (
  78. <div className="navbar shrink-0 bg-base-100 shadow-xl rounded-box justify-center min-h-4 relative">
  79. <div className="dropdown">
  80. <div tabIndex={0} role="button" className="btn btn-sm btn-wide btn-ghost">选择对话<BsCaretDownFill /></div>
  81. <ul tabIndex={0} className="dropdown-content menu bg-base-100 rounded-box z-[1] w-52 p-2 shadow">
  82. <li><a>Item 1</a></li>
  83. <li><a>Item 2</a></li>
  84. </ul>
  85. </div>
  86. {status === 'authenticated'
  87. ? (
  88. <div className="absolute right-4 flex gap-2">
  89. <div>
  90. <p>Hi, {session.user?.name}</p>
  91. </div>
  92. <button className='btn btn-sm' onClick={logOut}>退出登录</button>
  93. </div>
  94. ) : (
  95. <>
  96. <dialog className="modal modal-close" onCancel={event => event.preventDefault()}>
  97. <div className="modal-box">
  98. <iframe src="https://edu.cocorobo.cn/course/login?type=2"
  99. style={{ border: "0px", width: "450px", height: "480px" }}></iframe>
  100. </div>
  101. <div className="modal-box" style={{ display: "none" }} >
  102. <h3 className="font-bold text-lg" >您需要先登录</h3>
  103. <div className="w-full flex flex-col items-center gap-2 py-2">
  104. {isLogInFail && <div role="alert" className="alert alert-error">
  105. <span>账号或密码错误</span>
  106. </div>}
  107. <label className="form-control w-full max-w-xs">
  108. <div className="label">
  109. <span className="label-text">组织(选填)</span>
  110. {orgQuery?.data?.name && <span className="label-text-alt text-indigo-400">{orgQuery.data.name}</span>}
  111. </div>
  112. <input type="text" placeholder="" className="input input-bordered w-full max-w-xs" onChange={e => setOrg(e.target.value)} />
  113. </label>
  114. <label className="form-control w-full max-w-xs">
  115. <div className="label">
  116. <span className="label-text">用户名</span>
  117. </div>
  118. <input type="text" placeholder="" className="input input-bordered w-full max-w-xs" onChange={e => setUsername(e.target.value)} />
  119. </label>
  120. <label className="form-control w-full max-w-xs">
  121. <div className="label">
  122. <span className="label-text">密码</span>
  123. </div>
  124. <input type="password" placeholder="" className="input input-bordered w-full max-w-xs" onChange={e => setPassword(e.target.value)} />
  125. </label>
  126. <button className='btn btn-wide' disabled={!username || !password} onClick={logIn}>登录</button>
  127. </div>
  128. </div>
  129. </dialog>
  130. </>
  131. )
  132. }
  133. </div >
  134. )
  135. }