mirror of
https://github.com/Luzifer/cloudkeys-go.git
synced 2024-11-09 22:50: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) {
|
func ajaxGetHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
|
||||||
res.Header().Set("Content-Type", "application/json")
|
res.Header().Set("Content-Type", "application/json")
|
||||||
user, err := checkLogin(r, session)
|
user, _ := checkLogin(r, session)
|
||||||
if err != nil {
|
|
||||||
return nil, err // TODO: Handle in-app?
|
|
||||||
}
|
|
||||||
|
|
||||||
if user == nil || !storage.IsPresent(user.UserFile) {
|
if user == nil || !storage.IsPresent(user.UserFile) {
|
||||||
res.Write(ajaxResponse{Error: true}.Bytes())
|
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)
|
userFileRaw, err := storage.Read(user.UserFile)
|
||||||
if err != nil {
|
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)
|
userFile, _ := readDataObject(userFileRaw)
|
||||||
if err != nil {
|
|
||||||
return nil, err // TODO: Handle in-app?
|
|
||||||
}
|
|
||||||
|
|
||||||
res.Write(ajaxResponse{Version: userFile.MetaData.Version, Data: userFile.Data}.Bytes())
|
res.Write(ajaxResponse{Version: userFile.MetaData.Version, Data: userFile.Data}.Bytes())
|
||||||
return nil, nil
|
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) {
|
func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
|
||||||
res.Header().Set("Content-Type", "application/json")
|
res.Header().Set("Content-Type", "application/json")
|
||||||
user, err := checkLogin(r, session)
|
user, _ := checkLogin(r, session)
|
||||||
if err != nil {
|
|
||||||
return nil, err // TODO: Handle in-app?
|
|
||||||
}
|
|
||||||
|
|
||||||
if user == nil {
|
if user == nil {
|
||||||
res.Write(ajaxResponse{Error: true, Type: "login"}.Bytes())
|
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)
|
userFileRaw, err := storage.Read(user.UserFile)
|
||||||
if err != nil {
|
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)
|
userFile, _ := readDataObject(userFileRaw)
|
||||||
if err != nil {
|
|
||||||
return nil, err // TODO: Handle in-app?
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version = r.FormValue("version")
|
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 {
|
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.MetaData.Version = checksum
|
||||||
userFile.Data = data
|
userFile.Data = data
|
||||||
|
|
||||||
d, err := userFile.GetData()
|
d, _ := userFile.GetData()
|
||||||
if err != nil {
|
|
||||||
return nil, err // TODO: Handle in-app?
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := storage.Write(user.UserFile, d); err != nil {
|
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())
|
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))
|
userFileRaw, err := storage.Read(createUserFilename(username))
|
||||||
if err != nil {
|
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)
|
userFile, _ := readDataObject(userFileRaw)
|
||||||
if err != nil {
|
|
||||||
return nil, err // TODO: Handle in-app?
|
|
||||||
}
|
|
||||||
|
|
||||||
if userFile.MetaData.Password != password {
|
if userFile.MetaData.Password != password {
|
||||||
(*ctx)["error"] = true
|
(*ctx)["error"] = true
|
||||||
|
@ -74,7 +73,7 @@ func checkLogin(r *http.Request, session *sessions.Session) (*authorizedAccount,
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
idx, err := strconv.ParseInt(vars["userIndex"], 10, 64)
|
idx, err := strconv.ParseInt(vars["userIndex"], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err // TODO: Handle in-app?
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
auth, ok := session.Values["authorizedAccounts"].(authorizedAccounts)
|
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) {
|
func overviewHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
|
||||||
user, err := checkLogin(r, session)
|
user, _ := checkLogin(r, session)
|
||||||
if err != nil {
|
|
||||||
return nil, err // TODO: Handle in-app?
|
|
||||||
}
|
|
||||||
|
|
||||||
if user == nil || !storage.IsPresent(user.UserFile) {
|
if user == nil || !storage.IsPresent(user.UserFile) {
|
||||||
http.Redirect(res, r, "../../login", http.StatusFound)
|
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 := dataObject{}
|
||||||
d.MetaData.Password = hashedPassword
|
d.MetaData.Password = hashedPassword
|
||||||
data, err := d.GetData()
|
data, _ := d.GetData()
|
||||||
if err != nil {
|
|
||||||
return nil, err // TODO: Handle in-app?
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := storage.Write(createUserFilename(username), data); err == nil {
|
if err := storage.Write(createUserFilename(username), data); err != nil {
|
||||||
(*ctx)["created"] = true
|
fmt.Printf("ERR: Unable to write user file: %s\n", err)
|
||||||
|
(*ctx)["error"] = true
|
||||||
return stringPointer("register.html"), nil
|
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>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% 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">
|
<form class="form-signin" method="post">
|
||||||
<h2 class="form-signin-heading">Please register</h2>
|
<h2 class="form-signin-heading">Please register</h2>
|
||||||
<input type="text" name="username" class="form-control" placeholder="Username">
|
<input type="text" name="username" class="form-control" placeholder="Username">
|
||||||
|
|
Loading…
Reference in a new issue