Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add registration and post count
  • Loading branch information
zeebo committed Sep 5, 2012
1 parent 7c2ff89 commit 46bf28e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
33 changes: 33 additions & 0 deletions handlers.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"labix.org/v2/mgo/bson"
"net/http"
)

Expand Down Expand Up @@ -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.

Copy link
@zeebo

zeebo Sep 5, 2012

Author 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
}
Expand Down Expand Up @@ -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 changes: 15 additions & 1 deletion main.go
Expand Up @@ -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.

Copy link
@zeebo

zeebo Sep 5, 2012

Author 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 {
Expand Down
5 changes: 4 additions & 1 deletion templates/index.html
Expand Up @@ -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 }}
15 changes: 15 additions & 0 deletions templates/register.html
@@ -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.