From 6830de63854be1c9f76d40b7d65d4537c41e9b82 Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Thu, 10 Jun 2021 14:18:07 +0200 Subject: [PATCH] feat: login success page (#1849) * feat: login success page * fix: script import on mfa verify --- internal/ui/login/handler/callback_handler.go | 11 ----- .../ui/login/handler/login_success_handler.go | 45 +++++++++++++++++++ internal/ui/login/handler/renderer.go | 3 +- internal/ui/login/handler/router.go | 2 + internal/ui/login/static/i18n/de.yaml | 5 +++ internal/ui/login/static/i18n/en.yaml | 5 +++ .../static/resources/scripts/login_success.js | 17 +++++++ .../login/static/templates/login_success.html | 32 +++++++++++++ .../ui/login/static/templates/mfa_verify.html | 4 +- 9 files changed, 110 insertions(+), 14 deletions(-) delete mode 100644 internal/ui/login/handler/callback_handler.go create mode 100644 internal/ui/login/handler/login_success_handler.go create mode 100644 internal/ui/login/static/resources/scripts/login_success.js create mode 100644 internal/ui/login/static/templates/login_success.html diff --git a/internal/ui/login/handler/callback_handler.go b/internal/ui/login/handler/callback_handler.go deleted file mode 100644 index 0877c31911..0000000000 --- a/internal/ui/login/handler/callback_handler.go +++ /dev/null @@ -1,11 +0,0 @@ -package handler - -import ( - "github.com/caos/zitadel/internal/domain" - "net/http" -) - -func (l *Login) redirectToCallback(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest) { - callback := l.oidcAuthCallbackURL + authReq.ID - http.Redirect(w, r, callback, http.StatusFound) -} diff --git a/internal/ui/login/handler/login_success_handler.go b/internal/ui/login/handler/login_success_handler.go new file mode 100644 index 0000000000..a25be5ba70 --- /dev/null +++ b/internal/ui/login/handler/login_success_handler.go @@ -0,0 +1,45 @@ +package handler + +import ( + "net/http" + + "github.com/caos/zitadel/internal/domain" +) + +const ( + tmplLoginSuccess = "login_success" +) + +type loginSuccessData struct { + userData + RedirectURI string `schema:"redirect-uri"` +} + +func (l *Login) redirectToLoginSuccess(w http.ResponseWriter, r *http.Request, id string) { + http.Redirect(w, r, l.renderer.pathPrefix+EndpointLoginSuccess+"?authRequestID="+id, http.StatusFound) +} + +func (l *Login) handleLoginSuccess(w http.ResponseWriter, r *http.Request) { + authRequest, _ := l.getAuthRequest(r) + if authRequest != nil { + if !(len(authRequest.PossibleSteps) == 1 && authRequest.PossibleSteps[0].Type() == domain.NextStepRedirectToCallback) { + l.renderNextStep(w, r, authRequest) + return + } + } + l.renderSuccessAndCallback(w, r, authRequest, nil) +} + +func (l *Login) renderSuccessAndCallback(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest, err error) { + var errType, errMessage string + if err != nil { + errMessage = l.getErrorMessage(r, err) + } + data := loginSuccessData{ + userData: l.getUserData(r, authReq, "Login Successful", errType, errMessage), + } + if authReq != nil { + data.RedirectURI = l.oidcAuthCallbackURL + } + l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplLoginSuccess], data, nil) +} diff --git a/internal/ui/login/handler/renderer.go b/internal/ui/login/handler/renderer.go index c87d485fbf..98559036ca 100644 --- a/internal/ui/login/handler/renderer.go +++ b/internal/ui/login/handler/renderer.go @@ -66,6 +66,7 @@ func CreateRenderer(pathPrefix string, staticDir http.FileSystem, staticStorage tmplChangeUsernameDone: "change_username_done.html", tmplLinkUsersDone: "link_users_done.html", tmplExternalNotFoundOption: "external_not_found_option.html", + tmplLoginSuccess: "login_success.html", } funcs := map[string]interface{}{ "resourceUrl": func(file string) string { @@ -252,7 +253,7 @@ func (l *Login) chooseNextStep(w http.ResponseWriter, r *http.Request, authReq * l.chooseNextStep(w, r, authReq, 1, err) return } - l.redirectToCallback(w, r, authReq) + l.redirectToLoginSuccess(w, r, authReq.ID) case *domain.ChangePasswordStep: l.renderChangePassword(w, r, authReq, err) case *domain.VerifyEMailStep: diff --git a/internal/ui/login/handler/router.go b/internal/ui/login/handler/router.go index 3f6b7f9b13..e5464cbb6b 100644 --- a/internal/ui/login/handler/router.go +++ b/internal/ui/login/handler/router.go @@ -35,6 +35,7 @@ const ( EndpointExternalRegisterCallback = "/register/externalidp/callback" EndpointRegisterOrg = "/register/org" EndpointLogoutDone = "/logout/done" + EndpointLoginSuccess = "/login/success" EndpointExternalNotFoundOption = "/externaluser/option" EndpointResources = "/resources" @@ -82,5 +83,6 @@ func CreateRouter(login *Login, staticDir http.FileSystem, interceptors ...mux.M router.PathPrefix(EndpointResources).Handler(login.handleResources(staticDir)).Methods(http.MethodGet) router.HandleFunc(EndpointRegisterOrg, login.handleRegisterOrg).Methods(http.MethodGet) router.HandleFunc(EndpointRegisterOrg, login.handleRegisterOrgCheck).Methods(http.MethodPost) + router.HandleFunc(EndpointLoginSuccess, login.handleLoginSuccess).Methods(http.MethodGet) return router } diff --git a/internal/ui/login/static/i18n/de.yaml b/internal/ui/login/static/i18n/de.yaml index 22b4d2f2a6..b711ea488a 100644 --- a/internal/ui/login/static/i18n/de.yaml +++ b/internal/ui/login/static/i18n/de.yaml @@ -200,6 +200,11 @@ ExternalNotFoundOption: Link: Verlinken AutoRegister: Automatisches registrieren +LoginSuccess: + Title: Erfolgreich eingeloggt + AutoRedirect: Du wirst automatisch zurück in die Applikation geleitet. Danach kannst du diese Fenster schliessen. + Redirected: Du kannst diese Fenster nun schliessen. + LogoutDone: Title: Ausgeloggt Description: Du wurdest erfolgreich ausgeloggt. diff --git a/internal/ui/login/static/i18n/en.yaml b/internal/ui/login/static/i18n/en.yaml index 3d1ad44c96..220484cdb4 100644 --- a/internal/ui/login/static/i18n/en.yaml +++ b/internal/ui/login/static/i18n/en.yaml @@ -190,6 +190,11 @@ RegistrationOrg: PrivacyLinkText: privacy policy PrivacyLink: https://zitadel.ch/pdf/privacy.pdf +LoginSuccess: + Title: Login successful + AutoRedirect: You will be directed back to your application automatically. If not, click on the button below. You can close the window afterwards. + Redirected: You can now close this window. + LogoutDone: Title: Logged out Description: You have logged out successfully. diff --git a/internal/ui/login/static/resources/scripts/login_success.js b/internal/ui/login/static/resources/scripts/login_success.js new file mode 100644 index 0000000000..b0ea74b9cd --- /dev/null +++ b/internal/ui/login/static/resources/scripts/login_success.js @@ -0,0 +1,17 @@ +document.addEventListener('DOMContentLoaded', function () { + autoSubmit(); +}); + +function autoSubmit() { + let form = document.getElementsByTagName('form')[0]; + if (form) { + let button = document.getElementById("redirect-button"); + if (button) { + button.addEventListener("click", function (event) { + location.reload(); + event.preventDefault(); + }); + } + form.submit(); + } +} diff --git a/internal/ui/login/static/templates/login_success.html b/internal/ui/login/static/templates/login_success.html new file mode 100644 index 0000000000..7052f76772 --- /dev/null +++ b/internal/ui/login/static/templates/login_success.html @@ -0,0 +1,32 @@ +{{template "main-top" .}} + + +
+

{{t "LoginSuccess.Title"}}

+ + {{ template "user-profile" . }} + + {{if .RedirectURI}} +

{{t "LoginSuccess.AutoRedirect"}}

+
+ +
+ + + + {{ template "error-message" .}} + +
+ + +
+ +
+ + +{{else}} +

{{t "LoginSuccess.Redirected"}}

+ +{{end}} + +{{template "main-bottom" .}} \ No newline at end of file diff --git a/internal/ui/login/static/templates/mfa_verify.html b/internal/ui/login/static/templates/mfa_verify.html index c3967db401..de63956de9 100644 --- a/internal/ui/login/static/templates/mfa_verify.html +++ b/internal/ui/login/static/templates/mfa_verify.html @@ -43,6 +43,6 @@ {{ end }} - - + + {{template "main-bottom" .}} \ No newline at end of file