diff --git a/server/middleware/auth.ts b/server/middleware/auth.ts index f8f7b9ade..68869222f 100644 --- a/server/middleware/auth.ts +++ b/server/middleware/auth.ts @@ -5,36 +5,35 @@ import { getSettings } from '../lib/settings'; export const checkUser: Middleware = async (req, _res, next) => { const settings = getSettings(); + let user: User | undefined; if (req.header('X-API-Key') === settings.main.apiKey) { const userRepository = getRepository(User); let userId = 1; // Work on original administrator account - // If a User ID is provided, we will act on that users behalf + // If a User ID is provided, we will act on that user's behalf if (req.header('X-API-User')) { userId = Number(req.header('X-API-User')); } - const user = await userRepository.findOne({ where: { id: userId } }); - if (user) { - req.user = user; - } + user = await userRepository.findOne({ where: { id: userId } }); } else if (req.session?.userId) { const userRepository = getRepository(User); - const user = await userRepository.findOne({ + user = await userRepository.findOne({ where: { id: req.session.userId }, }); + } - if (user) { - req.user = user; - req.locale = user.settings?.locale - ? user.settings?.locale - : settings.main.locale; - } + if (user) { + req.user = user; } + req.locale = user?.settings?.locale + ? user.settings.locale + : settings.main.locale; + next(); }; diff --git a/src/components/Layout/index.tsx b/src/components/Layout/index.tsx index 2f2f8be1a..662868354 100644 --- a/src/components/Layout/index.tsx +++ b/src/components/Layout/index.tsx @@ -3,9 +3,6 @@ import { ArrowLeftIcon, InformationCircleIcon } from '@heroicons/react/solid'; import { useRouter } from 'next/router'; import React, { useEffect, useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; -import { AvailableLocale } from '../../context/LanguageContext'; -import useLocale from '../../hooks/useLocale'; -import useSettings from '../../hooks/useSettings'; import { Permission, useUser } from '../../hooks/useUser'; import SearchInput from './SearchInput'; import Sidebar from './Sidebar'; @@ -19,21 +16,9 @@ const messages = defineMessages({ const Layout: React.FC = ({ children }) => { const [isSidebarOpen, setSidebarOpen] = useState(false); const [isScrolled, setIsScrolled] = useState(false); - const { user, hasPermission } = useUser(); + const { hasPermission } = useUser(); const router = useRouter(); const intl = useIntl(); - const { currentSettings } = useSettings(); - const { setLocale } = useLocale(); - - useEffect(() => { - if (setLocale) { - setLocale( - (user?.settings?.locale - ? user.settings.locale - : currentSettings.locale) as AvailableLocale - ); - } - }, [setLocale, currentSettings.locale, user]); useEffect(() => { const updateScrolled = () => { diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 4e73f9bb1..592d1dcb8 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -90,6 +90,14 @@ const CoreApp: Omit = ({ const [loadedMessages, setMessages] = useState(messages); const [currentLocale, setLocale] = useState(locale); + useEffect(() => { + setLocale( + (user?.settings?.locale + ? user.settings.locale + : currentSettings.locale) as AvailableLocale + ); + }, [currentSettings.locale, user?.settings?.locale]); + useEffect(() => { loadLocaleData(currentLocale).then(setMessages); }, [currentLocale]);