user.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package client
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/porter-dev/porter/api/types"
  6. )
  7. // AuthCheck performs a check to ensure that the user is logged in
  8. func (c *Client) AuthCheck(ctx context.Context) (*types.GetAuthenticatedUserResponse, error) {
  9. resp := &types.GetAuthenticatedUserResponse{}
  10. err := c.getRequest(
  11. fmt.Sprintf(
  12. "/users/current",
  13. ),
  14. nil,
  15. resp,
  16. )
  17. return resp, err
  18. }
  19. // Login authorizes the user and grants them a cookie-based session
  20. func (c *Client) Login(ctx context.Context, req *types.LoginUserRequest) (*types.GetAuthenticatedUserResponse, error) {
  21. resp := &types.GetAuthenticatedUserResponse{}
  22. err := c.postRequest(
  23. fmt.Sprintf(
  24. "/login",
  25. ),
  26. req,
  27. resp,
  28. )
  29. return resp, err
  30. }
  31. // Logout logs the user out and deauthorizes the cookie-based session
  32. func (c *Client) Logout(ctx context.Context) error {
  33. return c.postRequest(
  34. fmt.Sprintf(
  35. "/logout",
  36. ),
  37. nil,
  38. nil,
  39. )
  40. }
  41. // CreateUser will create the user, authorize the user and grant them a cookie-based session
  42. func (c *Client) CreateUser(
  43. ctx context.Context,
  44. req *types.CreateUserRequest,
  45. ) (*types.CreateUserResponse, error) {
  46. resp := &types.CreateUserResponse{}
  47. err := c.postRequest(
  48. fmt.Sprintf(
  49. "/users",
  50. ),
  51. req,
  52. resp,
  53. )
  54. return resp, err
  55. }
  56. // ListUserProjects returns a list of projects associated with a user
  57. func (c *Client) ListUserProjects(ctx context.Context) (*types.ListUserProjectsResponse, error) {
  58. resp := &types.ListUserProjectsResponse{}
  59. err := c.getRequest(
  60. fmt.Sprintf(
  61. "/projects",
  62. ),
  63. nil,
  64. resp,
  65. )
  66. return resp, err
  67. }
  68. // DeleteUser deletes the current user
  69. func (c *Client) DeleteUser(
  70. ctx context.Context,
  71. ) error {
  72. return c.deleteRequest(
  73. fmt.Sprintf(
  74. "/users/current",
  75. ),
  76. nil,
  77. nil,
  78. )
  79. }