Skip to content

Instantly share code, notes, and snippets.

@graywolf336
Created June 26, 2020 20:48
Show Gist options
  • Save graywolf336/214b5d53ce7447643ff3c00cf8c9a23f to your computer and use it in GitHub Desktop.
Save graywolf336/214b5d53ce7447643ff3c00cf8c9a23f to your computer and use it in GitHub Desktop.
Returning multiple values from a single function
package controllers
func getAccountUserAndWorkspaceFromToken(token, workspaceID string) (*models.Account, *models.User, *models.Workspace, error) {
client := core.GetClient()
userID := ""
accountID := ""
if len(token) > 0 {
user, err := client.Utils().ValidateUserJWTToken(token)
if err != nil {
return nil, nil, nil, utils.GenerateUnauthorized(err, 222)
}
userID = user.UserID.Hex()
accountID = user.AccountID.Hex()
}
if _userID, ok := c.Get("userId"); ok && len(_userID.(string)) > 0 {
userID = _userID.(string)
}
if _accountID, ok := c.Get("accountId"); ok && len(_accountID.(string)) > 0 {
accountID = _accountID.(string)
}
if len(userID) < 1 || len(accountID) < 1 {
return nil, nil, nil, utils.GenerateBadRequest(nil, 223, "account is missing")
}
account, err := client.Accounts().Get(accountID)
if err != nil || !account.ID.Valid() {
return nil, nil, nil, utils.GenerateBadRequest(nil, 224, "account is missing")
}
user, err := client.Users().Get(userID)
if err != nil {
return nil, nil, nil, utils.GenerateBadRequest(err, 225, "internal error occured")
}
workspace, err := client.Workspaces().Get(workspaceID)
if err != nil || workspace == nil || !workspace.ID.Valid() {
return nil, nil, nil, utils.GenerateBadRequest(nil, 226, "workspace is missing")
}
if workspace.Account.ID != account.ID {
return nil, nil, nil, utils.GenerateUnauthorized(err, 227)
}
return account, user, workspace, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment