Skip to content

Commit

Permalink
Revert "Fix user management and Auth"
Browse files Browse the repository at this point in the history
This reverts commit 31f938c.
  • Loading branch information
matborowczyk committed Feb 13, 2025
1 parent 31f938c commit 6bbe7bc
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
3 changes: 0 additions & 3 deletions changelogs/unreleased/buildmaster-fix-user-and-auth.yml

This file was deleted.

17 changes: 2 additions & 15 deletions src/Data/Auth/Providers/DatabaseAuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import {
createCookie,
Expand Down Expand Up @@ -31,23 +31,10 @@ export const DatabaseAuthProvider: React.FC<React.PropsWithChildren> = ({

const updateUser = (username: string, token: string) => {
setUser(username);
localStorage.setItem("inmanta_user", username);
createCookie("inmanta_user", token, 1);
};

const isDisabled = () => !getToken();

useEffect(() => {
// If user is not set and token is present, set the user from the local storage or logs out. case where there is an user but not token is handled automatically as lacks of token prompt use to login again
if (!user && getToken()) {
const username = localStorage.getItem("inmanta_user");
if (username) {
setUser(username);
} else {
logout();
}
}
}, [user]);
const isDisabled = () => !getUser();

return (
<AuthContext.Provider
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Managers/V2/Auth/AddUser/useAddUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { usePostWithoutEnv } from "../../helpers";
import { usePost } from "../../helpers";

interface AddUSerResponse {
data: {
Expand All @@ -19,7 +19,7 @@ interface AddUserBody {
*/
export const useAddUser = () => {
const client = useQueryClient();
const post = usePostWithoutEnv()<AddUserBody>;
const post = usePost()<AddUserBody>;

return useMutation<AddUSerResponse, Error, AddUserBody>({
mutationFn: (body) => post(`/api/v2/user`, body),
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Managers/V2/Auth/GetCurrentUser/useGetCurrentUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UseQueryResult, useQuery } from "@tanstack/react-query";
import { useGetWithoutEnv } from "../../helpers";
import { useGet } from "../../helpers";

interface LoggedUser {
username: string;
Expand All @@ -12,7 +12,7 @@ interface LoggedUser {
*/
export const useGetCurrentUser = () => {
const url = `/api/v2/current_user/`;
const get = useGetWithoutEnv()<{ data: LoggedUser }>;
const get = useGet()<{ data: LoggedUser }>;

return {
/**
Expand Down
29 changes: 26 additions & 3 deletions src/Data/Managers/V2/Auth/GetUsers/useGetUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* @returns An object containing a custom hook to fetch user information.
*/
import { useQuery } from "@tanstack/react-query";
import { useGetWithoutEnv } from "../../helpers";
import { PrimaryBaseUrlManager } from "@/UI";
import { useFetchHelpers } from "../../helpers";

/**
* Represents the user information.
Expand All @@ -18,7 +19,29 @@ export interface UserInfo {
* @returns An object containing a custom hook to fetch user information.
*/
export const useGetUsers = () => {
const get = useGetWithoutEnv()<{ data: UserInfo[] }>;
const { handleErrors, createHeaders } = useFetchHelpers();
const baseUrlManager = new PrimaryBaseUrlManager(
globalThis.location.origin,
globalThis.location.pathname,
);
const baseUrl = baseUrlManager.getBaseUrl(process.env.API_BASEURL);

/**
* Fetches the user information from the API.
* @returns A promise that resolves to an object containing the user information.
* @throws An error if the API request fails.
*/
const fetchUsers = async (): Promise<{
data: UserInfo[];
}> => {
const response = await fetch(`${baseUrl}/api/v2/user`, {
headers: createHeaders(),
});

await handleErrors(response);

return response.json();
};

return {
/**
Expand All @@ -28,7 +51,7 @@ export const useGetUsers = () => {
useOneTime: () =>
useQuery({
queryKey: ["get_users-one_time"],
queryFn: () => get("/api/v2/user"),
queryFn: fetchUsers,
retry: false,
select: (data) => data.data,
}),
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Managers/V2/Auth/RemoveUser/useRemoveUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
useMutation,
useQueryClient,
} from "@tanstack/react-query";
import { useDeleteWithoutEnv } from "../../helpers";
import { useDelete } from "../../helpers";

/**
* React Query hook for removing a user from the server.
Expand All @@ -17,7 +17,7 @@ export const useRemoveUser = (): UseMutationResult<
unknown
> => {
const client = useQueryClient();
const deleteFn = useDeleteWithoutEnv();
const deleteFn = useDelete();

return useMutation({
mutationFn: (username) => deleteFn(`/api/v2/user/${username}`),
Expand Down

0 comments on commit 6bbe7bc

Please sign in to comment.