2015-07-29 07:01:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2015-07-31 22:59:44 +00:00
|
|
|
"strings"
|
2015-07-29 07:01:23 +00:00
|
|
|
|
2017-12-24 10:55:52 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
2015-07-29 07:01:23 +00:00
|
|
|
"github.com/flosch/pongo2"
|
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
)
|
|
|
|
|
|
|
|
func registerHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
|
|
|
|
var (
|
2017-12-24 10:55:52 +00:00
|
|
|
username = strings.ToLower(r.FormValue("username"))
|
|
|
|
password = r.FormValue("password")
|
|
|
|
passwordCheck = r.FormValue("password_repeat")
|
2015-07-29 07:01:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if username == "" || password == "" || password != passwordCheck {
|
2015-07-29 07:49:23 +00:00
|
|
|
return stringPointer("register.html"), nil
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if storage.IsPresent(createUserFilename(username)) {
|
|
|
|
(*ctx)["exists"] = true
|
2015-07-29 07:49:23 +00:00
|
|
|
return stringPointer("register.html"), nil
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|
|
|
|
|
2017-12-24 10:55:52 +00:00
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERR: Unable to hash users password: %s\n", err)
|
|
|
|
(*ctx)["error"] = true
|
|
|
|
return stringPointer("register.html"), nil
|
|
|
|
}
|
|
|
|
|
2015-07-29 07:01:23 +00:00
|
|
|
d := dataObject{}
|
2017-12-24 10:55:52 +00:00
|
|
|
d.MetaData.Password = string(hashedPassword)
|
2015-07-29 08:10:46 +00:00
|
|
|
data, _ := d.GetData()
|
2015-07-29 07:01:23 +00:00
|
|
|
|
2015-07-29 08:10:46 +00:00
|
|
|
if err := storage.Write(createUserFilename(username), data); err != nil {
|
|
|
|
fmt.Printf("ERR: Unable to write user file: %s\n", err)
|
|
|
|
(*ctx)["error"] = true
|
2015-07-29 07:49:23 +00:00
|
|
|
return stringPointer("register.html"), nil
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 08:10:46 +00:00
|
|
|
(*ctx)["created"] = true
|
|
|
|
return stringPointer("register.html"), nil
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|