Changed so that username is required to get projects, so that you can get another user's projects (for admin stuff)

This commit is contained in:
Peter KW 2024-03-29 20:19:22 +01:00
parent 4ab23b3c3c
commit 8b6462abee
3 changed files with 12 additions and 21 deletions

View file

@ -4,15 +4,16 @@ import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v5"
"github.com/gofiber/fiber/v2/log"
)
// GetUserProjects returns all projects that the user is a member of
func GetUserProjects(c *fiber.Ctx) error {
// First we get the username from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
username := c.Params("username")
if username == "" {
log.Info("No username provided")
return c.Status(400).SendString("No username provided")
}
// Then dip into the database to get the projects
projects, err := db.GetDb(c).GetProjectsForUser(username)

View file

@ -112,7 +112,7 @@ func main() {
// All project related routes
// projectGroup := api.Group("/project") // Not currently in use
api.Get("/getUserProjects", projects.GetUserProjects)
api.Get("/getUserProjects/:username", projects.GetUserProjects)
api.Get("/project/:projectId", projects.GetProject)
api.Get("/checkIfProjectManager/:projectName", projects.IsProjectManagerHandler)
api.Get("/getUsersProject/:projectName", projects.ListAllUsersProject)

View file

@ -2,6 +2,7 @@ import { useState } from "react";
import { Project } from "../Types/goTypes";
import { useNavigate } from "react-router-dom";
import GetProjects from "./GetProjects";
import { api } from "../API/API";
/**
* Renders a component that displays the projects a user is a part of and links to the projects start-page.
@ -11,16 +12,10 @@ function DisplayUserProject(): JSX.Element {
const [projects, setProjects] = useState<Project[]>([]);
const navigate = useNavigate();
const getProjects = async (): Promise<void> => {
const token = localStorage.getItem("accessToken") ?? "";
const response = await api.getUserProjects(token);
console.log(response);
if (response.success) {
setProjects(response.data ?? []);
} else {
console.error(response.message);
}
};
GetProjects({
setProjectsProp: setProjects,
username: localStorage.getItem("username") ?? "",
});
const handleProjectClick = async (projectName: string): Promise<void> => {
const token = localStorage.getItem("accessToken") ?? "";
@ -37,11 +32,6 @@ function DisplayUserProject(): JSX.Element {
}
};
// Call getProjects when the component mounts
useEffect(() => {
void getProjects();
}, []);
return (
<>
<h1 className="font-bold text-[30px] mb-[20px]">Your Projects</h1>