Make login and logout preserve what page you're on

This commit is contained in:
Ben Visness 2021-06-11 19:48:03 -05:00
parent b6c611004c
commit e2f85708db
6 changed files with 23 additions and 9 deletions

View File

@ -63,7 +63,7 @@ func TestLoginPage(t *testing.T) {
} }
func TestLogoutAction(t *testing.T) { func TestLogoutAction(t *testing.T) {
AssertRegexMatch(t, BuildLogoutAction(), RegexLogoutAction, nil) AssertRegexMatch(t, BuildLogoutAction(""), RegexLogoutAction, nil)
} }
func TestRegister(t *testing.T) { func TestRegister(t *testing.T) {

View File

@ -66,9 +66,12 @@ func BuildLoginPage(redirectTo string) string {
var RegexLogoutAction = regexp.MustCompile("^/logout$") var RegexLogoutAction = regexp.MustCompile("^/logout$")
func BuildLogoutAction() string { func BuildLogoutAction(redir string) string {
defer CatchPanic() defer CatchPanic()
return Url("/logout", nil) if redir == "" {
redir = "/"
}
return Url("/logout", []Q{{"redirect", redir}})
} }
var RegexRegister = regexp.MustCompile("^/_register$") var RegexRegister = regexp.MustCompile("^/_register$")

View File

@ -5,7 +5,7 @@
<a class="admin-panel" href="{{ .Header.AdminUrl }}"><span class="icon-settings"> Admin</span></a> <a class="admin-panel" href="{{ .Header.AdminUrl }}"><span class="icon-settings"> Admin</span></a>
{{ end }} {{ end }}
<a class="username settings" href="{{ .Header.MemberSettingsUrl }}"><span class="icon-settings"></span> {{ .User.Username }}</a> <a class="username settings" href="{{ .Header.MemberSettingsUrl }}"><span class="icon-settings"></span> {{ .User.Username }}</a>
<a class="logout" href="{{ .Header.LogoutActionUrl }}"><span class="icon-logout"></span> Logout</a> <a class="logout" href="{{ .Header.LogoutActionUrl }}"><span class="icon-logout"></span> Log Out</a>
{{ else }} {{ else }}
<a class="register" id="register-link" href="{{ .Header.RegisterUrl }}">Register</a> <a class="register" id="register-link" href="{{ .Header.RegisterUrl }}">Register</a>
<a class="login" id="login-link" href="{{ .LoginPageUrl }}">Log in</a> <a class="login" id="login-link" href="{{ .LoginPageUrl }}">Log in</a>
@ -23,6 +23,7 @@
</tr> </tr>
{{/* TODO: Forgot password flow? Or just on standalone page? */}} {{/* TODO: Forgot password flow? Or just on standalone page? */}}
</table> </table>
<input type="hidden" name="redirect" value="{{ $.CurrentUrl }}">
<div class="actionbar pt2"> <div class="actionbar pt2">
<input type="submit" value="Log In" /> <input type="submit" value="Log In" />
</div> </div>

View File

@ -14,6 +14,7 @@ type BaseData struct {
BodyClasses []string BodyClasses []string
Breadcrumbs []Breadcrumb Breadcrumbs []Breadcrumb
CurrentUrl string
LoginPageUrl string LoginPageUrl string
ProjectCSSUrl string ProjectCSSUrl string

View File

@ -91,7 +91,12 @@ func Logout(c *RequestContext) ResponseData {
} }
} }
res := c.Redirect("/", http.StatusSeeOther) // TODO: Redirect to the page the user was currently on, or if not authorized to view that page, immediately to the home page. redir := c.Req.URL.Query().Get("redirect")
if redir == "" {
redir = "/"
}
res := c.Redirect(redir, http.StatusSeeOther)
res.SetCookie(auth.DeleteSessionCookie) res.SetCookie(auth.DeleteSessionCookie)
return res return res

View File

@ -131,17 +131,21 @@ func getBaseData(c *RequestContext) templates.BaseData {
} }
return templates.BaseData{ return templates.BaseData{
Project: templates.ProjectToTemplate(c.CurrentProject, c.Theme),
LoginPageUrl: hmnurl.BuildLoginPage(c.FullUrl()),
User: templateUser,
Theme: c.Theme, Theme: c.Theme,
CurrentUrl: c.FullUrl(),
LoginPageUrl: hmnurl.BuildLoginPage(c.FullUrl()),
ProjectCSSUrl: hmnurl.BuildProjectCSS(c.CurrentProject.Color1), ProjectCSSUrl: hmnurl.BuildProjectCSS(c.CurrentProject.Color1),
Project: templates.ProjectToTemplate(c.CurrentProject, c.Theme),
User: templateUser,
IsProjectPage: !c.CurrentProject.IsHMN(), IsProjectPage: !c.CurrentProject.IsHMN(),
Header: templates.Header{ Header: templates.Header{
AdminUrl: hmnurl.BuildHomepage(), // TODO(asaf) AdminUrl: hmnurl.BuildHomepage(), // TODO(asaf)
MemberSettingsUrl: hmnurl.BuildHomepage(), // TODO(asaf) MemberSettingsUrl: hmnurl.BuildHomepage(), // TODO(asaf)
LoginActionUrl: hmnurl.BuildLoginAction(c.FullUrl()), LoginActionUrl: hmnurl.BuildLoginAction(c.FullUrl()),
LogoutActionUrl: hmnurl.BuildLogoutAction(), LogoutActionUrl: hmnurl.BuildLogoutAction(c.FullUrl()),
RegisterUrl: hmnurl.BuildHomepage(), // TODO(asaf) RegisterUrl: hmnurl.BuildHomepage(), // TODO(asaf)
HMNHomepageUrl: hmnurl.BuildHomepage(), // TODO(asaf) HMNHomepageUrl: hmnurl.BuildHomepage(), // TODO(asaf)
ProjectHomepageUrl: hmnurl.BuildProjectHomepage(c.CurrentProject.Slug), ProjectHomepageUrl: hmnurl.BuildProjectHomepage(c.CurrentProject.Slug),