-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: login limit * feat: env template * fix: ts
- Loading branch information
Showing
9 changed files
with
57 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 45 additions & 53 deletions
98
projects/app/src/pages/api/support/user/account/loginByPassword.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,63 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { jsonRes } from '@fastgpt/service/common/response'; | ||
import { MongoUser } from '@fastgpt/service/support/user/schema'; | ||
import { createJWT, setCookie } from '@fastgpt/service/support/permission/controller'; | ||
import { connectToDatabase } from '@/service/mongo'; | ||
import { getUserDetail } from '@fastgpt/service/support/user/controller'; | ||
import type { PostLoginProps } from '@fastgpt/global/support/user/api.d'; | ||
import { UserStatusEnum } from '@fastgpt/global/support/user/constant'; | ||
import { NextAPI } from '@/service/middleware/entry'; | ||
import { useReqFrequencyLimit } from '@fastgpt/service/common/middle/reqFrequencyLimit'; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
await connectToDatabase(); | ||
const { username, password } = req.body as PostLoginProps; | ||
async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const { username, password } = req.body as PostLoginProps; | ||
|
||
if (!username || !password) { | ||
throw new Error('缺少参数'); | ||
} | ||
if (!username || !password) { | ||
throw new Error('缺少参数'); | ||
} | ||
|
||
// 检测用户是否存在 | ||
const authCert = await MongoUser.findOne( | ||
{ | ||
username | ||
}, | ||
'status' | ||
); | ||
if (!authCert) { | ||
throw new Error('用户未注册'); | ||
} | ||
// 检测用户是否存在 | ||
const authCert = await MongoUser.findOne( | ||
{ | ||
username | ||
}, | ||
'status' | ||
); | ||
if (!authCert) { | ||
throw new Error('用户未注册'); | ||
} | ||
|
||
if (authCert.status === UserStatusEnum.forbidden) { | ||
throw new Error('账号已停用,无法登录'); | ||
} | ||
if (authCert.status === UserStatusEnum.forbidden) { | ||
throw new Error('账号已停用,无法登录'); | ||
} | ||
|
||
const user = await MongoUser.findOne({ | ||
username, | ||
password | ||
}); | ||
const user = await MongoUser.findOne({ | ||
username, | ||
password | ||
}); | ||
|
||
if (!user) { | ||
throw new Error('密码错误'); | ||
} | ||
if (!user) { | ||
throw new Error('密码错误'); | ||
} | ||
|
||
const userDetail = await getUserDetail({ | ||
tmbId: user?.lastLoginTmbId, | ||
userId: user._id | ||
}); | ||
const userDetail = await getUserDetail({ | ||
tmbId: user?.lastLoginTmbId, | ||
userId: user._id | ||
}); | ||
|
||
MongoUser.findByIdAndUpdate(user._id, { | ||
lastLoginTmbId: userDetail.team.tmbId | ||
}); | ||
MongoUser.findByIdAndUpdate(user._id, { | ||
lastLoginTmbId: userDetail.team.tmbId | ||
}); | ||
|
||
const token = createJWT({ | ||
...userDetail, | ||
isRoot: username === 'root' | ||
}); | ||
const token = createJWT({ | ||
...userDetail, | ||
isRoot: username === 'root' | ||
}); | ||
|
||
setCookie(res, token); | ||
setCookie(res, token); | ||
|
||
jsonRes(res, { | ||
data: { | ||
user: userDetail, | ||
token | ||
} | ||
}); | ||
} catch (err) { | ||
jsonRes(res, { | ||
code: 500, | ||
error: err | ||
}); | ||
} | ||
return { | ||
user: userDetail, | ||
token | ||
}; | ||
} | ||
|
||
export default NextAPI(useReqFrequencyLimit(120, 10), handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters