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.

114 lines
2.9 KiB

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