vormiaqueryjs
Version:
Vormia Query Js - A npm package for query management with VormiaPHP laravel Backend application
1 lines • 7.93 kB
Source Map (JSON)
{"version":3,"file":"useVrmAuth.mjs","sources":["../../../src/hooks/useVrmAuth.js"],"sourcesContent":["import { useQuery, useMutation } from \"@tanstack/react-query\";\nimport { getGlobalVormiaClient } from \"../client/createVormiaClient\";\n\n/**\n * Hook for authenticated queries with encryption\n * @param {Object} options - Query options\n * @param {string} options.endpoint - API endpoint\n * @param {string} [options.method='GET'] - HTTP method\n * @param {Object} [options.params] - Query parameters\n * @param {Object} [options.data] - Request body\n * @param {Object} [options.headers] - Custom headers\n * @param {Function} [options.transform] - Transform function for response data\n * @param {boolean} [options.encryptData=false] - Whether to encrypt the request data\n * @param {boolean} [options.storeToken=true] - Whether to store the auth token\n * @returns {Object} Query result\n */\nexport const useVormiaQueryAuth = (options) => {\n const client = getGlobalVormiaClient();\n\n const {\n endpoint,\n method = \"GET\",\n params,\n data,\n headers = {},\n transform,\n encryptData = false,\n storeToken = true,\n ...queryOptions\n } = options;\n\n const queryKey = [endpoint, method, params, data];\n\n const queryFn = async () => {\n try {\n const config = {\n method,\n url: endpoint,\n params: method === \"GET\" ? params : undefined,\n data: method !== \"GET\" ? data : undefined,\n headers: {\n \"Content-Type\": \"application/json\",\n ...headers,\n },\n encryptData,\n };\n\n const response = await client.request(config);\n\n // Store token if present in response\n if (storeToken && response.data?.token) {\n client.setAuthToken(response.data.token);\n }\n\n if (transform && typeof transform === \"function\") {\n return {\n ...response,\n data: transform(response.data),\n };\n }\n\n return response;\n } catch (error) {\n // Clear token on 401\n if (error.status === 401) {\n client.removeAuthToken();\n }\n throw error instanceof Error\n ? error\n : new Error(\"Authentication query failed\");\n }\n };\n\n return useQuery({\n queryKey,\n queryFn,\n retry: (failureCount, error) => {\n // Don't retry on 401\n if (error.status === 401) return false;\n return failureCount < 3; // Retry up to 3 times\n },\n ...queryOptions,\n });\n};\n\n/**\n * Hook for authentication mutations (login, register, etc.)\n * @param {Object} options - Mutation options\n * @param {string} options.endpoint - API endpoint\n * @param {string} [options.method='POST'] - HTTP method\n * @param {Object} [options.headers] - Custom headers\n * @param {Function} [options.transform] - Transform function for response data\n * @param {boolean} [options.encryptData=false] - Whether to encrypt the request data\n * @param {boolean} [options.storeToken=true] - Whether to store the auth token\n * @param {Function} [options.onLoginSuccess] - Callback on successful login\n * @returns {Object} Mutation result and auth utilities\n */\nexport const useVormiaQueryAuthMutation = (options) => {\n const client = getGlobalVormiaClient();\n\n const {\n endpoint,\n method = \"POST\",\n headers = {},\n transform,\n encryptData = false,\n storeToken = true,\n onLoginSuccess,\n onSuccess,\n onError,\n ...mutationOptions\n } = options;\n\n const mutation = useMutation({\n mutationFn: async (variables) => {\n try {\n const config = {\n method,\n url: endpoint,\n data: variables,\n headers: {\n \"Content-Type\": \"application/json\",\n ...headers,\n },\n encryptData,\n };\n\n const response = await client.request(config);\n\n // Store token if present in response\n if (storeToken && response.data?.token) {\n client.setAuthToken(response.data.token);\n }\n\n if (transform && typeof transform === \"function\") {\n return {\n ...response,\n data: transform(response.data),\n };\n }\n\n return response;\n } catch (error) {\n // Clear token on 401\n if (error.status === 401) {\n client.removeAuthToken();\n }\n throw error instanceof Error\n ? error\n : new Error(\"Authentication failed\");\n }\n },\n onSuccess: (data, variables, context) => {\n if (onSuccess) {\n onSuccess(data, variables, context);\n }\n if (onLoginSuccess && data.data?.token) {\n onLoginSuccess(data);\n }\n },\n onError: (error, variables, context) => {\n if (onError) {\n onError(error, variables, context);\n }\n },\n ...mutationOptions,\n });\n\n // Login helper\n const login = async (credentials) => {\n return mutation.mutateAsync(credentials);\n };\n\n // Logout helper\n const logout = () => {\n client.removeAuthToken();\n };\n\n // Check if user is authenticated\n const isAuthenticated = !!client.getAuthToken();\n\n return {\n ...mutation,\n login,\n logout,\n isAuthenticated,\n };\n};\n\n/**\n * Hook for checking authentication status\n * @returns {Object} Authentication status\n */\nexport const useAuthStatus = () => {\n const client = getGlobalVormiaClient();\n const isAuthen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