feat: |Worker| multi language add messages (#587)

This commit is contained in:
Dream Hunter
2025-02-20 01:41:34 +08:00
committed by GitHub
parent b43353ea47
commit 241e0b7b28
6 changed files with 95 additions and 29 deletions

View File

@@ -1,6 +1,7 @@
import { Context } from 'hono';
import { Jwt } from 'hono/utils/jwt'
import i18n from '../i18n';
import { HonoCustomType } from '../types';
import { getJsonSetting } from '../utils';
import { UserOauth2Settings } from '../models';
@@ -11,22 +12,26 @@ export default {
getOauth2LoginUrl: async (c: Context<HonoCustomType>) => {
const settings = await getJsonSetting<UserOauth2Settings[]>(c, CONSTANTS.OAUTH2_SETTINGS_KEY);
const { clientID, state } = c.req.query();
const lang = c.get("lang") || c.env.DEFAULT_LANG;
const msgs = i18n.getMessages(lang);
const setting = settings?.find(s => s.clientID === clientID);
if (!setting) {
return c.text("Client not found", 400);
return c.text(msgs.Oauth2ClientIDNotFoundMsg, 400);
}
const url = `${setting.authorizationURL}?client_id=${setting.clientID}&response_type=code&redirect_uri=${setting.redirectURL}&scope=${setting.scope}&state=${state}`
return c.json({ url });
},
oauth2Login: async (c: Context<HonoCustomType>) => {
const { clientID, code } = await c.req.json<{ clientID?: string, code?: string }>();
const lang = c.get("lang") || c.env.DEFAULT_LANG;
const msgs = i18n.getMessages(lang);
if (!clientID || !code) {
return c.text("clientID or code is missing", 400);
return c.text(msgs.Oauth2CliendIDOrCodeMissingMsg, 400);
}
const settings = await getJsonSetting<UserOauth2Settings[]>(c, CONSTANTS.OAUTH2_SETTINGS_KEY);
const setting = settings?.find(s => s.clientID === clientID);
if (!setting) {
return c.text("Client not found", 400);
return c.text(msgs.Oauth2ClientIDNotFoundMsg, 400);
}
const params = {
code,
@@ -48,7 +53,7 @@ export default {
})
if (!res.ok) {
console.error(`Failed to get access token: ${res.status} ${res.statusText} ${await res.text()}`)
return c.text("Failed to get access token", 400);
return c.text(msgs.Oauth2FailedGetAccessTokenMsg, 400);
}
const resJson = await res.json();
const { access_token, token_type } = resJson as { access_token: string, token_type?: string };
@@ -61,17 +66,17 @@ export default {
})
if (!user.ok) {
console.error(`Failed to get user info: ${res.status} ${res.statusText} ${await res.text()}`)
return c.text("Failed to get user info", 400);
return c.text(msgs.Oauth2FailedGetUserInfoMsg, 400);
}
const userInfo = await user.json()
const { [setting.userEmailKey]: email } = userInfo as { [key: string]: string };
if (!email) {
return c.text("Failed to get user email", 400);
return c.text(msgs.Oauth2FailedGetUserEmailMsg, 400);
}
// check email in mail allow list
const mailDomain = email.split("@")[1];
if (setting.enableMailAllowList && !setting.mailAllowList?.includes(mailDomain)) {
return c.text(`Mail domain must in ${JSON.stringify(setting.mailAllowList, null, 2)}`, 400)
return c.text(`${msgs.UserMailDomainMustInMsg} ${JSON.stringify(setting.mailAllowList, null, 2)}`, 400)
}
// insert or update user
const { success } = await c.env.DB.prepare(
@@ -82,13 +87,13 @@ export default {
email, JSON.stringify(userInfo)
).run();
if (!success) {
return c.text("Failed to register", 500)
return c.text(msgs.FailedToRegisterMsg, 500)
}
const { id: user_id } = await c.env.DB.prepare(
`SELECT id FROM users where user_email = ?`
).bind(email).first() || {};
if (!user_id) {
return c.text("User not found", 400)
return c.text(msgs.UserNotFoundMsg, 400)
}
// create jwt
const jwt = await Jwt.sign({