mirror of
https://github.com/Luzifer/cloudkeys-go.git
synced 2024-11-09 14:40:05 +00:00
Do not throw error messages at users, let the frontend handle errors
fixes #1
This commit is contained in:
parent
dc3fb4df50
commit
a89e45f9b9
5 changed files with 37 additions and 41 deletions
41
ajax.go
41
ajax.go
|
@ -24,10 +24,7 @@ func (a ajaxResponse) Bytes() []byte {
|
|||
|
||||
func ajaxGetHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
|
||||
res.Header().Set("Content-Type", "application/json")
|
||||
user, err := checkLogin(r, session)
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
}
|
||||
user, _ := checkLogin(r, session)
|
||||
|
||||
if user == nil || !storage.IsPresent(user.UserFile) {
|
||||
res.Write(ajaxResponse{Error: true}.Bytes())
|
||||
|
@ -36,13 +33,12 @@ func ajaxGetHandler(res http.ResponseWriter, r *http.Request, session *sessions.
|
|||
|
||||
userFileRaw, err := storage.Read(user.UserFile)
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
fmt.Printf("ERR: Unable to read user file: %s\n", err)
|
||||
res.Write(ajaxResponse{Error: true}.Bytes())
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
userFile, err := readDataObject(userFileRaw)
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
}
|
||||
userFile, _ := readDataObject(userFileRaw)
|
||||
|
||||
res.Write(ajaxResponse{Version: userFile.MetaData.Version, Data: userFile.Data}.Bytes())
|
||||
return nil, nil
|
||||
|
@ -50,10 +46,7 @@ func ajaxGetHandler(res http.ResponseWriter, r *http.Request, session *sessions.
|
|||
|
||||
func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
|
||||
res.Header().Set("Content-Type", "application/json")
|
||||
user, err := checkLogin(r, session)
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
}
|
||||
user, _ := checkLogin(r, session)
|
||||
|
||||
if user == nil {
|
||||
res.Write(ajaxResponse{Error: true, Type: "login"}.Bytes())
|
||||
|
@ -67,13 +60,12 @@ func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions
|
|||
|
||||
userFileRaw, err := storage.Read(user.UserFile)
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
fmt.Printf("ERR: Unable to read user file: %s\n", err)
|
||||
res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes())
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
userFile, err := readDataObject(userFileRaw)
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
}
|
||||
userFile, _ := readDataObject(userFileRaw)
|
||||
|
||||
var (
|
||||
version = r.FormValue("version")
|
||||
|
@ -92,19 +84,20 @@ func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions
|
|||
}
|
||||
|
||||
if err := storage.Backup(user.UserFile); err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
fmt.Printf("ERR: Unable to backup user file: %s\n", err)
|
||||
res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes())
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
userFile.MetaData.Version = checksum
|
||||
userFile.Data = data
|
||||
|
||||
d, err := userFile.GetData()
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
}
|
||||
d, _ := userFile.GetData()
|
||||
|
||||
if err := storage.Write(user.UserFile, d); err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
fmt.Printf("ERR: Unable to write user file: %s\n", err)
|
||||
res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes())
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
res.Write(ajaxResponse{Version: userFile.MetaData.Version, Data: userFile.Data}.Bytes())
|
||||
|
|
11
login.go
11
login.go
|
@ -24,13 +24,12 @@ func loginHandler(res http.ResponseWriter, r *http.Request, session *sessions.Se
|
|||
|
||||
userFileRaw, err := storage.Read(createUserFilename(username))
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
fmt.Printf("ERR: Unable to read user file: %s\n", err)
|
||||
(*ctx)["error"] = true
|
||||
return stringPointer("login.html"), nil
|
||||
}
|
||||
|
||||
userFile, err := readDataObject(userFileRaw)
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
}
|
||||
userFile, _ := readDataObject(userFileRaw)
|
||||
|
||||
if userFile.MetaData.Password != password {
|
||||
(*ctx)["error"] = true
|
||||
|
@ -74,7 +73,7 @@ func checkLogin(r *http.Request, session *sessions.Session) (*authorizedAccount,
|
|||
vars := mux.Vars(r)
|
||||
idx, err := strconv.ParseInt(vars["userIndex"], 10, 64)
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
return nil, err
|
||||
}
|
||||
|
||||
auth, ok := session.Values["authorizedAccounts"].(authorizedAccounts)
|
||||
|
|
|
@ -8,10 +8,7 @@ import (
|
|||
)
|
||||
|
||||
func overviewHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
|
||||
user, err := checkLogin(r, session)
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
}
|
||||
user, _ := checkLogin(r, session)
|
||||
|
||||
if user == nil || !storage.IsPresent(user.UserFile) {
|
||||
http.Redirect(res, r, "../../login", http.StatusFound)
|
||||
|
|
13
register.go
13
register.go
|
@ -28,15 +28,14 @@ func registerHandler(res http.ResponseWriter, r *http.Request, session *sessions
|
|||
|
||||
d := dataObject{}
|
||||
d.MetaData.Password = hashedPassword
|
||||
data, err := d.GetData()
|
||||
if err != nil {
|
||||
return nil, err // TODO: Handle in-app?
|
||||
}
|
||||
data, _ := d.GetData()
|
||||
|
||||
if err := storage.Write(createUserFilename(username), data); err == nil {
|
||||
(*ctx)["created"] = true
|
||||
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
|
||||
}
|
||||
|
||||
return nil, err // TODO: Handle in-app?
|
||||
(*ctx)["created"] = true
|
||||
return stringPointer("register.html"), nil
|
||||
}
|
||||
|
|
|
@ -18,6 +18,14 @@
|
|||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if error %}
|
||||
<div class="row">
|
||||
<div class="col-md-4 col-md-push-4 alert alert-danger fade in">
|
||||
<h4>Ooops…</h4>
|
||||
<p>Something went wrong. Please try again.</p>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
<form class="form-signin" method="post">
|
||||
<h2 class="form-signin-heading">Please register</h2>
|
||||
<input type="text" name="username" class="form-control" placeholder="Username">
|
||||
|
|
Loading…
Reference in a new issue