Skip to content

Commit

Permalink
Feat/0.1.9.2 (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaojin3616 authored Nov 8, 2023
2 parents dd5f503 + 6f6cf9d commit f5c9d76
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
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}')
16 changes: 12 additions & 4 deletions src/frontend/src/contexts/userContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactNode, createContext, useEffect, useState } from "react";
import { ReactNode, createContext, useEffect, useLayoutEffect, useState } from "react";
import { getUserInfo } from "../controllers/API/user";

type userContextType = {
Expand All @@ -8,7 +8,7 @@ type userContextType = {

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

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

useEffect(() => {
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 : {})
setUser(res.data.user_id ? res.data : null)
})
}, [])

Expand Down

0 comments on commit f5c9d76

Please sign in to comment.