Skip to content

Commit

Permalink
Show login on www.goatcounter.com, add feature to email domains
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Dec 27, 2019
1 parent 7565755 commit f84e3b4
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 35 deletions.
4 changes: 2 additions & 2 deletions handlers/mw.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var (
func addctx(db *sqlx.DB, loadSite bool) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Add database and timeout to context.
// Add timeout.
ctx, cancel := context.WithTimeout(r.Context(), 4*time.Second)
defer func() {
cancel()
Expand All @@ -81,7 +81,7 @@ func addctx(db *sqlx.DB, loadSite bool) func(http.Handler) http.Handler {
// Add database.
*r = *r.WithContext(zdb.With(ctx, db))

// Load site from subdomain
// Load site from subdomain.
if loadSite {
var s goatcounter.Site
err := s.ByHost(r.Context(), r.Host)
Expand Down
80 changes: 78 additions & 2 deletions handlers/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package handlers // import "zgo.at/goatcounter/handlers"
import (
"context"
"fmt"
"html/template"
"net/http"
"net/mail"
"net/url"
"strings"
"time"
Expand All @@ -21,6 +23,8 @@ import (
"zgo.at/zdb"
"zgo.at/zhttp"
"zgo.at/zhttp/ctxkey"
"zgo.at/zhttp/zmail"
"zgo.at/zlog"
"zgo.at/zvalidate"
)

Expand Down Expand Up @@ -54,6 +58,8 @@ func (h website) Mount(r *chi.Mux, db *sqlx.DB) {
r.Get("/status", zhttp.Wrap(h.status()))
r.Get("/signup", zhttp.Wrap(h.signup))
r.Post("/signup", zhttp.Wrap(h.doSignup))
r.Get("/user/forgot", zhttp.Wrap(h.forgot))
r.Post("/user/forgot", zhttp.Wrap(h.doForgot))
for _, t := range []string{"", "help", "privacy", "terms", "contact"} {
r.Get("/"+t, zhttp.Wrap(h.tpl))
}
Expand All @@ -65,10 +71,26 @@ func (h website) tpl(w http.ResponseWriter, r *http.Request) error {
if t == "" {
t = "home"
}

var loggedIn template.HTML
if c, err := r.Cookie("key"); err == nil {
var u goatcounter.User
err = u.ByToken(r.Context(), c.Value)
if err == nil {
var s goatcounter.Site
err = s.ByID(r.Context(), u.Site)
if err == nil {
loggedIn = template.HTML(fmt.Sprintf("Logged in as %s on <a href='%s'>%[2]s</a>",
template.HTMLEscapeString(u.Name), template.HTMLEscapeString(s.URL())))
}
}
}

return zhttp.Template(w, t+".gohtml", struct {
Globals
Page string
}{newGlobals(w, r), t})
Page string
LoggedIn template.HTML
}{newGlobals(w, r), t, loggedIn})
}

func (h website) status() func(w http.ResponseWriter, r *http.Request) error {
Expand Down Expand Up @@ -169,3 +191,57 @@ func (h website) doSignup(w http.ResponseWriter, r *http.Request) error {
return zhttp.SeeOther(w, fmt.Sprintf("%s/user/new?mailed=%s",
site.URL(), url.QueryEscape(user.Email)))
}

func (h website) forgot(w http.ResponseWriter, r *http.Request) error {
return zhttp.Template(w, "user_forgot.gohtml", struct {
Globals
Page string
}{newGlobals(w, r), "forgot"})
}

func (h website) doForgot(w http.ResponseWriter, r *http.Request) error {
var args struct {
Email string `json:"email"`
}
_, err := zhttp.Decode(r, &args)
if err != nil {
return err
}

var users goatcounter.Users
err = users.ByEmail(r.Context(), args.Email)
if err != nil {
return err
}

go func(ctx context.Context) {
var body, name string
if len(users) == 0 {
body = fmt.Sprintf("There are no GoatCounter domains associated with ‘%s’", args.Email)
} else {
name = users[0].Name
body = fmt.Sprintf("Sites associated with ‘%s’:\r\n\r\n", args.Email)
for _, u := range users {
var s goatcounter.Site
err := s.ByID(ctx, u.Site)
if err != nil {
zlog.Error(err)
continue
}

body += fmt.Sprintf("- %s\r\n\r\n", s.URL())
}
}

err := zmail.Send("Your GoatCounter sites",
mail.Address{Name: "GoatCounter login", Address: "[email protected]"},
[]mail.Address{{Name: name, Address: args.Email}},
body)
if err != nil {
zlog.Errorf("zmail: %s", err)
}
}(goatcounter.NewContext(r.Context()))

zhttp.Flash(w, "List of login URLs mailed to %s", args.Email)
return zhttp.SeeOther(w, "/user/forgot")
}
9 changes: 9 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"time"

"zgo.at/zdb"
"zgo.at/zhttp"
"zgo.at/zhttp/ctxkey"
"zgo.at/zlog"
Expand Down Expand Up @@ -154,5 +155,13 @@ func GetUser(ctx context.Context) *User {
return u
}

// NewContext creates a new context with the all the request values set.
func NewContext(ctx context.Context) context.Context {
n := zdb.With(context.Background(), zdb.MustGet(ctx))
n = context.WithValue(n, ctxkey.User, GetUser(ctx))
n = context.WithValue(n, ctxkey.Site, GetSite(ctx))
return n
}

func dayStart(t time.Time) string { return t.Format("2006-01-02") + " 00:00:00" }
func dayEnd(t time.Time) string { return t.Format("2006-01-02") + " 23:59:59" }
75 changes: 61 additions & 14 deletions pack/pack.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/all.min.css
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ html, body {
@media (max-width: 54em) {
.page, .center {
max-width: 100%;
padding: 0 1rem;
padding: .5rem 1rem;
}
}
@page {
Expand Down
37 changes: 33 additions & 4 deletions public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@
hr { margin: 1em 0; }
u { text-decoration: none; box-shadow: inset 0 -.175em yellow, inset 0 -.2em #eee; }

footer { padding: 1em; text-align: center; background-color: #f6f3da; box-shadow: 0 0 4px #cdc8a4;
display: flex; justify-content: space-between; }
footer a { font-weight: bold; color: #252525; }
.cbox { box-shadow: 0 0 4px #cdc8a4; background-color: #f6f3da; color: #252525; }

footer { padding: 1em; text-align: center; display: flex; justify-content: space-between; }
footer a { font-weight: bold; color: #252525; margin: 0 .5em; }

@media (max-width: 54em) {
footer { text-align: left; justify-content: space-around; }
footer a { display: block; padding: .5em 0; }
footer span { display: none; }
}

.page { position: relative; }

/*** Home page ***/
.page-home { padding: 0; box-shadow: none; background-color: transparent; }
.page-home h1 { margin-top: 2em; font-size: 2em; }
.page-home *:target { background-color: inherit; }

.hlink { font-weight: bold; box-shadow: 0 0 4px #cdc8a4; background-color: #f6f3da; color: #252525; }
.hlink { font-weight: bold; }
.hlink:hover { text-decoration: none; background-color: #fffded; color: #252525; }
.hlink img { vertical-align: middle; margin: 0 5px; height: 1.1em; }

Expand All @@ -26,6 +35,7 @@ footer a { font-weight: bold; color: #252525; }

#home-demo { text-align: center; margin: 2.5em 0; }
#home-demo a { padding: 1em 3em; }
#home-login { text-align: center }

@media (min-width: 45rem) {
#home-screens { display: flex; justify-content: space-between; align-items: center; }
Expand Down Expand Up @@ -122,3 +132,22 @@ button { display: block; }

/*** Help page ***/
dt { font-weight: bold; margin-top: 1em; }

.flash {
text-align: center;
padding: 10px;
border-radius: 2px;
border: 2px solid #fff;
margin-bottom: 1em;
}
.flash p { max-width: none !important; }
.flash pre { text-align: left; margin: 0 auto; display: inline-block; }
.flash-i {
background-color: #c9f0ff;
border-color: #a1a1ff;
}
.flash-e {
background-color: #fff0f0;
border-color: #f00;
}

14 changes: 7 additions & 7 deletions tpl/_bottom_links.gohtml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<footer class="center">
<footer class="center cbox">
<div>
<a {{if .Site}}target="_blank"{{end}} href="//www.{{.Domain}}/">Home</a> |
<a {{if .Site}}target="_blank"{{end}} href="//www.{{.Domain}}/contact">Contact</a> |
<a {{if .Site}}target="_blank"{{end}} href="//www.{{.Domain}}/help">Help</a> |
<a {{if .Site}}target="_blank"{{end}} href="//www.{{.Domain}}/privacy">Privacy</a> |
<a {{if .Site}}target="_blank"{{end}} href="//www.{{.Domain}}/">Home</a><span> |</span>
<a {{if .Site}}target="_blank"{{end}} href="//www.{{.Domain}}/contact">Contact</a><span> |</span>
<a {{if .Site}}target="_blank"{{end}} href="//www.{{.Domain}}/help">Help</a><span> |</span>
<a {{if .Site}}target="_blank"{{end}} href="//www.{{.Domain}}/privacy">Privacy</a><span> |</span>
<a {{if .Site}}target="_blank"{{end}} href="//www.{{.Domain}}/terms">Terms</a>
</div>
<div>
<a href="https://github.com/zgoat/goatcounter" target="_blank">GitHub</a> |
<a href="https://www.producthunt.com/posts/goatcounter" target="_blank">Product Hunt</a> |
<a href="https://github.com/zgoat/goatcounter" target="_blank">GitHub</a><span> |</span>
<a href="https://www.producthunt.com/posts/goatcounter" target="_blank">Product Hunt</a><span> |</span>
<a href="https://patreon.com/arp242">Patreon</a>
</div>
</footer>
Loading

0 comments on commit f84e3b4

Please sign in to comment.