GetUserProjects handler
This commit is contained in:
parent
e1153934c6
commit
aa7c5de1fc
2 changed files with 24 additions and 6 deletions
|
@ -11,12 +11,12 @@ import (
|
|||
|
||||
// The actual interface that we will use
|
||||
type GlobalState interface {
|
||||
Register(c *fiber.Ctx) error // To register a new user
|
||||
UserDelete(c *fiber.Ctx) error // To delete a user
|
||||
Login(c *fiber.Ctx) error // To get the token
|
||||
LoginRenew(c *fiber.Ctx) error // To renew the token
|
||||
CreateProject(c *fiber.Ctx) error // To create a new project
|
||||
// GetProjects(c *fiber.Ctx) error // To get all projects
|
||||
Register(c *fiber.Ctx) error // To register a new user
|
||||
UserDelete(c *fiber.Ctx) error // To delete a user
|
||||
Login(c *fiber.Ctx) error // To get the token
|
||||
LoginRenew(c *fiber.Ctx) error // To renew the token
|
||||
CreateProject(c *fiber.Ctx) error // To create a new project
|
||||
GetUserProjects(c *fiber.Ctx) error // To get all projects
|
||||
// GetProject(c *fiber.Ctx) error // To get a specific project
|
||||
// UpdateProject(c *fiber.Ctx) error // To update a project
|
||||
// DeleteProject(c *fiber.Ctx) error // To delete a project
|
||||
|
@ -163,3 +163,20 @@ func (gs *GState) CreateProject(c *fiber.Ctx) error {
|
|||
|
||||
return c.Status(200).SendString("Project added")
|
||||
}
|
||||
|
||||
// GetUserProjects returns all projects that the user is a member of
|
||||
func (gs *GState) 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)
|
||||
|
||||
// Then dip into the database to get the projects
|
||||
projects, err := gs.Db.GetProjectsForUser(username)
|
||||
if err != nil {
|
||||
return c.Status(500).SendString(err.Error())
|
||||
}
|
||||
|
||||
// Return a json serialized list of projects
|
||||
return c.JSON(projects)
|
||||
}
|
||||
|
|
|
@ -69,6 +69,7 @@ func main() {
|
|||
SigningKey: jwtware.SigningKey{Key: []byte("secret")},
|
||||
}))
|
||||
|
||||
server.Get("/api/getUserProjects", gs.GetUserProjects)
|
||||
server.Post("/api/loginrenew", gs.LoginRenew)
|
||||
server.Delete("/api/userdelete", gs.UserDelete) // Perhaps just use POST to avoid headaches
|
||||
server.Post("/api/project", gs.CreateProject)
|
||||
|
|
Loading…
Add table
Reference in a new issue