Permalink
Browse files

add registration and post count

  • Loading branch information...
zeebo committed Sep 5, 2012
1 parent 7c2ff89 commit 46bf28e9f862d083e6a40d813ff3f271bf96a5f1
Showing with 67 additions and 2 deletions.
  1. +33 −0 handlers.go
  2. +15 −1 main.go
  3. +4 −1 templates/index.html
  4. +15 −0 templates/register.html
@@ -2,6 +2,7 @@ package main
import (
"errors"
"labix.org/v2/mgo/bson"
"net/http"
)
@@ -44,6 +45,12 @@ func sign(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
return
}
//ignore errors: it's ok if the post count is wrong. we can always look at
//the entries table to fix.
ctx.C("users").Update(bson.M{"_id": ctx.User.ID}, bson.M{
"$inc": bson.M{"posts": 1},
})

This comment has been minimized.

@zeebo

zeebo Sep 5, 2012

Owner

If we really wanted to be sure they got inserted correctly, we could use the experimental txn subpackage of mgo, but that's outside the scope right now.

http.Redirect(w, req, reverse("index"), http.StatusSeeOther)
return
}
@@ -74,3 +81,29 @@ func logout(w http.ResponseWriter, req *http.Request, ctx *Context) error {
http.Redirect(w, req, reverse("index"), http.StatusSeeOther)
return nil
}
func registerForm(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
return T("register.html").Execute(w, map[string]interface{}{
"ctx": ctx,
})
}
func register(w http.ResponseWriter, req *http.Request, ctx *Context) error {
username, password := req.FormValue("username"), req.FormValue("password")
u := &User{
Username: username,
ID: bson.NewObjectId(),
}
u.SetPassword(password)
if err := ctx.C("users").Insert(u); err != nil {
ctx.Session.AddFlash("Problem registering user.")
return registerForm(w, req, ctx)
}
//store the user id in the values and redirect to index
ctx.Session.Values["user"] = u.ID
http.Redirect(w, req, reverse("index"), http.StatusSeeOther)
return nil
}
16 main.go
@@ -42,13 +42,27 @@ func main() {
}
database = session.DB("").Name
//create an index for the username field on the users collection
if err := session.DB("").C("users").EnsureIndex(mgo.Index{
Key: []string{"username"},
Unique: true,
}); err != nil {
panic(err)
}

This comment has been minimized.

@zeebo

zeebo Sep 5, 2012

Owner

I glossed over this in the article, but by creating a unique index on the username key we make sure that two users don't register with the same name. The error is just reported when they try to create it.

store = sessions.NewCookieStore([]byte(os.Getenv("KEY")))
router = pat.New()
router.Add("GET", "/login", handler(loginForm)).Name("login")
router.Add("GET", "/logout", handler(logout)).Name("logout")
router.Add("POST", "/login", handler(login))
router.Add("GET", "/register", handler(registerForm)).Name("register")
router.Add("POST", "/register", handler(register))
router.Add("GET", "/logout", handler(logout)).Name("logout")
router.Add("GET", "/", handler(hello)).Name("index")
router.Add("POST", "/sign", handler(sign)).Name("sign")
if err = http.ListenAndServe(":"+os.Getenv("PORT"), router); err != nil {
@@ -22,6 +22,9 @@ <h1>Guestbook</h1>
<p><button>Sign</button></p>
</form>
{{ else }}
<h1>Please <a href="{{ reverse "login" }}">login</a> to sign.</h1>
<h1>Please
<a href="{{ reverse "login" }}">login</a> or
<a href="{{ reverse "register" }}">register</a>
to sign.</h1>
{{ end }}
{{ end }}
@@ -0,0 +1,15 @@
{{ define "title" }}Guestbook - Register{{ end }}
{{ define "content" }}
<h1>Register</h1>
{{ range .ctx.Session.Flashes }}
<h2>{{ . }}</h2>
{{ end }}
<form action="{{ reverse "register" }}" method="POST">
<p>Username: <input type="text" name="username"></p>
<p>Password: <input type="password" name="password"></p>
<p><button>Login</button></p>
</form>
{{ end }}

0 comments on commit 46bf28e

Please sign in to comment.