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
117 lines
3.0 KiB
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"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, next http.HandlerFunc) 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()))
|
|
}
|
|
|
|
ctx := context.WithValue(r.Context(), "user", res.User)
|
|
ctx = context.WithValue(ctx, "roles", res.Roles)
|
|
|
|
next(w, r.WithContext(ctx))
|
|
})
|
|
}
|
|
|
|
func ResetPassword(client proto.UsersClient, endpoint string) 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: %s/change_password?token=%s\n",
|
|
*endpoint, 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)
|
|
})
|
|
}
|