Browse Source

feat: 清理全局配置和认证中不必要的代码,简化逻辑

shadcn-as
npmrun 3 weeks ago
parent
commit
7759bb3ff5
  1. 17
      app/composables/useGlobalConfig.ts
  2. 6
      app/middleware/auth.global.ts
  3. 2
      app/plugins/auth-session.server.ts
  4. 3
      app/utils/auth-routes.ts
  5. 2
      server/api/config/global.get.ts

17
app/composables/useGlobalConfig.ts

@ -3,14 +3,6 @@ import { request, unwrapApiBody, type ApiResponse } from '../utils/http/factory'
export type GlobalConfig = {
siteName: string
allowRegister: boolean
showDiscoverInHeaderForGuest: boolean
commentEmailNotifyEnabled: boolean
commentMailFromEmail: string
commentSmtpHost: string
commentSmtpPort: number
commentSmtpSecure: boolean
commentSmtpUser: string
commentSmtpPass: string
}
type GlobalConfigResult = {
@ -20,14 +12,6 @@ type GlobalConfigResult = {
const DEFAULT_GLOBAL_CONFIG: GlobalConfig = {
siteName: 'Person Panel',
allowRegister: true,
showDiscoverInHeaderForGuest: true,
commentEmailNotifyEnabled: false,
commentMailFromEmail: '',
commentSmtpHost: '',
commentSmtpPort: 465,
commentSmtpSecure: true,
commentSmtpUser: '',
commentSmtpPass: '',
}
export function useGlobalConfig() {
@ -55,7 +39,6 @@ export function useGlobalConfig() {
return n && n.length > 0 ? n : DEFAULT_GLOBAL_CONFIG.siteName
}),
allowRegister: computed(() => config.value.allowRegister),
showDiscoverInHeaderForGuest: computed(() => config.value.showDiscoverInHeaderForGuest),
pending,
error,
refresh,

6
app/middleware/auth.global.ts

@ -5,7 +5,6 @@ import {
normalizeSafeRedirect,
} from "@/utils/auth-routes";
import { useAuthSession } from "../composables/useAuthSession";
import { useGlobalConfig } from "../composables/useGlobalConfig";
export default defineNuxtRouteMiddleware(async (to) => {
if(to.path.startsWith("/__nuxt_error")) {
@ -25,11 +24,8 @@ export default defineNuxtRouteMiddleware(async (to) => {
const currentPath = to.path;
const currentFullPath = to.fullPath;
const isLoggedIn = loggedIn.value;
const { showDiscoverInHeaderForGuest } = useGlobalConfig();
const isDiscoverRoute = currentPath === "/discover" || currentPath.startsWith("/discover/");
const canGuestAccessDiscover = isDiscoverRoute && showDiscoverInHeaderForGuest.value;
if (!isLoggedIn && !isPublicRoute(currentPath) && !canGuestAccessDiscover) {
if (!isLoggedIn && !isPublicRoute(currentPath)) {
return navigateTo({
path: "/auth",
query: { redirect: currentFullPath },

2
app/plugins/auth-session.server.ts

@ -18,8 +18,6 @@ export default defineNuxtPlugin(async () => {
if (state.value.initialized) {
return;
}
console.log(useCookie);
const user = await getCurrentUser(event);
state.value = {
initialized: true,

3
app/utils/auth-routes.ts

@ -1,7 +1,6 @@
const PUBLIC_ROUTE_EXACT = new Set(["/", "/auth/login", "/auth/register"]);
const GUEST_ONLY_ROUTE_EXACT = new Set(["/auth/login", "/auth/register"]);
/** 公开主页 /@slug 与仅链接分享 /p/slug/t/token */
const PUBLIC_ROUTE_PREFIXES: string[] = ["/@", "/p/"];
const PUBLIC_ROUTE_PREFIXES: string[] = [];
export const DEFAULT_AUTHENTICATED_LANDING_PATH = "/";

2
server/api/config/global.get.ts

@ -20,7 +20,7 @@ export default defineWrappedResponseHandler(async (event) => {
const entries = await Promise.all(
keys.map(async (key) => {
const value = await getConfigGlobal(event, key);
const value = await getConfigGlobal(key);
const safeValue =
SECRET_MASKED_GLOBAL_CONFIG_KEYS.has(key)
? ""

Loading…
Cancel
Save