Auth.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { createContext, useEffect, useState } from 'react';
  2. import { AuthContextType } from './Types';
  3. import { MilvusService } from '@/http';
  4. import {
  5. MILVUS_CLIENT_ID,
  6. MILVUS_URL,
  7. MILVUS_DATABASE,
  8. ATTU_AUTH_REQ,
  9. } from '@/consts';
  10. import type { AuthReq } from '@server/types';
  11. export const authContext = createContext<AuthContextType>({
  12. clientId: '',
  13. authReq: {
  14. username: '',
  15. password: '',
  16. address: '',
  17. token: '',
  18. database: '',
  19. checkHealth: true,
  20. clientId: '',
  21. ssl: false,
  22. },
  23. setAuthReq: () => {},
  24. isManaged: false,
  25. isServerless: false,
  26. isDedicated: false,
  27. isAuth: false,
  28. login: async () => {
  29. return { clientId: '', database: '' };
  30. },
  31. logout: () => {},
  32. });
  33. const { Provider } = authContext;
  34. export const AuthProvider = (props: { children: React.ReactNode }) => {
  35. // get data from local storage
  36. const localClientId = window.localStorage.getItem(MILVUS_CLIENT_ID) || '';
  37. const localAuthReq = JSON.parse(
  38. window.localStorage.getItem(ATTU_AUTH_REQ) ||
  39. JSON.stringify({
  40. username: '',
  41. password: '',
  42. address: MILVUS_URL,
  43. token: '',
  44. database: MILVUS_DATABASE,
  45. checkHealth: true,
  46. clientId: localClientId,
  47. })
  48. );
  49. // state
  50. const [authReq, setAuthReq] = useState<AuthReq>(localAuthReq);
  51. const [clientId, setClientId] = useState<string>(localClientId);
  52. // update local storage when authReq changes
  53. useEffect(() => {
  54. // store auth request in local storage
  55. window.localStorage.setItem(
  56. ATTU_AUTH_REQ,
  57. JSON.stringify({ ...authReq, password: '', token: '' })
  58. );
  59. // set title
  60. document.title = authReq.address ? `${authReq.address} - Attu` : 'Attu';
  61. }, [authReq]);
  62. // login API
  63. const login = async (params: AuthReq) => {
  64. // create a new client id
  65. params.clientId = Math.random().toString(36).substring(7);
  66. // save clientId to local storage
  67. // console.log('params.clientId', params.clientId);
  68. window.localStorage.setItem(MILVUS_CLIENT_ID, params.clientId);
  69. // connect to Milvus
  70. const res = await MilvusService.connect(params);
  71. // update auth request
  72. setAuthReq({ ...params, database: res.database });
  73. setClientId(res.clientId);
  74. // save clientId to local storage
  75. window.localStorage.setItem(MILVUS_CLIENT_ID, res.clientId);
  76. return res;
  77. };
  78. // logout API
  79. const logout = async (pass?: boolean) => {
  80. if (!pass) {
  81. // close connetion
  82. await MilvusService.closeConnection();
  83. }
  84. // clear client id
  85. setClientId('');
  86. // remove client id from local storage
  87. window.localStorage.removeItem(MILVUS_CLIENT_ID);
  88. };
  89. const isManaged = authReq.address.includes('zilliz');
  90. const isServerless = isManaged && authReq.address.includes('serverless');
  91. return (
  92. <Provider
  93. value={{
  94. authReq,
  95. setAuthReq,
  96. login,
  97. logout,
  98. clientId,
  99. isAuth: !!clientId,
  100. isManaged: isManaged,
  101. isServerless: isServerless,
  102. isDedicated: !isServerless && isManaged,
  103. }}
  104. >
  105. {props.children}
  106. </Provider>
  107. );
  108. };