Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
761ae74b
authored
Aug 05, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: token
parent
eb5a2526
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
69 additions
and
45 deletions
+69
-45
client/src/api/fetch.ts
+3
-1
client/src/api/request.ts
+3
-3
client/src/api/response/user.d.ts
+1
-0
client/src/pages/account/index.tsx
+2
-2
client/src/pages/api/user/account/loginByPassword.ts
+5
-3
client/src/pages/api/user/account/register.ts
+5
-3
client/src/pages/api/user/account/updatePasswordByCode.ts
+5
-3
client/src/pages/api/user/account/updatePasswordByOld.ts
+0
-3
client/src/pages/login/index.tsx
+3
-1
client/src/service/utils/auth.ts
+14
-23
client/src/service/utils/tools.ts
+15
-2
client/src/utils/app.ts
+3
-0
client/src/utils/user.ts
+10
-1
No files found.
client/src/api/fetch.ts
View file @
761ae74b
...
...
@@ -3,6 +3,7 @@ import { getErrText } from '@/utils/tools';
import
{
parseStreamChunk
,
SSEParseData
}
from
'@/utils/sse'
;
import
type
{
ChatHistoryItemResType
}
from
'@/types/chat'
;
import
{
StartChatFnProps
}
from
'@/components/ChatBox'
;
import
{
getToken
}
from
'@/utils/user'
;
interface
StreamFetchProps
{
url
?:
string
;
...
...
@@ -24,7 +25,8 @@ export const streamFetch = ({
const
response
=
await
window
.
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Content-Type'
:
'application/json'
'Content-Type'
:
'application/json'
,
token
:
getToken
()
},
signal
:
abortSignal
.
signal
,
body
:
JSON
.
stringify
({
...
...
client/src/api/request.ts
View file @
761ae74b
import
axios
,
{
Method
,
InternalAxiosRequestConfig
,
AxiosResponse
}
from
'axios'
;
import
{
clear
Cookie
}
from
'@/utils/user'
;
import
{
clear
Token
,
getToken
}
from
'@/utils/user'
;
import
{
TOKEN_ERROR_CODE
}
from
'@/service/errorCode'
;
interface
ConfigType
{
...
...
@@ -18,7 +18,7 @@ interface ResponseDataType {
*/
function
requestStart
(
config
:
InternalAxiosRequestConfig
):
InternalAxiosRequestConfig
{
if
(
config
.
headers
)
{
// config.headers.Authorizatio
n = getToken();
config
.
headers
.
toke
n
=
getToken
();
}
return
config
;
...
...
@@ -57,7 +57,7 @@ function responseError(err: any) {
}
// 有报错响应
if
(
err
?.
code
in
TOKEN_ERROR_CODE
)
{
clear
Cookie
();
clear
Token
();
window
.
location
.
replace
(
`/login?lastRoute=
${
encodeURIComponent
(
location
.
pathname
+
location
.
search
)}
`
);
...
...
client/src/api/response/user.d.ts
View file @
761ae74b
...
...
@@ -2,6 +2,7 @@ import type { UserType } from '@/types/user';
import
type
{
PromotionRecordSchema
}
from
'@/types/mongoSchema'
;
export
interface
ResLogin
{
user
:
UserType
;
token
:
string
;
}
export
interface
PromotionRecordType
{
...
...
client/src/pages/account/index.tsx
View file @
761ae74b
...
...
@@ -3,7 +3,7 @@ import { Box, Flex, useTheme } from '@chakra-ui/react';
import
{
useGlobalStore
}
from
'@/store/global'
;
import
{
useRouter
}
from
'next/router'
;
import
dynamic
from
'next/dynamic'
;
import
{
clear
Cookie
}
from
'@/utils/user'
;
import
{
clear
Token
}
from
'@/utils/user'
;
import
{
useUserStore
}
from
'@/store/user'
;
import
{
useConfirm
}
from
'@/hooks/useConfirm'
;
import
PageContainer
from
'@/components/PageContainer'
;
...
...
@@ -77,7 +77,7 @@ const Account = ({ currentTab }: { currentTab: `${TabEnum}` }) => {
(
tab
:
string
)
=>
{
if
(
tab
===
TabEnum
.
loginout
)
{
openConfirm
(()
=>
{
clear
Cookie
();
clear
Token
();
setUserInfo
(
null
);
router
.
replace
(
'/login'
);
})();
...
...
client/src/pages/api/user/account/loginByPassword.ts
View file @
761ae74b
...
...
@@ -3,7 +3,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import
{
jsonRes
}
from
'@/service/response'
;
import
{
connectToDatabase
}
from
'@/service/mongo'
;
import
{
User
}
from
'@/service/models/user'
;
import
{
setCookie
}
from
'@/service/utils/tools'
;
import
{
generateToken
,
setCookie
}
from
'@/service/utils/tools'
;
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
try
{
...
...
@@ -32,11 +32,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
throw
new
Error
(
'密码错误'
);
}
setCookie
(
res
,
user
.
_id
);
const
token
=
generateToken
(
user
.
_id
);
setCookie
(
res
,
token
);
jsonRes
(
res
,
{
data
:
{
user
user
,
token
}
});
}
catch
(
err
)
{
...
...
client/src/pages/api/user/account/register.ts
View file @
761ae74b
...
...
@@ -4,7 +4,7 @@ import { jsonRes } from '@/service/response';
import
{
User
}
from
'@/service/models/user'
;
import
{
AuthCode
}
from
'@/service/models/authCode'
;
import
{
connectToDatabase
}
from
'@/service/mongo'
;
import
{
setCookie
}
from
'@/service/utils/tools'
;
import
{
generateToken
,
setCookie
}
from
'@/service/utils/tools'
;
import
{
UserAuthTypeEnum
}
from
'@/constants/common'
;
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
...
...
@@ -56,11 +56,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
username
});
setCookie
(
res
,
user
.
_id
);
const
token
=
generateToken
(
user
.
_id
);
setCookie
(
res
,
token
);
jsonRes
(
res
,
{
data
:
{
user
user
,
token
}
});
}
catch
(
err
)
{
...
...
client/src/pages/api/user/account/updatePasswordByCode.ts
View file @
761ae74b
...
...
@@ -5,7 +5,7 @@ import { User } from '@/service/models/user';
import
{
AuthCode
}
from
'@/service/models/authCode'
;
import
{
connectToDatabase
}
from
'@/service/mongo'
;
import
{
UserAuthTypeEnum
}
from
'@/constants/common'
;
import
{
setCookie
}
from
'@/service/utils/tools'
;
import
{
generateToken
,
setCookie
}
from
'@/service/utils/tools'
;
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
...
...
@@ -48,11 +48,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
throw
new
Error
(
'获取用户信息异常'
);
}
setCookie
(
res
,
user
.
_id
);
const
token
=
generateToken
(
user
.
_id
);
setCookie
(
res
,
token
);
jsonRes
(
res
,
{
data
:
{
user
user
,
token
}
});
}
catch
(
err
)
{
...
...
client/src/pages/api/user/account/updatePasswordByOld.ts
View file @
761ae74b
...
...
@@ -2,10 +2,7 @@
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
{
jsonRes
}
from
'@/service/response'
;
import
{
User
}
from
'@/service/models/user'
;
import
{
AuthCode
}
from
'@/service/models/authCode'
;
import
{
connectToDatabase
}
from
'@/service/mongo'
;
import
{
UserAuthTypeEnum
}
from
'@/constants/common'
;
import
{
setCookie
}
from
'@/service/utils/tools'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
...
...
client/src/pages/login/index.tsx
View file @
761ae74b
import
React
,
{
useState
,
useCallback
,
useEffect
}
from
'react'
;
import
React
,
{
useState
,
useCallback
}
from
'react'
;
import
styles
from
'./index.module.scss'
;
import
{
Box
,
Flex
,
Image
}
from
'@chakra-ui/react'
;
import
{
PageTypeEnum
}
from
'@/constants/user'
;
...
...
@@ -10,6 +10,7 @@ import { useChatStore } from '@/store/chat';
import
LoginForm
from
'./components/LoginForm'
;
import
dynamic
from
'next/dynamic'
;
import
{
serviceSideProps
}
from
'@/utils/i18n'
;
import
{
setToken
}
from
'@/utils/user'
;
const
RegisterForm
=
dynamic
(()
=>
import
(
'./components/RegisterForm'
));
const
ForgetPasswordForm
=
dynamic
(()
=>
import
(
'./components/ForgetPasswordForm'
));
...
...
@@ -28,6 +29,7 @@ const Login = () => {
setLastChatAppId
(
''
);
setUserInfo
(
res
.
user
);
setToken
(
res
.
token
);
setTimeout
(()
=>
{
router
.
push
(
lastRoute
?
decodeURIComponent
(
lastRoute
)
:
'/app/list'
);
},
100
);
...
...
client/src/service/utils/auth.ts
View file @
761ae74b
import
type
{
NextApiRequest
}
from
'next'
;
import
jwt
from
'jsonwebtoken'
;
import
Cookie
from
'cookie'
;
import
{
App
,
OpenApi
,
User
,
OutLink
,
KB
}
from
'../mongo'
;
import
type
{
AppSchema
}
from
'@/types/mongoSchema'
;
import
{
ERROR_ENUM
}
from
'../errorCode'
;
import
{
authJWT
}
from
'./tools'
;
export
enum
AuthUserTypeEnum
{
token
=
'token'
,
...
...
@@ -11,26 +11,16 @@ export enum AuthUserTypeEnum {
apikey
=
'apikey'
}
export
const
parseCookie
=
(
cookie
?:
string
):
Promise
<
string
>
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
// 获取 cookie
const
cookies
=
Cookie
.
parse
(
cookie
||
''
);
const
token
=
cookies
.
token
;
export
const
authCookieToken
=
async
(
cookie
?:
string
,
token
?:
string
):
Promise
<
string
>
=>
{
// 获取 cookie
const
cookies
=
Cookie
.
parse
(
cookie
||
''
);
const
cookieToken
=
cookies
.
token
||
token
;
if
(
!
token
)
{
return
reject
(
ERROR_ENUM
.
unAuthorization
);
}
const
key
=
process
.
env
.
TOKEN_KEY
as
string
;
if
(
!
cookieToken
)
{
return
Promise
.
reject
(
ERROR_ENUM
.
unAuthorization
);
}
jwt
.
verify
(
token
,
key
,
function
(
err
,
decoded
:
any
)
{
if
(
err
||
!
decoded
?.
userId
)
{
reject
(
ERROR_ENUM
.
unAuthorization
);
return
;
}
resolve
(
decoded
.
userId
);
});
});
return
await
authJWT
(
cookieToken
);
};
/* auth balance */
...
...
@@ -117,8 +107,9 @@ export const authUser = async ({
return
userId
;
};
const
{
cookie
,
apikey
,
rootkey
,
userid
,
authorization
}
=
(
req
.
headers
||
{})
as
{
const
{
cookie
,
token
,
apikey
,
rootkey
,
userid
,
authorization
}
=
(
req
.
headers
||
{})
as
{
cookie
?:
string
;
token
?:
string
;
apikey
?:
string
;
rootkey
?:
string
;
userid
?:
string
;
...
...
@@ -130,13 +121,13 @@ export const authUser = async ({
let
authType
:
`
${
AuthUserTypeEnum
}
`
=
AuthUserTypeEnum
.
token
;
if
(
authToken
)
{
uid
=
await
parseCookie
(
cookie
);
uid
=
await
authCookieToken
(
cookie
,
token
);
authType
=
AuthUserTypeEnum
.
token
;
}
else
if
(
authRoot
)
{
uid
=
await
parseRootKey
(
rootkey
,
userid
);
authType
=
AuthUserTypeEnum
.
root
;
}
else
if
(
cookie
)
{
uid
=
await
parseCookie
(
cookie
);
}
else
if
(
cookie
||
token
)
{
uid
=
await
authCookieToken
(
cookie
,
token
);
authType
=
AuthUserTypeEnum
.
token
;
}
else
if
(
apikey
)
{
uid
=
await
parseOpenApiKey
(
apikey
);
...
...
client/src/service/utils/tools.ts
View file @
761ae74b
...
...
@@ -4,6 +4,7 @@ import crypto from 'crypto';
import
jwt
from
'jsonwebtoken'
;
import
{
generateQA
}
from
'../events/generateQA'
;
import
{
generateVector
}
from
'../events/generateVector'
;
import
{
ERROR_ENUM
}
from
'../errorCode'
;
/* 密码加密 */
export
const
hashPassword
=
(
psw
:
string
)
=>
{
...
...
@@ -22,12 +23,24 @@ export const generateToken = (userId: string) => {
);
return
token
;
};
// auth token
export
const
authJWT
=
(
token
:
string
)
=>
new
Promise
<
string
>
((
resolve
,
reject
)
=>
{
const
key
=
process
.
env
.
TOKEN_KEY
as
string
;
jwt
.
verify
(
token
,
key
,
function
(
err
,
decoded
:
any
)
{
if
(
err
||
!
decoded
?.
userId
)
{
reject
(
ERROR_ENUM
.
unAuthorization
);
return
;
}
resolve
(
decoded
.
userId
);
});
});
/* set cookie */
export
const
setCookie
=
(
res
:
NextApiResponse
,
userId
:
string
)
=>
{
export
const
setCookie
=
(
res
:
NextApiResponse
,
token
:
string
)
=>
{
res
.
setHeader
(
'Set-Cookie'
,
`token=
${
generateToken
(
userId
)
}
; Path=/; HttpOnly; Max-Age=604800; Samesite=None; Secure;`
`token=
${
token
}
; Path=/; HttpOnly; Max-Age=604800; Samesite=None; Secure;`
);
};
/* clear cookie */
...
...
client/src/utils/app.ts
View file @
761ae74b
...
...
@@ -331,6 +331,7 @@ const simpleChatTemplate = (formData: EditFormType): AppModuleItemType[] => [
name
:
'AI 对话'
,
flowType
:
FlowModuleTypeEnum
.
chatNode
,
inputs
:
chatModelInput
(
formData
),
showStatus
:
true
,
outputs
:
[
{
key
:
'answerText'
,
...
...
@@ -426,6 +427,7 @@ const kbTemplate = (formData: EditFormType): AppModuleItemType[] => [
{
name
:
'知识库搜索'
,
flowType
:
FlowModuleTypeEnum
.
kbSearchNode
,
showStatus
:
true
,
inputs
:
[
{
key
:
'kbList'
,
...
...
@@ -537,6 +539,7 @@ const kbTemplate = (formData: EditFormType): AppModuleItemType[] => [
name
:
'AI 对话'
,
flowType
:
FlowModuleTypeEnum
.
chatNode
,
inputs
:
chatModelInput
(
formData
),
showStatus
:
true
,
outputs
:
[
{
key
:
'answerText'
,
...
...
client/src/utils/user.ts
View file @
761ae74b
import
{
PRICE_SCALE
}
from
'@/constants/common'
;
import
{
loginOut
}
from
'@/api/user'
;
export
const
clearCookie
=
()
=>
{
const
tokenKey
=
'token'
;
export
const
clearToken
=
()
=>
{
try
{
loginOut
();
localStorage
.
removeItem
(
tokenKey
);
}
catch
(
error
)
{
error
;
}
};
export
const
setToken
=
(
token
:
string
)
=>
{
localStorage
.
setItem
(
tokenKey
,
token
);
};
export
const
getToken
=
()
=>
{
return
localStorage
.
getItem
(
tokenKey
)
||
''
;
};
/**
* 把数据库读取到的price,转化成元
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment