|
|
@ -1,6 +1,7 @@ |
|
|
|
package gateway |
|
|
|
|
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"fmt" |
|
|
|
"net/http" |
|
|
|
|
|
|
@ -52,7 +53,7 @@ func Login(client proto.UsersClient) http.HandlerFunc { |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
func Authorize(client proto.UsersClient, serverSecret *string) http.HandlerFunc { |
|
|
|
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, |
|
|
@ -66,12 +67,14 @@ func Authorize(client proto.UsersClient, serverSecret *string) http.HandlerFunc |
|
|
|
w.Write([]byte(err.Error())) |
|
|
|
} |
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
w.Write([]byte(fmt.Sprintf("%d", res.User.Id))) |
|
|
|
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, port *int) http.HandlerFunc { |
|
|
|
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{ |
|
|
@ -86,8 +89,8 @@ func ResetPassword(client proto.UsersClient, port *int) http.HandlerFunc { |
|
|
|
|
|
|
|
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))) |
|
|
|
"Please follow this link to update your password: %s/change_password?token=%s\n", |
|
|
|
*endpoint, res.Token.Token))) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|