Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge #133

Closed
wants to merge 5 commits into from
Closed

merge #133

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/backend/bisheng/api/v2/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from bisheng.settings import settings
from fastapi import APIRouter, Depends
from fastapi.responses import RedirectResponse
from fastapi_jwt_auth import AuthJWT

# build router
Expand All @@ -12,14 +13,11 @@
@router.get('/auth')
def set_cookie(Authorize: AuthJWT = Depends()):
"""设置默认"""
default_user_id = settings.get_from_db('default_operator')
default_user_id = settings.get_from_db('default_operator').get('user')
payload = {'user_name': 'operator', 'user_id': default_user_id, 'role': [2]}
# Create the tokens and passing to set_access_cookies or set_refresh_cookies
access_token = Authorize.create_access_token(subject=json.dumps(payload),
expires_time=864000)
refresh_token = Authorize.create_refresh_token(subject='operator')

# Set the JWT cookies in the response
Authorize.set_access_cookies(access_token)
Authorize.set_refresh_cookies(refresh_token)
return {'cookie': access_token}
return RedirectResponse(settings.get_from_db('default_operator'
).get('url')+f'?token={access_token}')
6 changes: 5 additions & 1 deletion src/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,15 @@ export default function App() {
const noAuthPages = ['chat']
const path = location.pathname.split('/')?.[1] || ''

console.log('user :>> ', user);

return (
//need parent component with width and height
<div className="flex h-full flex-col">
{/* <Header /> */}
{user || noAuthPages.includes(path) ? <RouterProvider router={router} /> : <LoginPage></LoginPage>}
{(user?.user_id || noAuthPages.includes(path)) ? <RouterProvider router={router} />
: user ? <div className="loading"></div>
: <LoginPage></LoginPage>}
<div></div>
<div className="app-div" style={{ zIndex: 999 }}>
{alertsList.map((alert) => (
Expand Down
21 changes: 18 additions & 3 deletions src/frontend/src/contexts/userContext.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ReactNode, createContext, useState } from "react";
import { ReactNode, createContext, useEffect, useLayoutEffect, useState } from "react";
import { getUserInfo } from "../controllers/API/user";

type userContextType = {
user: any;
setUser: (newState: any) => void;
}

const userInfoLocalStr = localStorage.getItem('UUR_INFO')
// const userInfoLocalStr = localStorage.getItem('UUR_INFO')
const initialValue = {
user: userInfoLocalStr ? JSON.parse(atob(userInfoLocalStr)) : null,
user: {}, // userInfoLocalStr ? JSON.parse(atob(userInfoLocalStr)) : null,
setUser: () => { }
}

Expand All @@ -16,6 +17,20 @@ export const userContext = createContext<userContextType>(initialValue);
export function UserProvider({ children }: { children: ReactNode }) {
const [user, setUser] = useState(initialValue.user);

useLayoutEffect(() => {
// 链接ar参数存cookie(免登录接口)
const cookie = location.search.match(/(?<=token=)[^&]+/g)?.[0]
if (cookie) {
document.cookie = `access_token_cookie=${cookie}`;
location.href = location.origin + location.pathname;
return
}

getUserInfo().then(res => {
setUser(res.data.user_id ? res.data : null)
})
}, [])

return (
<userContext.Provider
value={{
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/src/controllers/API/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ axios.interceptors.response.use(function (response) {
}
return Promise.reject(error);
})
// 校验登录
export async function getUserInfo() {
return await axios.get(`/api/v1/user/info`);
}
// 登录
export async function loginApi(name, pwd) {
return await axios.post(`/api/v1/user/login`, { user_name: name, password: pwd });
Expand Down
13 changes: 12 additions & 1 deletion src/frontend/src/layout/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ export default function MainLayout() {

const { language, options, changLanguage, t } = useLanguage()

function clearAllCookies() {
var cookies = document.cookie.split(";");

for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}

return <div className="flex">
<div className="bg-white h-screen w-40 px-4 py-8 shadow-xl dark:shadow-slate-700 relative text-center">
<Link className="inline-block mb-1" to='/'><img src='/logo.jpeg' className="w-9 h-9" alt="" /></Link>
Expand Down Expand Up @@ -101,7 +112,7 @@ export default function MainLayout() {
<TooltipProvider>
<Tooltip>
<TooltipTrigger className="flex-1 py-1 rounded-sm hover:bg-gray-100 dark:hover:bg-gray-600 cursor-pointer">
<div className=" flex justify-center gap-2 items-center" onClick={() => { setUser(null); localStorage.setItem('UUR_INFO', '') }}>
<div className=" flex justify-center gap-2 items-center" onClick={() => { clearAllCookies(); setUser(null); }}>
<LogOut className="side-bar-button-size" />
<span>{t('menu.logout')}</span>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { viteStaticCopy } from 'vite-plugin-static-copy'
const apiRoutes = ["^/api/v1/", "/health"];

// Use environment variable to determine the target.
const target = process.env.VITE_PROXY_TARGET || "http://192.168.106.120:7860";
const target = process.env.VITE_PROXY_TARGET || "http://192.168.106.116:7861";

const proxyTargets = apiRoutes.reduce((proxyObj, route) => {
proxyObj[route] = {
Expand Down
Loading