|
@@ -4,6 +4,7 @@ import { useSession, signIn, signOut } from "next-auth/react"
|
|
import { createRef, useEffect, useState } from "react";
|
|
import { createRef, useEffect, useState } from "react";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { trpc } from "@/lib/trpc";
|
|
import { trpc } from "@/lib/trpc";
|
|
|
|
+import Cookies from 'js-cookie';
|
|
|
|
|
|
export default function Header() {
|
|
export default function Header() {
|
|
const { data: session, status } = useSession()
|
|
const { data: session, status } = useSession()
|
|
@@ -16,6 +17,12 @@ export default function Header() {
|
|
const orgQuery = trpc.org.bySlug.useQuery({ mode: org })
|
|
const orgQuery = trpc.org.bySlug.useQuery({ mode: org })
|
|
|
|
|
|
const logOut = async () => {
|
|
const logOut = async () => {
|
|
|
|
+ const res = await fetch(" beta.api.cocorobo.cn/api/logout", {
|
|
|
|
+ method: "GET",
|
|
|
|
+ headers: {
|
|
|
|
+ Origin: "https://edu.cocorobo.cn"
|
|
|
|
+ },
|
|
|
|
+ });
|
|
await signOut({ redirect: false })
|
|
await signOut({ redirect: false })
|
|
}
|
|
}
|
|
const logIn = async () => {
|
|
const logIn = async () => {
|
|
@@ -27,9 +34,49 @@ export default function Header() {
|
|
setIsLogInFail(true)
|
|
setIsLogInFail(true)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // useEffect(() => {
|
|
|
|
+ // setIsLogInFail(false)
|
|
|
|
+ // }, [username, password, org])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ 用户登陆判断
|
|
|
|
+*/
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
- setIsLogInFail(false)
|
|
|
|
- }, [username, password, org])
|
|
|
|
|
|
+ const checkLoginStatus = async (intervalId) => {
|
|
|
|
+ // 检查名为 'authToken' 的 cookie 是否存在
|
|
|
|
+ const authToken = Cookies.get('cocorobo');
|
|
|
|
+ console.log(authToken)
|
|
|
|
+ const tf = authToken ? true : false;
|
|
|
|
+ if (status !== 'authenticated') {//tf &&
|
|
|
|
+ const cookie = await fetch("https://beta.api.cocorobo.cn/api/getcookieuserid", {
|
|
|
|
+ method: "GET",
|
|
|
|
+ headers: {
|
|
|
|
+ Origin: "https://edu.cocorobo.cn"
|
|
|
|
+ },
|
|
|
|
+ });
|
|
|
|
+ try {
|
|
|
|
+ const cookiejson = await cookie.json();
|
|
|
|
+ console.log(cookiejson);
|
|
|
|
+ const user = cookiejson?.[0]?.[0];
|
|
|
|
+ if (cookie.ok && user) {
|
|
|
|
+ const res = await signIn('credentials', { redirect: false, userid: user.userid })
|
|
|
|
+ setIsLogInFail(true);
|
|
|
|
+ clearInterval(intervalId);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch (e) {
|
|
|
|
+ setIsLogInFail(false)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ setIsLogInFail(authToken); // 如果存在 authToken,则用户已登录
|
|
|
|
+ };
|
|
|
|
+ console.log("start")
|
|
|
|
+ const intervalId = setInterval(async () => {
|
|
|
|
+ await checkLoginStatus(intervalId);
|
|
|
|
+ }, 5000);
|
|
|
|
+ }, []);
|
|
|
|
|
|
return (
|
|
return (
|
|
<div className="navbar bg-base-100 shadow-xl rounded-box justify-center min-h-4 relative">
|
|
<div className="navbar bg-base-100 shadow-xl rounded-box justify-center min-h-4 relative">
|
|
@@ -52,7 +99,11 @@ export default function Header() {
|
|
<>
|
|
<>
|
|
<dialog className="modal modal-open" onCancel={event => event.preventDefault()}>
|
|
<dialog className="modal modal-open" onCancel={event => event.preventDefault()}>
|
|
<div className="modal-box">
|
|
<div className="modal-box">
|
|
- <h3 className="font-bold text-lg">您需要先登录</h3>
|
|
|
|
|
|
+ <iframe src="https://edu.cocorobo.cn/course/login?type=2"
|
|
|
|
+ style={{ border: "0px", width: "450px", height: "480px" }}></iframe>
|
|
|
|
+ </div>
|
|
|
|
+ <div className="modal-box" style={{ display: "none" }} >
|
|
|
|
+ <h3 className="font-bold text-lg" >您需要先登录</h3>
|
|
<div className="w-full flex flex-col items-center gap-2 py-2">
|
|
<div className="w-full flex flex-col items-center gap-2 py-2">
|
|
{isLogInFail && <div role="alert" className="alert alert-error">
|
|
{isLogInFail && <div role="alert" className="alert alert-error">
|
|
<span>账号或密码错误</span>
|
|
<span>账号或密码错误</span>
|
|
@@ -83,6 +134,6 @@ export default function Header() {
|
|
</>
|
|
</>
|
|
)
|
|
)
|
|
}
|
|
}
|
|
- </div>
|
|
|
|
|
|
+ </div >
|
|
)
|
|
)
|
|
}
|
|
}
|