forked from awesomecoders/cloudkeys-go
/
register.go
42 lines (34 loc) · 1.09 KB
/
register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"crypto/sha1"
"fmt"
"net/http"
"strings"
"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 = strings.ToLower(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 {
return stringPointer("register.html"), nil
}
if storage.IsPresent(createUserFilename(username)) {
(*ctx)["exists"] = true
return stringPointer("register.html"), nil
}
d := dataObject{}
d.MetaData.Password = hashedPassword
data, _ := d.GetData()
if err := storage.Write(createUserFilename(username), data); err != nil {
fmt.Printf("ERR: Unable to write user file: %s\n", err)
(*ctx)["error"] = true
return stringPointer("register.html"), nil
}
(*ctx)["created"] = true
return stringPointer("register.html"), nil
}