diff --git a/32-platform/sportsstore/admin/signout_handler.go b/32-platform/sportsstore/admin/signout_handler.go new file mode 100644 index 0000000..f56b902 --- /dev/null +++ b/32-platform/sportsstore/admin/signout_handler.go @@ -0,0 +1,24 @@ +package admin + +import ( + "platform/authorization/identity" + "platform/http/actionresults" + "platform/http/handing" + "sportstore/store" +) + +type SignOutHandler struct { + identity.User + handing.URLGenerator +} + +func (handler SignOutHandler) GetUserWidget() actionresults.ActionResult { + context := struct { + identity.User + SignOutUrl string + }{ + User: handler.User, + SignOutUrl: store.MustGenerateUrl(handler.URLGenerator, AuthenticationHandler.PostSignOut), + } + return actionresults.NewTemplateAction("user_widget.html", context) +} diff --git a/32-platform/sportsstore/admin/user_store.go b/32-platform/sportsstore/admin/user_store.go new file mode 100644 index 0000000..72613b9 --- /dev/null +++ b/32-platform/sportsstore/admin/user_store.go @@ -0,0 +1,56 @@ +package admin + +import ( + "platform/authorization/identity" + "platform/config" + "platform/http/actionresults" + "platform/http/handing" + "platform/sessions" + "sportstore/store" +) + +type AuthenticationHandler struct { + identity.User + identity.SignInManager + identity.UserStore + sessions.Session + config.Configuration + handing.URLGenerator +} + +type Credentials struct { + UserName string + Password string +} + +const SignInMsgKey string = "sign_in_message" + +func (handler AuthenticationHandler) GetSignIn() actionresults.ActionResult { + message := handler.Session.GetValueDefault(SignInMsgKey, "").(string) + return actionresults.NewTemplateAction("sign_in.html", message) +} + +func (handler AuthenticationHandler) PostSignIn(cred Credentials) actionresults.ActionResult { + if cred.Password == "123456" { + user, ok := handler.UserStore.GetUserByName(cred.UserName) + if ok { + handler.Session.SetValue(SignInMsgKey, "") + err := handler.SignInManager.SignIn(user) + if err != nil { + handler.Session.SetValue(SignInMsgKey, "Sign in failed:"+err.Error()) + return actionresults.NewRedirectAction(store.MustGenerateUrl(handler.URLGenerator, AuthenticationHandler.GetSignIn)) + } + return actionresults.NewRedirectAction("/admin/section/") + } + } + handler.Session.SetValue(SignInMsgKey, "Invalid username or password") + return actionresults.NewRedirectAction(store.MustGenerateUrl(handler.URLGenerator, AuthenticationHandler.GetSignIn)) +} + +func (handler AuthenticationHandler) PostSignOut() actionresults.ActionResult { + err := handler.SignInManager.SignOut(handler.User) + if err != nil { + return actionresults.NewRedirectAction("/") + } + return actionresults.NewRedirectAction("/") +} diff --git a/32-platform/sportsstore/config.json b/32-platform/sportsstore/config.json index fc44b72..34e1e8d 100644 --- a/32-platform/sportsstore/config.json +++ b/32-platform/sportsstore/config.json @@ -38,5 +38,8 @@ "SaveCategory": "sql/save_category.sql", "UpdateCategory": "sql/update_category.sql" } + }, + "authorization": { + "failUrl": "/signin" } } \ No newline at end of file diff --git a/32-platform/sportsstore/templates/admin.html b/32-platform/sportsstore/templates/admin.html index 0975755..798c223 100644 --- a/32-platform/sportsstore/templates/admin.html +++ b/32-platform/sportsstore/templates/admin.html @@ -12,6 +12,9 @@