Back the login to html if no javascript

This commit is contained in:
Las Zenow 2013-09-03 14:02:54 +02:00
parent 7410ae7942
commit 7f1b99364c
5 changed files with 47 additions and 17 deletions

View file

@ -44,6 +44,7 @@ var templates = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html",
TEMPLATE_PATH+"book.html",
TEMPLATE_PATH+"search.html",
TEMPLATE_PATH+"upload.html",
TEMPLATE_PATH+"login.html",
TEMPLATE_PATH+"new.html",
TEMPLATE_PATH+"read.html",
TEMPLATE_PATH+"edit.html",

View file

@ -77,7 +77,7 @@
</ul>
</li>
{{else}}
<li><a data-toggle="modal" href="#login"><i class="icon-user icon-white"></i></a></li>
<li><a data-toggle="modal" href="/login/#login"><i class="icon-user icon-white"></i></a></li>
{{end}}
</ul>
</div><!--/.nav-collapse -->

16
templates/login.html Normal file
View file

@ -0,0 +1,16 @@
{{template "header.html" .S}}
<h3>Log In</h3>
<form id="login" method="POST" action="/login/" enctype="application/x-www-form-urlencoded">
<div class="input-prepend">
<div class="add-on"><i class="icon-user"></i></div>
<input type="text" placeholder="Username" size="16" name="user" autofocus="autofocus">
</div>
<div class="input-prepend">
<div class="add-on"><i class="icon-lock"></i></div>
<input type="password" placeholder="Password" size="16" name="pass">
</div>
<input class="btn btn-primary" type="submit" name="submit" value="Log In"/>
</form>
{{template "footer.html"}}

View file

@ -35,21 +35,6 @@ func logoutHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
http.Redirect(w, r, "/", http.StatusFound)
}
func loginHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
user := r.FormValue("user")
pass := r.FormValue("pass")
if db.UserValid(user, pass) {
log.Println("User", user, "log in")
sess.LogIn(user)
sess.Notify("Successful login!", "Welcome "+user, "success")
} else {
log.Println("User", user, "bad user or password")
sess.Notify("Invalid login!", "user or password invalid", "error")
}
sess.Save(w, r)
http.Redirect(w, r, r.Referer(), http.StatusFound)
}
type bookData struct {
S Status
Book Book
@ -167,7 +152,8 @@ func setUpRouter() {
r.HandleFunc("/search/", GatherStats(searchHandler))
r.HandleFunc("/upload/", GatherStats(uploadHandler)).Methods("GET")
r.HandleFunc("/upload/", GatherStats(uploadPostHandler)).Methods("POST")
r.HandleFunc("/login/", GatherStats(loginHandler)).Methods("POST")
r.HandleFunc("/login/", GatherStats(loginHandler)).Methods("GET")
r.HandleFunc("/login/", GatherStats(loginPostHandler)).Methods("POST")
r.HandleFunc("/logout/", GatherStats(logoutHandler))
r.HandleFunc("/new/", GatherStats(newHandler))
r.HandleFunc("/store/{ids:([0-9a-fA-F]+/)+}", GatherStats(storeHandler))

27
user.go Normal file
View file

@ -0,0 +1,27 @@
package main
import (
"log"
"net/http"
)
func loginHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
var data statusData
data.S = GetStatus(w, r)
loadTemplate(w, "login", data)
}
func loginPostHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
user := r.FormValue("user")
pass := r.FormValue("pass")
if db.UserValid(user, pass) {
log.Println("User", user, "log in")
sess.LogIn(user)
sess.Notify("Successful login!", "Welcome "+user, "success")
} else {
log.Println("User", user, "bad user or password")
sess.Notify("Invalid login!", "user or password invalid", "error")
}
sess.Save(w, r)
http.Redirect(w, r, r.Referer(), http.StatusFound)
}