You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.0 KiB

  1. package gateway
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "git.chrishayward.xyz/x/users/proto"
  7. )
  8. func Register(client proto.UsersClient) http.HandlerFunc {
  9. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. password, passwordAgain :=
  11. r.URL.Query().Get("password"),
  12. r.URL.Query().Get("password_again")
  13. _, err := client.Register(r.Context(), &proto.RegisterRequest{
  14. Form: &proto.UserForm{
  15. Email: r.URL.Query().Get("email"),
  16. Password: &password,
  17. PasswordAgain: &passwordAgain,
  18. },
  19. })
  20. if err != nil {
  21. w.WriteHeader(http.StatusBadRequest)
  22. w.Write([]byte(err.Error()))
  23. }
  24. w.WriteHeader(http.StatusOK)
  25. })
  26. }
  27. func Login(client proto.UsersClient) http.HandlerFunc {
  28. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  29. password, passwordAgain :=
  30. r.URL.Query().Get("password"),
  31. r.URL.Query().Get("password_again")
  32. res, err := client.Login(r.Context(), &proto.LoginRequest{
  33. Form: &proto.UserForm{
  34. Email: r.URL.Query().Get("email"),
  35. Password: &password,
  36. PasswordAgain: &passwordAgain,
  37. },
  38. })
  39. if err != nil {
  40. w.WriteHeader(http.StatusBadRequest)
  41. w.Write([]byte(err.Error()))
  42. }
  43. w.WriteHeader(http.StatusOK)
  44. w.Write([]byte(res.Token.Token))
  45. })
  46. }
  47. func Authorize(client proto.UsersClient, serverSecret *string, next http.HandlerFunc) http.HandlerFunc {
  48. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  49. res, err := client.Authorize(r.Context(), &proto.AuthorizeRequest{
  50. Secret: *serverSecret,
  51. Token: &proto.UserToken{
  52. Token: r.URL.Query().Get("token"),
  53. },
  54. })
  55. if err != nil {
  56. w.WriteHeader(http.StatusBadRequest)
  57. w.Write([]byte(err.Error()))
  58. }
  59. ctx := context.WithValue(r.Context(), "user", res.User)
  60. ctx = context.WithValue(ctx, "roles", res.Roles)
  61. next(w, r.WithContext(ctx))
  62. })
  63. }
  64. func ResetPassword(client proto.UsersClient, endpoint string) http.HandlerFunc {
  65. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  66. res, err := client.ResetPassword(r.Context(), &proto.ResetPasswordRequest{
  67. Form: &proto.UserForm{
  68. Email: r.URL.Query().Get("email"),
  69. },
  70. })
  71. if err != nil {
  72. w.WriteHeader(http.StatusBadRequest)
  73. w.Write([]byte(err.Error()))
  74. }
  75. w.WriteHeader(http.StatusOK)
  76. w.Write([]byte(fmt.Sprintf(
  77. "Please follow this link to update your password: %s/change_password?token=%s\n",
  78. *endpoint, res.Token.Token)))
  79. })
  80. }
  81. func ChangePassword(client proto.UsersClient) http.HandlerFunc {
  82. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  83. password, passwordAgain :=
  84. r.URL.Query().Get("password"),
  85. r.URL.Query().Get("password_again")
  86. _, err := client.ChangePassword(r.Context(), &proto.ChangePasswordRequest{
  87. Form: &proto.UserForm{
  88. Email: r.URL.Query().Get("email"),
  89. Password: &password,
  90. PasswordAgain: &passwordAgain,
  91. },
  92. })
  93. if err != nil {
  94. w.WriteHeader(http.StatusBadRequest)
  95. w.Write([]byte(err.Error()))
  96. }
  97. w.WriteHeader(http.StatusOK)
  98. })
  99. }