authMessages.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // ---------------------------------------------------------------------------
  2. // Folder: lib/frontend
  3. // File: authMessages.js
  4. // Relative Path: lib/frontend/authMessages.js
  5. // ---------------------------------------------------------------------------
  6. import { ApiClientError } from "@/lib/frontend/apiClient";
  7. import { LOGIN_REASONS } from "@/lib/frontend/authRedirect";
  8. /**
  9. * Auth UI messages (RHL-020).
  10. *
  11. * Why this file exists:
  12. * - Keep user-facing auth messages consistent and centralized.
  13. * - Avoid string duplication across multiple UI components.
  14. * - Make mappings easy to test (pure functions, no UI dependencies).
  15. */
  16. /**
  17. * Return a user-facing login-page alert message for a known reason.
  18. *
  19. * @param {string|null} reason
  20. * @returns {{ title: string, description: string } | null}
  21. */
  22. export function getLoginReasonAlert(reason) {
  23. if (reason === LOGIN_REASONS.EXPIRED) {
  24. return {
  25. title: "Session expired",
  26. description: "Your session has expired. Please log in again.",
  27. };
  28. }
  29. if (reason === LOGIN_REASONS.LOGGED_OUT) {
  30. return {
  31. title: "Logged out",
  32. description: "You have been logged out successfully.",
  33. };
  34. }
  35. return null;
  36. }
  37. /**
  38. * Map an error to a safe, user-friendly login error message.
  39. *
  40. * Principles:
  41. * - Do not leak internal server details to the UI.
  42. * - Use stable error codes (ApiClientError.code) for UX decisions.
  43. *
  44. * @param {unknown} err
  45. * @returns {string}
  46. */
  47. export function getLoginErrorMessage(err) {
  48. if (err instanceof ApiClientError) {
  49. if (err.code === "AUTH_INVALID_CREDENTIALS") {
  50. return "Invalid username or password.";
  51. }
  52. if (err.code === "CLIENT_NETWORK_ERROR") {
  53. return "Network error. Please check your connection and try again.";
  54. }
  55. // Generic fallback for other known API client errors
  56. return "Login failed. Please try again.";
  57. }
  58. // Unknown error type
  59. return "Login failed. Please try again.";
  60. }