|
|
package gateway
import ( "fmt" "net/http"
"git.chrishayward.xyz/x/users/proto" )
func Register(client proto.UsersClient) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { password, passwordAgain := r.URL.Query().Get("password"), r.URL.Query().Get("password_again") _, err := client.Register(r.Context(), &proto.RegisterRequest{ Form: &proto.UserForm{ Email: r.URL.Query().Get("email"), Password: &password, PasswordAgain: &passwordAgain, }, })
if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(err.Error())) }
w.WriteHeader(http.StatusOK) }) }
func Login(client proto.UsersClient) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { password, passwordAgain := r.URL.Query().Get("password"), r.URL.Query().Get("password_again") res, err := client.Login(r.Context(), &proto.LoginRequest{ Form: &proto.UserForm{ Email: r.URL.Query().Get("email"), Password: &password, PasswordAgain: &passwordAgain, }, })
if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(err.Error())) }
w.WriteHeader(http.StatusOK) w.Write([]byte(res.Token.Token)) }) }
func Authorize(client proto.UsersClient, serverSecret *string) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { res, err := client.Authorize(r.Context(), &proto.AuthorizeRequest{ Secret: *serverSecret, Token: &proto.UserToken{ Token: r.URL.Query().Get("token"), }, })
if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(err.Error())) }
w.WriteHeader(http.StatusOK) w.Write([]byte(fmt.Sprintf("%d", res.User.Id))) }) }
func ResetPassword(client proto.UsersClient) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { res, err := client.ResetPassword(r.Context(), &proto.ResetPasswordRequest{ Form: &proto.UserForm{ Email: r.URL.Query().Get("email"), }, })
if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(err.Error())) }
w.WriteHeader(http.StatusOK) w.Write([]byte(fmt.Sprintf( "Please follow this link to update your password: http://localhost:%d/change_password?token=%s\n", *port, res.Token.Token))) }) }
func ChangePassword(client proto.UsersClient) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { password, passwordAgain := r.URL.Query().Get("password"), r.URL.Query().Get("password_again") _, err := client.ChangePassword(r.Context(), &proto.ChangePasswordRequest{ Form: &proto.UserForm{ Email: r.URL.Query().Get("email"), Password: &password, PasswordAgain: &passwordAgain, }, })
if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(err.Error())) }
w.WriteHeader(http.StatusOK) }) }
|