From 6680064cd557c946bde65c98c6ad4cfc920d638c Mon Sep 17 00:00:00 2001 From: Lee Brown Date: Sun, 4 Aug 2019 23:27:02 -0800 Subject: [PATCH] fix signup validation --- cmd/web-app/handlers/routes.go | 8 ++ cmd/web-app/handlers/signup.go | 2 +- .../templates/content/users-update.gohtml | 83 ++++++++++++++++++ .../templates/content/users-view.gohtml | 85 +++++++++++++++++++ internal/user_account/user.go | 4 +- 5 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 cmd/web-app/templates/content/users-update.gohtml create mode 100644 cmd/web-app/templates/content/users-view.gohtml diff --git a/cmd/web-app/handlers/routes.go b/cmd/web-app/handlers/routes.go index 1a7b65e..b627d95 100644 --- a/cmd/web-app/handlers/routes.go +++ b/cmd/web-app/handlers/routes.go @@ -40,6 +40,7 @@ func APP(shutdown chan os.Signal, log *log.Logger, env webcontext.Env, staticDir // Construct the web.App which holds all routes as well as common Middleware. app := web.NewApp(shutdown, log, env, middlewares...) + // Register project management pages. p := Projects{ MasterDB: masterDB, @@ -63,6 +64,7 @@ func APP(shutdown chan os.Signal, log *log.Logger, env webcontext.Env, staticDir app.Handle("GET", "/users/:user_id/update", us.Update, mid.AuthenticateSessionRequired(authenticator), mid.HasRole(auth.RoleAdmin)) app.Handle("GET", "/users/:user_id", us.View, mid.AuthenticateSessionRequired(authenticator), mid.HasRole(auth.RoleAdmin)) + // Register user management and authentication endpoints. u := User{ MasterDB: masterDB, @@ -92,6 +94,7 @@ func APP(shutdown chan os.Signal, log *log.Logger, env webcontext.Env, staticDir app.Handle("POST", "/user", u.View, mid.AuthenticateSessionRequired(authenticator), mid.HasAuth()) app.Handle("GET", "/user", u.View, mid.AuthenticateSessionRequired(authenticator), mid.HasAuth()) + // Register account management endpoints. acc := Account{ MasterDB: masterDB, @@ -103,6 +106,7 @@ func APP(shutdown chan os.Signal, log *log.Logger, env webcontext.Env, staticDir app.Handle("POST", "/account", acc.View, mid.AuthenticateSessionRequired(authenticator), mid.HasRole(auth.RoleAdmin)) app.Handle("GET", "/account", acc.View, mid.AuthenticateSessionRequired(authenticator), mid.HasRole(auth.RoleAdmin)) + // Register user management and authentication endpoints. s := Signup{ MasterDB: masterDB, @@ -121,6 +125,7 @@ func APP(shutdown chan os.Signal, log *log.Logger, env webcontext.Env, staticDir app.Handle("GET", "/examples/flash-messages", ex.FlashMessages) app.Handle("GET", "/examples/images", ex.Images) + // Register geo g := Geo{ MasterDB: masterDB, @@ -131,6 +136,7 @@ func APP(shutdown chan os.Signal, log *log.Logger, env webcontext.Env, staticDir app.Handle("GET", "/geo/geonames/postal_code/:postalCode", g.GeonameByPostalCode) app.Handle("GET", "/geo/country/:countryCode/timezones", g.CountryTimezones) + // Register root r := Root{ MasterDB: masterDB, @@ -146,6 +152,7 @@ func APP(shutdown chan os.Signal, log *log.Logger, env webcontext.Env, staticDir app.Handle("GET", "/index.html", r.IndexHtml) app.Handle("GET", "/robots.txt", r.RobotTxt) + // Register health check endpoint. This route is not authenticated. check := Check{ MasterDB: masterDB, @@ -154,6 +161,7 @@ func APP(shutdown chan os.Signal, log *log.Logger, env webcontext.Env, staticDir } app.Handle("GET", "/v1/health", check.Health) + // Handle static files/pages. Render a custom 404 page when file not found. static := func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error { err := web.StaticHandler(ctx, w, r, params, staticDir, "") diff --git a/cmd/web-app/handlers/signup.go b/cmd/web-app/handlers/signup.go index 0a10d8d..43a2a41 100644 --- a/cmd/web-app/handlers/signup.go +++ b/cmd/web-app/handlers/signup.go @@ -112,7 +112,7 @@ func (h *Signup) Step1(ctx context.Context, w http.ResponseWriter, r *http.Reque data["form"] = req - if verr, ok := weberror.NewValidationError(ctx, webcontext.Validator().Struct(signup.SignupRequest{})); ok { + if verr, ok := weberror.NewValidationError(ctx, signup.Validator().Struct(signup.SignupRequest{})); ok { data["validationDefaults"] = verr.(*weberror.Error) } diff --git a/cmd/web-app/templates/content/users-update.gohtml b/cmd/web-app/templates/content/users-update.gohtml new file mode 100644 index 0000000..75a9eda --- /dev/null +++ b/cmd/web-app/templates/content/users-update.gohtml @@ -0,0 +1,83 @@ +{{define "title"}}Update Profile{{end}} +{{define "style"}} + +{{end}} +{{define "content"}} +
+
+
+
+ + + {{template "invalid-feedback" dict "validationDefaults" $.userValidationDefaults "validationErrors" $.validationErrors "fieldName" "FirstName" }} +
+
+ + + {{template "invalid-feedback" dict "validationDefaults" $.userValidationDefaults "validationErrors" $.validationErrors "fieldName" "LastName" }} +
+
+ + + {{template "invalid-feedback" dict "validationDefaults" $.userValidationDefaults "validationErrors" $.validationErrors "fieldName" "Email" }} +
+
+ + + {{template "invalid-feedback" dict "validationDefaults" $.validationDefaults "validationErrors" $.validationErrors "fieldName" "Timezone" }} +
+
+
+ +
+
+

Change Password

+

Optional. You can change your password by specifying a new one below. Otherwise leave the fields empty.

+
+ + + Generate random password + {{template "invalid-feedback" dict "validationDefaults" $.passwordValidationDefaults "validationErrors" $.validationErrors "fieldName" "Password" }} +
+
+ + + {{template "invalid-feedback" dict "validationDefaults" $.passwordValidationDefaults "validationErrors" $.validationErrors "fieldName" "PasswordConfirm" }} +
+
+
+
+
+
+ +
+
+
+{{end}} +{{define "js"}} + +{{end}} diff --git a/cmd/web-app/templates/content/users-view.gohtml b/cmd/web-app/templates/content/users-view.gohtml new file mode 100644 index 0000000..373eaf6 --- /dev/null +++ b/cmd/web-app/templates/content/users-view.gohtml @@ -0,0 +1,85 @@ +{{define "title"}}Profile{{end}} +{{define "style"}} + +{{end}} +{{define "content"}} +
+
+
+
+ gravatar image +
+
+

Name

+

+ {{ .user.Name }} +

+
+
+
+

Update Avatar

+
+ +
+ +
+ +
+
+

+ Name
+ {{ .user.Name }} +

+

+ Email
+ {{ .user.Email }} +

+ {{if .user.Timezone }} +

+ Timezone
+ {{.user.Timezone }} +

+ {{end}} +
+
+
+

+ Role
+ {{ if .userAccount }} + + {{ range $r := .userAccount.Roles }} + {{ if eq $r "admin" }} + {{ $r }} + {{else}} + {{ $r }} + {{end}} + {{ end }} + + {{ end }} +

+

+ Status
+ {{ if .userAccount }} + + {{ if eq .userAccount.Status.Value "active" }} + {{ .userAccount.Status.Title }} + {{ else if eq .userAccount.Status.Value "invited" }} + {{ .userAccount.Status.Title }} + {{else}} + {{.userAccount.Status.Title }} + {{end}} + + {{ end }} +

+

+ ID
+ {{ .user.ID }} +

+
+
+{{end}} +{{define "js"}} + +{{end}} diff --git a/internal/user_account/user.go b/internal/user_account/user.go index a36efa9..7738007 100644 --- a/internal/user_account/user.go +++ b/internal/user_account/user.go @@ -2,8 +2,8 @@ package user_account import ( "context" - "geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext" + "geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext" "geeks-accelerator/oss/saas-starter-kit/internal/platform/auth" "github.com/jmoiron/sqlx" "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" @@ -11,7 +11,7 @@ import ( // UserFindByAccount lists all the users for a given account ID. func UserFindByAccount(ctx context.Context, claims auth.Claims, dbConn *sqlx.DB, req UserFindByAccountRequest) (Users, error) { - span, ctx := tracer.StartSpanFromContext(ctx, "internal.user_account.UserFind") + span, ctx := tracer.StartSpanFromContext(ctx, "internal.user_account.UserFindByAccount") defer span.Finish() v := webcontext.Validator()