Commit bbd9c385 authored by Leonard Techel's avatar Leonard Techel
Browse files

Use a URLFor route resolver helper

Now, routes are only specified at one central place and referenced
everywhere else in the code.
parent 1459c72b
......@@ -9,7 +9,7 @@
{{template "flashes" .}}
<div class="well well-xs">
<form action="/user/logout" method="post">
<form action="{{URLFor "user/logout"}}" method="post">
<input type="hidden" name="_csrf" value="{{.csrf_token}}" />
<button type="submit" class="btn btn-default">{{.i18n.Tr "user.logout"}}</button>
</form>
......
......@@ -8,7 +8,7 @@
<div class="col-sm-4 col-sm-push-4">
<div class="well well-xs">
{{template "flashes" .}}
<form action="/user/login" method="post">
<form action="{{URLFor "user/login"}}" method="post">
<div class="form-group">
<label for="username">{{.i18n.Tr "user.username"}}</label>
<input type="text" name="username" id="username" placeholder="{{.i18n.Tr "user.username_example"}}" class="form-control" autofocus />
......@@ -22,6 +22,7 @@
<input type="hidden" name="_csrf" value="{{.csrf_token}}" />
<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-log-in"></span> {{.i18n.Tr "user.sign_in"}}</button>
<a href="{{URLFor "user/resetpw"}}" class="btn btn-link pull-right">{{.i18n.Tr "user.lost_pw"}}</a>
</form>
</div>
</div>
......
......@@ -6,6 +6,7 @@ import (
"github.com/barnslig/ucp/Godeps/_workspace/src/github.com/go-macaron/i18n"
"github.com/barnslig/ucp/Godeps/_workspace/src/github.com/go-macaron/session"
"github.com/barnslig/ucp/Godeps/_workspace/src/gopkg.in/macaron.v1"
"html/template"
"net/http"
"github.com/barnslig/ucp/authprovider"
......@@ -32,7 +33,11 @@ func CreateUCP(cfg UCPConfig) (ucp *UCP) {
Langs: []string{"en-US", "de-DE"},
Names: []string{"English", "Deutsch"},
}))
m.Use(macaron.Renderer())
m.Use(macaron.Renderer(macaron.RenderOptions{
Funcs: []template.FuncMap{map[string]interface{}{
"URLFor": m.URLFor,
}},
}))
m.Use(session.Sessioner())
m.Use(csrf.Csrfer())
m.Use(func(ctx *macaron.Context, x csrf.CSRF) {
......@@ -52,13 +57,14 @@ func CreateUCP(cfg UCPConfig) (ucp *UCP) {
})
m.Group("/user", func() {
m.Get("/", UserGet)
m.Get("/", UserGet).Name("user")
m.Combo("/login").
Get(UserGetLogin).
Post(csrf.Validate, binding.BindIgnErr(UserLoginForm{}), UserPostLogin)
Post(csrf.Validate, binding.BindIgnErr(UserLoginForm{}), UserPostLogin).
Name("user/login")
m.Post("/logout", csrf.Validate, UserPostLogout)
m.Post("/logout", csrf.Validate, UserPostLogout).Name("user/logout")
})
panic(http.ListenAndServe(ucp.cfg.HttpListen, m))
......
......@@ -16,7 +16,7 @@ type UserLoginForm struct {
func UserGet(ctx *macaron.Context, sess session.Store, f *session.Flash) {
if sess.Get("user") == nil {
f.Error(ctx.Tr("user.not_authenticated"))
ctx.Redirect("/user/login")
ctx.Redirect(ctx.URLFor("user/login"))
return
}
......@@ -26,7 +26,7 @@ func UserGet(ctx *macaron.Context, sess session.Store, f *session.Flash) {
func UserGetLogin(ctx *macaron.Context, sess session.Store) {
if sess.Get("user") != nil {
ctx.Redirect("/user")
ctx.Redirect(ctx.URLFor("user"))
return
}
......@@ -36,14 +36,14 @@ func UserGetLogin(ctx *macaron.Context, sess session.Store) {
func UserPostLogin(ctx *macaron.Context, sess session.Store, f *session.Flash, errs binding.Errors, postData UserLoginForm, auth authprovider.AuthProvider) {
if len(errs) > 0 {
f.Error(ctx.Tr("user.invalid_credentials"))
ctx.Redirect("/user/login")
ctx.Redirect(ctx.URLFor("user/login"))
return
}
user, err := auth.Authenticate(postData.Username, postData.Password)
if err != nil {
f.Error(ctx.Tr("user.invalid_credentials"))
ctx.Redirect("/user/login")
ctx.Redirect(ctx.URLFor("user/login"))
return
}
......@@ -54,5 +54,5 @@ func UserPostLogin(ctx *macaron.Context, sess session.Store, f *session.Flash, e
func UserPostLogout(ctx *macaron.Context, sess session.Store, f *session.Flash) {
sess.Set("user", nil)
f.Success(ctx.Tr("user.logout_success"))
ctx.Redirect("/user/login")
ctx.Redirect(ctx.URLFor("user/login"))
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment