Skip to content

Commit 5c4259a

Browse files
author
Kerwin
committed
feat: 开启登录
1 parent fba52bc commit 5c4259a

File tree

11 files changed

+59
-18
lines changed

11 files changed

+59
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chatgpt-web",
3-
"version": "2.10.7",
3+
"version": "2.11.0",
44
"private": false,
55
"description": "ChatGPT Web",
66
"author": "ChenZhaoYu <chenzhaoyu1994@gmail.com>",

service/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ router.post('/config', auth, async (req, res) => {
232232

233233
router.post('/session', async (req, res) => {
234234
try {
235-
const AUTH_SECRET_KEY = process.env.AUTH_SECRET_KEY
236-
const hasAuth = typeof AUTH_SECRET_KEY === 'string' && AUTH_SECRET_KEY.length > 0
235+
const config = await getCacheConfig()
236+
const hasAuth = config.siteConfig.loginEnabled
237237
const allowRegister = (await getCacheConfig()).siteConfig.registerEnabled
238238
res.send({ status: 'Success', message: '', data: { auth: hasAuth, allowRegister, model: currentModel() } })
239239
}
@@ -256,14 +256,14 @@ router.post('/user-login', async (req, res) => {
256256
throw new Error('请去邮箱中验证 | Please verify in the mailbox')
257257
throw new Error('用户不存在或密码错误 | User does not exist or incorrect password.')
258258
}
259-
259+
const config = await getCacheConfig()
260260
const token = jwt.sign({
261261
name: user.name ? user.name : user.email,
262262
avatar: user.avatar,
263263
description: user.description,
264264
userId: user._id,
265265
root: username.toLowerCase() === process.env.ROOT_USER,
266-
}, process.env.AUTH_SECRET_KEY)
266+
}, config.siteConfig.loginSalt.trim())
267267
res.send({ status: 'Success', message: '登录成功 | Login successfully', data: { token } })
268268
}
269269
catch (error) {

service/src/middleware/auth.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import jwt from 'jsonwebtoken'
2-
import { isNotEmptyString } from '../utils/is'
2+
import { getCacheConfig } from '../storage/config'
33

44
const auth = async (req, res, next) => {
5-
const AUTH_SECRET_KEY = process.env.AUTH_SECRET_KEY
6-
if (isNotEmptyString(AUTH_SECRET_KEY)) {
5+
const config = await getCacheConfig()
6+
if (config.siteConfig.loginEnabled) {
77
try {
88
const token = req.header('Authorization').replace('Bearer ', '')
9-
const info = jwt.verify(token, AUTH_SECRET_KEY.trim())
9+
const info = jwt.verify(token, config.siteConfig.loginSalt.trim())
1010
req.headers.userId = info.userId
1111
next()
1212
}

service/src/storage/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ObjectId } from 'mongodb'
2+
import { isNotEmptyString } from '../utils/is'
23
import { Config, MailConfig, SiteConfig } from './model'
34
import { getConfig } from './mongo'
45

@@ -34,6 +35,8 @@ export async function getOriginConfig() {
3435
process.env.HTTPS_PROXY,
3536
new SiteConfig(
3637
process.env.SITE_TITLE || 'ChatGpt Web',
38+
isNotEmptyString(process.env.AUTH_SECRET_KEY),
39+
process.env.AUTH_SECRET_KEY,
3740
process.env.REGISTER_ENABLED === 'true',
3841
process.env.REGISTER_MAILS,
3942
process.env.SITE_DOMAIN),
@@ -43,6 +46,12 @@ export async function getOriginConfig() {
4346
process.env.SMTP_USERNAME,
4447
process.env.SMTP_PASSWORD))
4548
}
49+
else {
50+
if (config.siteConfig.loginEnabled === undefined)
51+
config.siteConfig.loginEnabled = isNotEmptyString(process.env.AUTH_SECRET_KEY)
52+
if (config.siteConfig.loginSalt === undefined)
53+
config.siteConfig.loginSalt = process.env.AUTH_SECRET_KEY
54+
}
4655
return config
4756
}
4857

service/src/storage/model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export class Config {
9999
export class SiteConfig {
100100
constructor(
101101
public siteTitle?: string,
102+
public loginEnabled?: boolean,
103+
public loginSalt?: string,
102104
public registerEnabled?: boolean,
103105
public registerMails?: string,
104106
public siteDomain?: string,

src/components/common/Setting/Mail.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ onMounted(() => {
6262
<div class="flex items-center space-x-4">
6363
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.smtpTsl') }}</span>
6464
<div class="flex-1">
65-
<NInput
66-
:value="config && config.smtpTsl !== undefined ? String(config.smtpTsl) : undefined"
67-
placeholder=""
68-
@input="(val) => { if (config) config.smtpTsl = typeof val === 'string' ? Boolean(val) : undefined }"
65+
<NSwitch
66+
:round="false"
67+
:value="config && config.smtpTsl"
68+
@update:value="(val) => { if (config) config.smtpTsl = val }"
6969
/>
7070
</div>
7171
</div>

src/components/common/Setting/Site.vue

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang='ts'>
22
import { onMounted, ref } from 'vue'
3-
import { NButton, NInput, NSpin, useMessage } from 'naive-ui'
3+
import { NButton, NInput, NSpin, NSwitch, useMessage } from 'naive-ui'
44
import type { ConfigState } from './model'
55
import { SiteConfig } from './model'
66
import { fetchChatConfig, fetchUpdateSite } from '@/api'
@@ -61,12 +61,34 @@ onMounted(() => {
6161
</div>
6262
</div>
6363
<div class="flex items-center space-x-4">
64-
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.registerEnabled') }}</span>
64+
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.loginEnabled') }}</span>
65+
<div class="flex-1">
66+
<NSwitch
67+
:round="false"
68+
:value="config && config.loginEnabled"
69+
@update:value="(val) => { if (config) config.loginEnabled = val }"
70+
/>
71+
</div>
72+
</div>
73+
<div class="flex items-center space-x-4">
74+
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.loginSalt') }}</span>
6575
<div class="flex-1">
6676
<NInput
67-
:value="config && config.registerEnabled !== undefined ? String(config.registerEnabled) : undefined"
68-
placeholder=""
69-
@input="(val) => { if (config) config.registerEnabled = typeof val === 'string' ? Boolean(val) : undefined }"
77+
:value="config && config.loginSalt" placeholder=""
78+
@input="(val) => { if (config) config.loginSalt = val }"
79+
/>
80+
</div>
81+
<p>
82+
变更后会导致旧的登录失效
83+
</p>
84+
</div>
85+
<div class="flex items-center space-x-4">
86+
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.registerEnabled') }}</span>
87+
<div class="flex-1">
88+
<NSwitch
89+
:round="false"
90+
:value="config && config.registerEnabled"
91+
@update:value="(val) => { if (config) config.registerEnabled = val }"
7092
/>
7193
</div>
7294
</div>

src/components/common/Setting/model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export class ConfigState {
1414

1515
export class SiteConfig {
1616
siteTitle?: string
17+
loginEnabled?: boolean
18+
loginSalt?: string
1719
registerEnabled?: boolean
1820
registerMails?: string
1921
siteDomain?: string

src/locales/en-US.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export default {
8080
apiBaseUrl: 'Api Base Url',
8181
apiModel: 'Api Model',
8282
accessToken: 'Access Token',
83+
loginEnabled: 'Login Enabled',
84+
loginSalt: 'Login Salt',
8385
},
8486
store: {
8587
local: 'Local',

src/locales/zh-CN.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export default {
8080
apiBaseUrl: '接口地址',
8181
apiModel: 'Api 模型',
8282
accessToken: 'Access Token',
83+
loginEnabled: '注册登录',
84+
loginSalt: '登录混淆盐',
8385
},
8486
store: {
8587
local: '本地',

src/locales/zh-TW.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export default {
8080
apiBaseUrl: '接口地址',
8181
apiModel: 'Api 模型',
8282
accessToken: 'Access Token',
83+
loginEnabled: '注册登录',
84+
loginSalt: '登录混淆盐',
8385
},
8486
store: {
8587
local: '本機',

0 commit comments

Comments
 (0)