1
0
Fork 0
mirror of https://github.com/Luzifer/cloudkeys-go.git synced 2024-11-08 14:10:05 +00:00
cloudkeys-go/register.go

42 lines
1.1 KiB
Go
Raw Normal View History

2015-07-29 07:01:23 +00:00
package main
import (
"crypto/sha1"
"fmt"
"net/http"
"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 (
username = r.FormValue("username")
password = r.FormValue("password")
passwordCheck = r.FormValue("password_repeat")
hashedPassword = fmt.Sprintf("%x", sha1.Sum([]byte(cfg.PasswordSalt+password)))
)
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
}
d := dataObject{}
d.MetaData.Password = hashedPassword
data, _ := d.GetData()
2015-07-29 07:01:23 +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
}
(*ctx)["created"] = true
return stringPointer("register.html"), nil
2015-07-29 07:01:23 +00:00
}