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

View file

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

View file

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