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

51 lines
1.3 KiB
Go
Raw Normal View History

2015-07-29 07:01:23 +00:00
package main
import (
"context"
2015-07-29 07:01:23 +00:00
"net/http"
"strings"
2015-07-29 07:01:23 +00:00
"golang.org/x/crypto/bcrypt"
2015-07-29 07:01:23 +00:00
"github.com/flosch/pongo2"
"github.com/gorilla/sessions"
log "github.com/sirupsen/logrus"
2015-07-29 07:01:23 +00:00
)
func registerHandler(c context.Context, res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
2015-07-29 07:01:23 +00:00
var (
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(c, createUserFilename(username)) {
2015-07-29 07:01:23 +00:00
(*ctx)["exists"] = true
2015-07-29 07:49:23 +00:00
return stringPointer("register.html"), nil
2015-07-29 07:01:23 +00:00
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
log.WithError(err).Error("Could not hash user password")
(*ctx)["error"] = true
return stringPointer("register.html"), nil
}
2015-07-29 07:01:23 +00:00
d := dataObject{}
d.MetaData.Password = string(hashedPassword)
data, _ := d.GetData()
2015-07-29 07:01:23 +00:00
if err := storage.Write(c, createUserFilename(username), data); err != nil {
log.WithError(err).Error("Could not write user file to storage")
(*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
}