user.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package client
  2. import (
  3. "context"
  4. "github.com/porter-dev/porter/api/types"
  5. )
  6. // AuthCheck performs a check to ensure that the user is logged in
  7. func (c *Client) AuthCheck(ctx context.Context) (*types.GetAuthenticatedUserResponse, error) {
  8. resp := &types.GetAuthenticatedUserResponse{}
  9. err := c.getRequest("/users/current", nil, resp)
  10. return resp, err
  11. }
  12. // Login authorizes the user and grants them a cookie-based session
  13. func (c *Client) Login(ctx context.Context, req *types.LoginUserRequest) (*types.GetAuthenticatedUserResponse, error) {
  14. resp := &types.GetAuthenticatedUserResponse{}
  15. err := c.postRequest("/login", req, resp)
  16. return resp, err
  17. }
  18. // Logout logs the user out and deauthorizes the cookie-based session
  19. func (c *Client) Logout(ctx context.Context) error {
  20. err := c.postRequest("/logout", nil, nil)
  21. if err != nil {
  22. return err
  23. }
  24. // remove the cookie, if it exists
  25. return c.deleteCookie()
  26. }
  27. // CreateUser will create the user, authorize the user and grant them a cookie-based session
  28. func (c *Client) CreateUser(
  29. ctx context.Context,
  30. req *types.CreateUserRequest,
  31. ) (*types.CreateUserResponse, error) {
  32. resp := &types.CreateUserResponse{}
  33. err := c.postRequest("/users", req, resp)
  34. return resp, err
  35. }
  36. // ListUserProjects returns a list of projects associated with a user
  37. func (c *Client) ListUserProjects(ctx context.Context) (*types.ListUserProjectsResponse, error) {
  38. resp := &types.ListUserProjectsResponse{}
  39. err := c.getRequest("/projects", nil, resp)
  40. return resp, err
  41. }
  42. // DeleteUser deletes the current user
  43. func (c *Client) DeleteUser(
  44. ctx context.Context,
  45. ) error {
  46. return c.deleteRequest("/users/current", nil, nil)
  47. }