auth.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import styles from "./auth.module.scss";
  2. import { IconButton } from "./button";
  3. import { useNavigate } from "react-router-dom";
  4. import { Path } from "../constant";
  5. import { useAccessStore, useAppConfig, useChatStore } from "../store";
  6. import Locale from "../locales";
  7. import BotIcon from "../icons/bot.svg";
  8. import { useEffect } from "react";
  9. import { getClientConfig } from "../config/client";
  10. export function AuthPage() {
  11. const navigate = useNavigate();
  12. const access = useAccessStore();
  13. const config = useAppConfig();
  14. const goHome = () => navigate(Path.Home);
  15. const goChat = () => navigate(Path.Chat);
  16. const resetAccessCode = () => {
  17. access.update((config) => (config.accessCode = ""));
  18. config.update((config) => (config.providerConfig.openai.apiKey = ""));
  19. }; // Reset access code to empty string
  20. useEffect(() => {
  21. if (getClientConfig()?.isApp) {
  22. navigate(Path.Settings);
  23. }
  24. // eslint-disable-next-line react-hooks/exhaustive-deps
  25. }, []);
  26. return (
  27. <div className={styles["auth-page"]}>
  28. <div className={`no-dark ${styles["auth-logo"]}`}>
  29. <BotIcon />
  30. </div>
  31. <div className={styles["auth-title"]}>{Locale.Auth.Title}</div>
  32. <div className={styles["auth-tips"]}>{Locale.Auth.Tips}</div>
  33. <input
  34. className={styles["auth-input"]}
  35. type="password"
  36. placeholder={Locale.Auth.Input}
  37. value={access.accessCode}
  38. onChange={(e) => {
  39. access.update(
  40. (config) => (config.accessCode = e.currentTarget.value),
  41. );
  42. }}
  43. />
  44. {!access.hideUserApiKey ? (
  45. <>
  46. <div className={styles["auth-tips"]}>{Locale.Auth.SubTips}</div>
  47. <input
  48. className={styles["auth-input"]}
  49. type="password"
  50. placeholder={Locale.Settings.Token.Placeholder}
  51. value={config.providerConfig.openai.apiKey}
  52. onChange={(e) => {
  53. config.update(
  54. (config) =>
  55. (config.providerConfig.openai.apiKey = e.currentTarget.value),
  56. );
  57. }}
  58. />
  59. </>
  60. ) : null}
  61. <div className={styles["auth-actions"]}>
  62. <IconButton
  63. text={Locale.Auth.Confirm}
  64. type="primary"
  65. onClick={goChat}
  66. />
  67. <IconButton
  68. text={Locale.Auth.Later}
  69. onClick={() => {
  70. resetAccessCode();
  71. goHome();
  72. }}
  73. />
  74. </div>
  75. </div>
  76. );
  77. }