Add user creation
This commit is contained in:
parent
7f1b99364c
commit
c6117f1c2f
5 changed files with 76 additions and 0 deletions
|
@ -89,6 +89,11 @@ func (d *DB) UserValid(user string, pass string) bool {
|
|||
return n != 0
|
||||
}
|
||||
|
||||
func (d *DB) AddUser(user string, pass string) error {
|
||||
hash := md5Pass(pass)
|
||||
return d.user.Insert(bson.M{"user": user, "pass": hash, "role": ""})
|
||||
}
|
||||
|
||||
func (d *DB) UserRole(user string) string {
|
||||
type result struct {
|
||||
Role string
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<div class="modal-header">
|
||||
<a type="button" class="close" data-dismiss="modal">×</a>
|
||||
<h3>Log In</h3>
|
||||
<small><a href="/login/">Or create an account -></a></small>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<fieldset>
|
||||
|
|
|
@ -1,5 +1,27 @@
|
|||
{{template "header.html" .S}}
|
||||
|
||||
<script>
|
||||
$(document).ready(function () {
|
||||
$("#confirmPass").keyup(checkPasswordMatch);
|
||||
});
|
||||
|
||||
function checkPasswordMatch() {
|
||||
var password = $("#pass").val();
|
||||
var confirmPassword = $("#confirmPass").val();
|
||||
|
||||
if (password != confirmPassword) {
|
||||
$("#passCheck").removeClass("success");
|
||||
$("#passCheck").addClass("error");
|
||||
$("#passCheckHelp").html("Passwords don't match!");
|
||||
} else {
|
||||
$("#passCheck").removeClass("error");
|
||||
$("#passCheck").addClass("success");
|
||||
$("#passCheckHelp").html("");
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="offset1 span4">
|
||||
<h3>Log In</h3>
|
||||
<form id="login" method="POST" action="/login/" enctype="application/x-www-form-urlencoded">
|
||||
<div class="input-prepend">
|
||||
|
@ -12,5 +34,34 @@
|
|||
</div>
|
||||
<input class="btn btn-primary" type="submit" name="submit" value="Log In"/>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="span2">
|
||||
<br />
|
||||
<br />
|
||||
<h3 class="centered">Or</h3>
|
||||
</div>
|
||||
|
||||
<div class="span4">
|
||||
<h3>Create an Account</h3>
|
||||
<form id="login" method="POST" action="/create_user/" 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" id="pass">
|
||||
</div>
|
||||
<div class="control-group" id="passCheck">
|
||||
<div class="controls input-prepend">
|
||||
<div class="add-on"><i class="icon-lock"></i></div>
|
||||
<input type="password" placeholder="Confirm password" size="16" name="confirmPass" id="confirmPass">
|
||||
</div>
|
||||
<span id="passCheckHelp" class="help-inline"></span>
|
||||
</div>
|
||||
<input class="btn btn-primary" type="submit" name="submit" value="Create"/>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{{template "footer.html"}}
|
||||
|
|
|
@ -154,6 +154,7 @@ func setUpRouter() {
|
|||
r.HandleFunc("/upload/", GatherStats(uploadPostHandler)).Methods("POST")
|
||||
r.HandleFunc("/login/", GatherStats(loginHandler)).Methods("GET")
|
||||
r.HandleFunc("/login/", GatherStats(loginPostHandler)).Methods("POST")
|
||||
r.HandleFunc("/create_user/", GatherStats(createUserHandler)).Methods("POST")
|
||||
r.HandleFunc("/logout/", GatherStats(logoutHandler))
|
||||
r.HandleFunc("/new/", GatherStats(newHandler))
|
||||
r.HandleFunc("/store/{ids:([0-9a-fA-F]+/)+}", GatherStats(storeHandler))
|
||||
|
|
18
user.go
18
user.go
|
@ -25,3 +25,21 @@ func loginPostHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
|||
sess.Save(w, r)
|
||||
http.Redirect(w, r, r.Referer(), http.StatusFound)
|
||||
}
|
||||
|
||||
func createUserHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||
pass := r.FormValue("pass")
|
||||
confirmPass := r.FormValue("confirmPass")
|
||||
if pass != confirmPass {
|
||||
sess.Notify("Registration error!", "Passwords don't match", "error")
|
||||
} else {
|
||||
user := r.FormValue("user")
|
||||
err := db.AddUser(user, pass)
|
||||
if err == nil {
|
||||
sess.Notify("Account created!", "Welcome "+user+". Now you can login", "success")
|
||||
} else {
|
||||
sess.Notify("Registration error!", "There was some database problem, if it keeps happening please inform me", "error")
|
||||
}
|
||||
}
|
||||
sess.Save(w, r)
|
||||
http.Redirect(w, r, r.Referer(), http.StatusFound)
|
||||
}
|
||||
|
|
Reference in a new issue