Moritz Schmidt 9 gadi atpakaļ
vecāks
revīzija
d07ac6b3d9
1 mainītis faili ar 16 papildinājumiem un 14 dzēšanām
  1. 16 14
      usermanager.go

+ 16 - 14
usermanager.go

@@ -26,6 +26,9 @@ const ( // Error constants
   ERR_USERNAME_TAKEN = "ERR_USERNAME_TAKEN"
   ERR_INVALID_PARAM = "ERR_INVALID_PARAM"
   ERR_NO_CHANGES = "ERR_NO_CHANGES"
+  SUCCESS_LOGIN = "SUCCESS_LOGIN"
+  SUCCESS_UPDATE = "SUCCESS_UPDATE"
+  SUCCESS_TOKENS_GENERATED = "SUCCESS_TOKENS_GENERATED"
 )
 
 type User struct { // User
@@ -125,13 +128,13 @@ func LogoutHandler(ctx *iris.Context) {
 
   user, err := GetUserFromDB(userID)
   if err != nil {
-    templatehelpers.ShowError(err.Error(), ctx, "account")
+    templatehelpers.ShowError([]string{err.Error()}, ctx)
     return
   }
 
   user.Logout(userID);
   ctx.SetCookieKV("token", "")
-  ctx.Redirect("/")
+  templatehelpers.ShowNotification([]string{"Successfully logged out", "login"}, ctx)
 }
 
 func (user *User) Update() error {
@@ -223,7 +226,7 @@ func CanBeAuthedHandler(ctx *iris.Context) {
     ctx.Set("userID", userID) // save userID for in-context use
   } else if err != nil {
     if !((err.Error() != "ERR_SESSION_TIMED_OUT") || (err.Error() != "ERR_INVALID_TOKEN")) { // ignore ERR_SESSION_TIMED_OUT and ERR_INVALID_TOKEN
-      templatehelpers.ShowError(err.Error(), ctx, "register")
+      templatehelpers.ShowError([]string{err.Error()}, ctx)
       return
     }
   }
@@ -245,7 +248,7 @@ func AdminHandler(ctx *iris.Context) {
     }
 }
 
-func GenerateTokens(numTokens int) []string {
+func GenerateTokens(numTokens int) ([]string, error) {
   const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
     tokens := make([]string, 0)
     dbTokens := make([][]string, 0)
@@ -259,12 +262,11 @@ func GenerateTokens(numTokens int) []string {
       dbTokens = [][]string{[]string{"value", string(b)}, []string{"used", "0"}}
       err := databaseutils.DBUtil.InsertRow("tokens", dbTokens)
       if err != nil {
-        fmt.Println(err.Error())
-        return []string{""}
+        return []string{""}, err
       }
     }
 
-    return tokens
+    return tokens, nil
 }
 
 func GetTokens(used bool) []string {
@@ -440,14 +442,14 @@ func RegisterHandler(ctx *iris.Context) {
   usedToken := IsTokenUsed(usedTokens, token) // check if token is used
 
   if !unusedToken && !usedToken { // token doesnt exist
-    templatehelpers.ShowError(ERR_INVALID_TOKEN, ctx, "register")
+    templatehelpers.ShowError([]string{ERR_INVALID_TOKEN}, ctx)
     return
   }
 
   tokenUserID, err := SearchUserByTokenInDB(token)
   if err != nil { // id of user, we're going to change if exists
     if err.Error() != "ERR_EMPTY_RESULT" { // if no user found for that token let them register
-      templatehelpers.ShowError(err.Error(), ctx, "register")
+      templatehelpers.ShowError([]string{err.Error()}, ctx)
       return
     }
   }
@@ -460,26 +462,26 @@ func RegisterHandler(ctx *iris.Context) {
 
     err := RegisterUserWithToken(username, string(passwordBin), token) // register user
     if err != nil {
-      templatehelpers.ShowError(err.Error(), ctx, "register")
+      templatehelpers.ShowError([]string{err.Error()}, ctx)
       return
     }
 
     tokenString, err := user.Login(username, password) // try to login
 
     if err != nil {
-      templatehelpers.ShowError(err.Error(), ctx, "login")
+      templatehelpers.ShowError([]string{err.Error()}, ctx)
     } else {
       ctx.SetCookieKV("token", tokenString) // set tokenString as cookie
-      templatehelpers.ShowNotification("registration successfull", ctx, "home")
+      templatehelpers.ShowNotification([]string{"registration successfull", "home"}, ctx)
     }
 
   } else { // used token -> update
     if err := UserUpdateProcessor(username, password, tokenUserIDStr); err != nil { // simply try to update
-      templatehelpers.ShowError(err.Error(), ctx, "register")
+      templatehelpers.ShowError([]string{err.Error()}, ctx)
       return
     } else {
       user.Logout(tokenUserIDStr) // log user out from system
-      templatehelpers.ShowNotification("reset successfull", ctx, "login")
+      templatehelpers.ShowNotification([]string{"reset successfull", "login"}, ctx)
     }
   }
 }