diff --git a/public/locales/de.json b/public/locales/de.json index 1ff41fba..7314b677 100644 --- a/public/locales/de.json +++ b/public/locales/de.json @@ -487,6 +487,19 @@ }, "navbar": { "project_name": "Maxun", + "notifications": { + "success": { + "logout": "Erfolgreich abgemeldet" + }, + "errors": { + "logout": { + "unauthorized": "Sie sind nicht berechtigt, diese Aktion durchzuführen", + "server": "Serverfehler beim Abmelden", + "network": "Netzwerkfehler beim Abmelden", + "unknown": "Ein unerwarteter Fehler ist beim Abmelden aufgetreten" + } + } + }, "upgrade": { "button": "Upgrade", "modal": { diff --git a/public/locales/en.json b/public/locales/en.json index 883892a6..703ec9c0 100644 --- a/public/locales/en.json +++ b/public/locales/en.json @@ -498,6 +498,19 @@ }, "navbar": { "project_name": "Maxun", + "notifications": { + "success": { + "logout": "Logged out successfully" + }, + "errors": { + "logout": { + "unauthorized": "You are not authorized to perform this action", + "server": "Server error occurred during logout", + "network": "Network error while logging out", + "unknown": "An unexpected error occurred during logout" + } + } + }, "upgrade": { "button": "Upgrade", "modal": { diff --git a/public/locales/es.json b/public/locales/es.json index cd79d2cd..ca4edc5d 100644 --- a/public/locales/es.json +++ b/public/locales/es.json @@ -488,6 +488,19 @@ }, "navbar": { "project_name": "Maxun", + "notifications": { + "success": { + "logout": "Sesión cerrada exitosamente" + }, + "errors": { + "logout": { + "unauthorized": "No estás autorizado para realizar esta acción", + "server": "Error del servidor durante el cierre de sesión", + "network": "Error de red al cerrar sesión", + "unknown": "Ocurrió un error inesperado al cerrar sesión" + } + } + }, "upgrade": { "button": "Actualizar", "modal": { diff --git a/public/locales/ja.json b/public/locales/ja.json index d2fe42ea..a4a66143 100644 --- a/public/locales/ja.json +++ b/public/locales/ja.json @@ -488,6 +488,19 @@ }, "navbar": { "project_name": "Maxun", + "notifications": { + "success": { + "logout": "ログアウトに成功しました" + }, + "errors": { + "logout": { + "unauthorized": "この操作を実行する権限がありません", + "server": "ログアウト中にサーバーエラーが発生しました", + "network": "ログアウト中にネットワークエラーが発生しました", + "unknown": "ログアウト中に予期せぬエラーが発生しました" + } + } + }, "upgrade": { "button": "アップグレード", "modal": { diff --git a/public/locales/zh.json b/public/locales/zh.json index e7c58660..c92ed186 100644 --- a/public/locales/zh.json +++ b/public/locales/zh.json @@ -488,6 +488,19 @@ }, "navbar": { "project_name": "Maxun", + "notifications": { + "success": { + "logout": "退出登录成功" + }, + "errors": { + "logout": { + "unauthorized": "您没有执行此操作的权限", + "server": "退出登录时发生服务器错误", + "network": "退出登录时发生网络错误", + "unknown": "退出登录时发生未知错误" + } + } + }, "upgrade": { "button": "升级", "modal": { diff --git a/server/src/routes/auth.ts b/server/src/routes/auth.ts index 47eced63..f18ee8c1 100644 --- a/server/src/routes/auth.ts +++ b/server/src/routes/auth.ts @@ -140,14 +140,38 @@ router.post("/login", async (req, res) => { } }); -router.get("/logout", async (req, res) => { - try { - res.clearCookie("token"); - return res.json({ message: "Logout successful" }); - } catch (error: any) { - res.status(500).send(`Could not logout user - ${error.message}`); +router.get( + "/logout", + requireSignIn, + async (req: Request, res) => { + const authenticatedReq = req as AuthenticatedRequest; + try { + if (!authenticatedReq.user) { + return res.status(401).json({ + ok: false, + message: "Unauthorized", + code: "unauthorized" + }); + } + + res.clearCookie("token"); + + return res.status(200).json({ + ok: true, + message: "Logged out successfully", + code: "success" + }); + } catch (error) { + console.error('Logout error:', error); + return res.status(500).json({ + ok: false, + message: "Error during logout", + code: "server", + error: process.env.NODE_ENV === 'development' ? error : undefined + }); + } } -}); +); router.get( "/current-user", diff --git a/src/components/dashboard/NavBar.tsx b/src/components/dashboard/NavBar.tsx index 8a9b1e9c..2456d013 100644 --- a/src/components/dashboard/NavBar.tsx +++ b/src/components/dashboard/NavBar.tsx @@ -108,11 +108,39 @@ export const NavBar: React.FC = ({ }; const logout = async () => { - dispatch({ type: "LOGOUT" }); - window.localStorage.removeItem("user"); - const { data } = await axios.get(`${apiUrl}/auth/logout`); - notify("success", data.message); - navigate("/login"); + try { + const { data } = await axios.get(`${apiUrl}/auth/logout`); + if (data.ok) { + dispatch({ type: "LOGOUT" }); + window.localStorage.removeItem("user"); + notify('success', t('navbar.notifications.success.logout')); + navigate("/login"); + } + } catch (error: any) { + const status = error.response?.status; + let errorKey = 'unknown'; + + switch (status) { + case 401: + errorKey = 'unauthorized'; + break; + case 500: + errorKey = 'server'; + break; + default: + if (error.message?.includes('Network Error')) { + errorKey = 'network'; + } + } + + notify( + 'error', + t(`navbar.notifications.errors.logout.${errorKey}`, { + error: error.response?.data?.message || error.message + }) + ); + navigate("/login"); + } }; const goToMainMenu = async () => {