2
0

user.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. err := c.postRequest(
  34. fmt.Sprintf(
  35. "/logout",
  36. ),
  37. nil,
  38. nil,
  39. )
  40. if err != nil {
  41. return err
  42. }
  43. // remove the cookie, if it exists
  44. return c.deleteCookie()
  45. }
  46. // CreateUser will create the user, authorize the user and grant them a cookie-based session
  47. func (c *Client) CreateUser(
  48. ctx context.Context,
  49. req *types.CreateUserRequest,
  50. ) (*types.CreateUserResponse, error) {
  51. resp := &types.CreateUserResponse{}
  52. err := c.postRequest(
  53. fmt.Sprintf(
  54. "/users",
  55. ),
  56. req,
  57. resp,
  58. )
  59. return resp, err
  60. }
  61. // ListUserProjects returns a list of projects associated with a user
  62. func (c *Client) ListUserProjects(ctx context.Context) (*types.ListUserProjectsResponse, error) {
  63. resp := &types.ListUserProjectsResponse{}
  64. err := c.getRequest(
  65. fmt.Sprintf(
  66. "/projects",
  67. ),
  68. nil,
  69. resp,
  70. )
  71. return resp, err
  72. }
  73. // DeleteUser deletes the current user
  74. func (c *Client) DeleteUser(
  75. ctx context.Context,
  76. ) error {
  77. return c.deleteRequest(
  78. fmt.Sprintf(
  79. "/users/current",
  80. ),
  81. nil,
  82. nil,
  83. )
  84. }