@@ -11,8 +11,9 @@ import { Status } from './storage/model'
11
11
import { clearChat , createChatRoom , createUser , deleteChat , deleteChatRoom , existsChatRoom , getChat , getChatRooms , getChats , getUser , getUserById , insertChat , renameChatRoom , updateChat , updateConfig , updateUserInfo , verifyUser } from './storage/mongo'
12
12
import { limiter } from './middleware/limiter'
13
13
import { isNotEmptyString } from './utils/is'
14
- import { sendMail } from './utils/mail'
14
+ import { sendTestMail , sendVerifyMail } from './utils/mail'
15
15
import { checkUserVerify , getUserVerifyUrl , md5 } from './utils/security'
16
+ import { rootAuth } from './middleware/rootAuth'
16
17
17
18
const app = express ( )
18
19
const router = express . Router ( )
@@ -182,41 +183,46 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
182
183
} )
183
184
184
185
router . post ( '/user-register' , async ( req , res ) => {
185
- const { username, password } = req . body as { username : string ; password : string }
186
- const config = await getCacheConfig ( )
187
- if ( ! config . siteConfig . registerEnabled ) {
188
- res . send ( { status : 'Fail' , message : '注册账号功能未启用 | Register account is disabled!' , data : null } )
189
- return
190
- }
191
- if ( isNotEmptyString ( config . siteConfig . registerMails ) ) {
192
- let allowSuffix = false
193
- const emailSuffixs = config . siteConfig . registerMails . split ( ',' )
194
- for ( let index = 0 ; index < emailSuffixs . length ; index ++ ) {
195
- const element = emailSuffixs [ index ]
196
- allowSuffix = username . toLowerCase ( ) . endsWith ( element )
197
- if ( allowSuffix )
198
- break
199
- }
200
- if ( ! allowSuffix ) {
201
- res . send ( { status : 'Fail' , message : '该邮箱后缀不支持 | The email service provider is not allowed' , data : null } )
186
+ try {
187
+ const { username, password } = req . body as { username : string ; password : string }
188
+ const config = await getCacheConfig ( )
189
+ if ( ! config . siteConfig . registerEnabled ) {
190
+ res . send ( { status : 'Fail' , message : '注册账号功能未启用 | Register account is disabled!' , data : null } )
202
191
return
203
192
}
204
- }
193
+ if ( isNotEmptyString ( config . siteConfig . registerMails ) ) {
194
+ let allowSuffix = false
195
+ const emailSuffixs = config . siteConfig . registerMails . split ( ',' )
196
+ for ( let index = 0 ; index < emailSuffixs . length ; index ++ ) {
197
+ const element = emailSuffixs [ index ]
198
+ allowSuffix = username . toLowerCase ( ) . endsWith ( element )
199
+ if ( allowSuffix )
200
+ break
201
+ }
202
+ if ( ! allowSuffix ) {
203
+ res . send ( { status : 'Fail' , message : '该邮箱后缀不支持 | The email service provider is not allowed' , data : null } )
204
+ return
205
+ }
206
+ }
205
207
206
- const user = await getUser ( username )
207
- if ( user != null ) {
208
- res . send ( { status : 'Fail' , message : '邮箱已存在 | The email exists' , data : null } )
209
- return
210
- }
211
- const newPassword = md5 ( password )
212
- await createUser ( username , newPassword )
208
+ const user = await getUser ( username )
209
+ if ( user != null ) {
210
+ res . send ( { status : 'Fail' , message : '邮箱已存在 | The email exists' , data : null } )
211
+ return
212
+ }
213
+ const newPassword = md5 ( password )
214
+ await createUser ( username , newPassword )
213
215
214
- if ( username . toLowerCase ( ) === process . env . ROOT_USER ) {
215
- res . send ( { status : 'Success' , message : '注册成功 | Register success' , data : null } )
216
+ if ( username . toLowerCase ( ) === process . env . ROOT_USER ) {
217
+ res . send ( { status : 'Success' , message : '注册成功 | Register success' , data : null } )
218
+ }
219
+ else {
220
+ await sendVerifyMail ( username , await getUserVerifyUrl ( username ) )
221
+ res . send ( { status : 'Success' , message : '注册成功, 去邮箱中验证吧 | Registration is successful, you need to go to email verification' , data : null } )
222
+ }
216
223
}
217
- else {
218
- await sendMail ( username , await getUserVerifyUrl ( username ) )
219
- res . send ( { status : 'Success' , message : '注册成功, 去邮箱中验证吧 | Registration is successful, you need to go to email verification' , data : null } )
224
+ catch ( error ) {
225
+ res . send ( { status : 'Fail' , message : error . message , data : null } )
220
226
}
221
227
} )
222
228
@@ -307,18 +313,13 @@ router.post('/verify', async (req, res) => {
307
313
}
308
314
} )
309
315
310
- router . post ( '/setting-base' , auth , async ( req , res ) => {
316
+ router . post ( '/setting-base' , rootAuth , async ( req , res ) => {
311
317
try {
312
318
const { apiKey, apiModel, apiBaseUrl, accessToken, timeoutMs, socksProxy, httpsProxy } = req . body as Config
313
- const userId = new ObjectId ( req . headers . userId . toString ( ) )
314
319
315
320
if ( apiKey == null && accessToken == null )
316
321
throw new Error ( 'Missing OPENAI_API_KEY or OPENAI_ACCESS_TOKEN environment variable.' )
317
322
318
- const user = await getUserById ( userId )
319
- if ( user == null || user . status !== Status . Normal || user . email . toLowerCase ( ) !== process . env . ROOT_USER )
320
- throw new Error ( '无权限 | No permission.' )
321
-
322
323
const thisConfig = await getOriginConfig ( )
323
324
thisConfig . apiKey = apiKey
324
325
thisConfig . apiModel = apiModel
@@ -338,14 +339,9 @@ router.post('/setting-base', auth, async (req, res) => {
338
339
}
339
340
} )
340
341
341
- router . post ( '/setting-site' , auth , async ( req , res ) => {
342
+ router . post ( '/setting-site' , rootAuth , async ( req , res ) => {
342
343
try {
343
344
const config = req . body as SiteConfig
344
- const userId = new ObjectId ( req . headers . userId . toString ( ) )
345
-
346
- const user = await getUserById ( userId )
347
- if ( user == null || user . status !== Status . Normal || user . email . toLowerCase ( ) !== process . env . ROOT_USER )
348
- throw new Error ( '无权限 | No permission.' )
349
345
350
346
const thisConfig = await getOriginConfig ( )
351
347
thisConfig . siteConfig = config
@@ -358,14 +354,9 @@ router.post('/setting-site', auth, async (req, res) => {
358
354
}
359
355
} )
360
356
361
- router . post ( '/setting-mail' , auth , async ( req , res ) => {
357
+ router . post ( '/setting-mail' , rootAuth , async ( req , res ) => {
362
358
try {
363
359
const config = req . body as MailConfig
364
- const userId = new ObjectId ( req . headers . userId . toString ( ) )
365
-
366
- const user = await getUserById ( userId )
367
- if ( user == null || user . status !== Status . Normal || user . email . toLowerCase ( ) !== process . env . ROOT_USER )
368
- throw new Error ( '无权限 | No permission.' )
369
360
370
361
const thisConfig = await getOriginConfig ( )
371
362
thisConfig . mailConfig = config
@@ -378,6 +369,19 @@ router.post('/setting-mail', auth, async (req, res) => {
378
369
}
379
370
} )
380
371
372
+ router . post ( '/mail-test' , rootAuth , async ( req , res ) => {
373
+ try {
374
+ const config = req . body as MailConfig
375
+ const userId = new ObjectId ( req . headers . userId as string )
376
+ const user = await getUserById ( userId )
377
+ await sendTestMail ( user . email , config )
378
+ res . send ( { status : 'Success' , message : '发送成功 | Successfully' , data : null } )
379
+ }
380
+ catch ( error ) {
381
+ res . send ( { status : 'Fail' , message : error . message , data : null } )
382
+ }
383
+ } )
384
+
381
385
app . use ( '' , router )
382
386
app . use ( '/api' , router )
383
387
app . set ( 'trust proxy' , 1 )
0 commit comments