Commit a43fc4ef authored by StyleZhang's avatar StyleZhang

add X-App-Code to allow_headers

parent 438ad600
...@@ -155,7 +155,7 @@ def register_blueprints(app): ...@@ -155,7 +155,7 @@ def register_blueprints(app):
resources={ resources={
r"/*": {"origins": app.config['WEB_API_CORS_ALLOW_ORIGINS']}}, r"/*": {"origins": app.config['WEB_API_CORS_ALLOW_ORIGINS']}},
supports_credentials=True, supports_credentials=True,
allow_headers=['Content-Type', 'Authorization'], allow_headers=['Content-Type', 'Authorization', 'X-App-Code'],
methods=['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'PATCH'], methods=['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'PATCH'],
expose_headers=['X-Version', 'X-Env'] expose_headers=['X-Version', 'X-Env']
) )
......
...@@ -40,7 +40,7 @@ class PassportResource(Resource): ...@@ -40,7 +40,7 @@ class PassportResource(Resource):
payload = { payload = {
"iss": site.app_id, "iss": site.app_id,
'sub': 'Web API Passport', 'sub': 'Web API Passport',
"aud": end_user.id, # "aud": end_user.id,
'app_id': site.app_id, 'app_id': site.app_id,
'end_user_id': end_user.id, 'end_user_id': end_user.id,
} }
......
...@@ -6,6 +6,7 @@ import cn from 'classnames' ...@@ -6,6 +6,7 @@ import cn from 'classnames'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useContext } from 'use-context-selector' import { useContext } from 'use-context-selector'
import produce from 'immer' import produce from 'immer'
import { useParams } from 'next/navigation'
import { useBoolean, useGetState } from 'ahooks' import { useBoolean, useGetState } from 'ahooks'
import AppUnavailable from '../../base/app-unavailable' import AppUnavailable from '../../base/app-unavailable'
import useConversation from './hooks/use-conversation' import useConversation from './hooks/use-conversation'
...@@ -14,7 +15,20 @@ import { ToastContext } from '@/app/components/base/toast' ...@@ -14,7 +15,20 @@ import { ToastContext } from '@/app/components/base/toast'
import Sidebar from '@/app/components/share/chat/sidebar' import Sidebar from '@/app/components/share/chat/sidebar'
import ConfigSence from '@/app/components/share/chat/config-scence' import ConfigSence from '@/app/components/share/chat/config-scence'
import Header from '@/app/components/share/header' import Header from '@/app/components/share/header'
import { delConversation, fetchAppInfo, fetchAppParams, fetchChatList, fetchConversations, fetchSuggestedQuestions, pinConversation, sendChatMessage, stopChatMessageResponding, unpinConversation, updateFeedback } from '@/service/share' import {
delConversation,
fetchAccessToken,
fetchAppInfo,
fetchAppParams,
fetchChatList,
fetchConversations,
fetchSuggestedQuestions,
pinConversation,
sendChatMessage,
stopChatMessageResponding,
unpinConversation,
updateFeedback,
} from '@/service/share'
import type { ConversationItem, SiteInfo } from '@/models/share' import type { ConversationItem, SiteInfo } from '@/models/share'
import type { PromptConfig, SuggestedQuestionsAfterAnswerConfig } from '@/models/debug' import type { PromptConfig, SuggestedQuestionsAfterAnswerConfig } from '@/models/debug'
import type { Feedbacktype, IChatItem } from '@/app/components/app/chat' import type { Feedbacktype, IChatItem } from '@/app/components/app/chat'
...@@ -54,6 +68,7 @@ const Main: FC<IMainProps> = ({ ...@@ -54,6 +68,7 @@ const Main: FC<IMainProps> = ({
// in mobile, show sidebar by click button // in mobile, show sidebar by click button
const [isShowSidebar, { setTrue: showSidebar, setFalse: hideSidebar }] = useBoolean(false) const [isShowSidebar, { setTrue: showSidebar, setFalse: hideSidebar }] = useBoolean(false)
// Can Use metadata(https://beta.nextjs.org/docs/api-reference/metadata) to set title. But it only works in server side client. // Can Use metadata(https://beta.nextjs.org/docs/api-reference/metadata) to set title. But it only works in server side client.
const params = useParams()
useEffect(() => { useEffect(() => {
if (siteInfo?.title) { if (siteInfo?.title) {
if (plan !== 'basic') if (plan !== 'basic')
...@@ -296,7 +311,21 @@ const Main: FC<IMainProps> = ({ ...@@ -296,7 +311,21 @@ const Main: FC<IMainProps> = ({
return fetchConversations(isInstalledApp, installedAppInfo?.id, undefined, undefined, 100) return fetchConversations(isInstalledApp, installedAppInfo?.id, undefined, undefined, 100)
} }
const fetchInitData = () => { const fetchAndSetAccessToken = async () => {
const res = await fetchAccessToken(params.token)
localStorage.setItem('accessToken', res.access_token)
fetchInitData()
}
const fetchInitData = async () => {
let appData: any = {}
try {
appData = await fetchAppInfo()
}
catch (e: any) {
if (e.code === 'unauthorized')
await fetchAndSetAccessToken()
}
return Promise.all([isInstalledApp return Promise.all([isInstalledApp
? { ? {
app_id: installedAppInfo?.id, app_id: installedAppInfo?.id,
...@@ -307,7 +336,7 @@ const Main: FC<IMainProps> = ({ ...@@ -307,7 +336,7 @@ const Main: FC<IMainProps> = ({
}, },
plan: 'basic', plan: 'basic',
} }
: fetchAppInfo(), fetchAllConversations(), fetchAppParams(isInstalledApp, installedAppInfo?.id)]) : appData, fetchAllConversations(), fetchAppParams(isInstalledApp, installedAppInfo?.id)])
} }
// init // init
......
...@@ -141,8 +141,8 @@ const baseFetch = ( ...@@ -141,8 +141,8 @@ const baseFetch = (
) => { ) => {
const options = Object.assign({}, baseOptions, fetchOptions) const options = Object.assign({}, baseOptions, fetchOptions)
if (isPublicAPI) { if (isPublicAPI) {
const sharedToken = globalThis.location.pathname.split('/').slice(-1)[0] const sharedToken = localStorage.getItem('accessToken') || ''
options.headers.set('Authorization', `bearer ${sharedToken}`) options.headers.set('Authorization', `Bearer ${sharedToken}`)
} }
if (deleteContentType) { if (deleteContentType) {
...@@ -194,7 +194,7 @@ const baseFetch = ( ...@@ -194,7 +194,7 @@ const baseFetch = (
case 401: { case 401: {
if (isPublicAPI) { if (isPublicAPI) {
Toast.notify({ type: 'error', message: 'Invalid token' }) Toast.notify({ type: 'error', message: 'Invalid token' })
return return bodyJson.then((data: any) => Promise.reject(data))
} }
const loginUrl = `${globalThis.location.origin}/signin` const loginUrl = `${globalThis.location.origin}/signin`
if (IS_CE_EDITION) { if (IS_CE_EDITION) {
......
...@@ -118,3 +118,9 @@ export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boole ...@@ -118,3 +118,9 @@ export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boole
export const audioToText = (url: string, isPublicAPI: boolean, body: FormData) => { export const audioToText = (url: string, isPublicAPI: boolean, body: FormData) => {
return (getAction('post', !isPublicAPI))(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ text: string }> return (getAction('post', !isPublicAPI))(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ text: string }>
} }
export const fetchAccessToken = async (appCode: string) => {
const headers = new Headers()
headers.append('X-App-Code', appCode)
return get('/passport', { headers }) as Promise<{ access_token: string }>
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment