Skip to content

Commit

Permalink
feat: 开启登录
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerwin committed Mar 22, 2023
1 parent fba52bc commit 5c4259a
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chatgpt-web",
"version": "2.10.7",
"version": "2.11.0",
"private": false,
"description": "ChatGPT Web",
"author": "ChenZhaoYu <[email protected]>",
Expand Down
8 changes: 4 additions & 4 deletions service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ router.post('/config', auth, async (req, res) => {

router.post('/session', async (req, res) => {
try {
const AUTH_SECRET_KEY = process.env.AUTH_SECRET_KEY
const hasAuth = typeof AUTH_SECRET_KEY === 'string' && AUTH_SECRET_KEY.length > 0
const config = await getCacheConfig()
const hasAuth = config.siteConfig.loginEnabled
const allowRegister = (await getCacheConfig()).siteConfig.registerEnabled
res.send({ status: 'Success', message: '', data: { auth: hasAuth, allowRegister, model: currentModel() } })
}
Expand All @@ -256,14 +256,14 @@ router.post('/user-login', async (req, res) => {
throw new Error('请去邮箱中验证 | Please verify in the mailbox')
throw new Error('用户不存在或密码错误 | User does not exist or incorrect password.')
}

const config = await getCacheConfig()
const token = jwt.sign({
name: user.name ? user.name : user.email,
avatar: user.avatar,
description: user.description,
userId: user._id,
root: username.toLowerCase() === process.env.ROOT_USER,
}, process.env.AUTH_SECRET_KEY)
}, config.siteConfig.loginSalt.trim())
res.send({ status: 'Success', message: '登录成功 | Login successfully', data: { token } })
}
catch (error) {
Expand Down
8 changes: 4 additions & 4 deletions service/src/middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import jwt from 'jsonwebtoken'
import { isNotEmptyString } from '../utils/is'
import { getCacheConfig } from '../storage/config'

const auth = async (req, res, next) => {
const AUTH_SECRET_KEY = process.env.AUTH_SECRET_KEY
if (isNotEmptyString(AUTH_SECRET_KEY)) {
const config = await getCacheConfig()
if (config.siteConfig.loginEnabled) {
try {
const token = req.header('Authorization').replace('Bearer ', '')
const info = jwt.verify(token, AUTH_SECRET_KEY.trim())
const info = jwt.verify(token, config.siteConfig.loginSalt.trim())
req.headers.userId = info.userId
next()
}
Expand Down
9 changes: 9 additions & 0 deletions service/src/storage/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ObjectId } from 'mongodb'
import { isNotEmptyString } from '../utils/is'
import { Config, MailConfig, SiteConfig } from './model'
import { getConfig } from './mongo'

Expand Down Expand Up @@ -34,6 +35,8 @@ export async function getOriginConfig() {
process.env.HTTPS_PROXY,
new SiteConfig(
process.env.SITE_TITLE || 'ChatGpt Web',
isNotEmptyString(process.env.AUTH_SECRET_KEY),
process.env.AUTH_SECRET_KEY,
process.env.REGISTER_ENABLED === 'true',
process.env.REGISTER_MAILS,
process.env.SITE_DOMAIN),
Expand All @@ -43,6 +46,12 @@ export async function getOriginConfig() {
process.env.SMTP_USERNAME,
process.env.SMTP_PASSWORD))
}
else {
if (config.siteConfig.loginEnabled === undefined)
config.siteConfig.loginEnabled = isNotEmptyString(process.env.AUTH_SECRET_KEY)
if (config.siteConfig.loginSalt === undefined)
config.siteConfig.loginSalt = process.env.AUTH_SECRET_KEY
}
return config
}

Expand Down
2 changes: 2 additions & 0 deletions service/src/storage/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export class Config {
export class SiteConfig {
constructor(
public siteTitle?: string,
public loginEnabled?: boolean,
public loginSalt?: string,
public registerEnabled?: boolean,
public registerMails?: string,
public siteDomain?: string,
Expand Down
8 changes: 4 additions & 4 deletions src/components/common/Setting/Mail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ onMounted(() => {
<div class="flex items-center space-x-4">
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.smtpTsl') }}</span>
<div class="flex-1">
<NInput
:value="config && config.smtpTsl !== undefined ? String(config.smtpTsl) : undefined"
placeholder=""
@input="(val) => { if (config) config.smtpTsl = typeof val === 'string' ? Boolean(val) : undefined }"
<NSwitch
:round="false"
:value="config && config.smtpTsl"
@update:value="(val) => { if (config) config.smtpTsl = val }"
/>
</div>
</div>
Expand Down
32 changes: 27 additions & 5 deletions src/components/common/Setting/Site.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang='ts'>
import { onMounted, ref } from 'vue'
import { NButton, NInput, NSpin, useMessage } from 'naive-ui'
import { NButton, NInput, NSpin, NSwitch, useMessage } from 'naive-ui'
import type { ConfigState } from './model'
import { SiteConfig } from './model'
import { fetchChatConfig, fetchUpdateSite } from '@/api'
Expand Down Expand Up @@ -61,12 +61,34 @@ onMounted(() => {
</div>
</div>
<div class="flex items-center space-x-4">
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.registerEnabled') }}</span>
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.loginEnabled') }}</span>
<div class="flex-1">
<NSwitch
:round="false"
:value="config && config.loginEnabled"
@update:value="(val) => { if (config) config.loginEnabled = val }"
/>
</div>
</div>
<div class="flex items-center space-x-4">
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.loginSalt') }}</span>
<div class="flex-1">
<NInput
:value="config && config.registerEnabled !== undefined ? String(config.registerEnabled) : undefined"
placeholder=""
@input="(val) => { if (config) config.registerEnabled = typeof val === 'string' ? Boolean(val) : undefined }"
:value="config && config.loginSalt" placeholder=""
@input="(val) => { if (config) config.loginSalt = val }"
/>
</div>
<p>
变更后会导致旧的登录失效
</p>
</div>
<div class="flex items-center space-x-4">
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.registerEnabled') }}</span>
<div class="flex-1">
<NSwitch
:round="false"
:value="config && config.registerEnabled"
@update:value="(val) => { if (config) config.registerEnabled = val }"
/>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/components/common/Setting/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export class ConfigState {

export class SiteConfig {
siteTitle?: string
loginEnabled?: boolean
loginSalt?: string
registerEnabled?: boolean
registerMails?: string
siteDomain?: string
Expand Down
2 changes: 2 additions & 0 deletions src/locales/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export default {
apiBaseUrl: 'Api Base Url',
apiModel: 'Api Model',
accessToken: 'Access Token',
loginEnabled: 'Login Enabled',
loginSalt: 'Login Salt',
},
store: {
local: 'Local',
Expand Down
2 changes: 2 additions & 0 deletions src/locales/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export default {
apiBaseUrl: '接口地址',
apiModel: 'Api 模型',
accessToken: 'Access Token',
loginEnabled: '注册登录',
loginSalt: '登录混淆盐',
},
store: {
local: '本地',
Expand Down
2 changes: 2 additions & 0 deletions src/locales/zh-TW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export default {
apiBaseUrl: '接口地址',
apiModel: 'Api 模型',
accessToken: 'Access Token',
loginEnabled: '注册登录',
loginSalt: '登录混淆盐',
},
store: {
local: '本機',
Expand Down

0 comments on commit 5c4259a

Please sign in to comment.