Example #1
0
func GetAuthtokenEmail(reqJson []byte, qs map[string][]string, uri []string, ac *dozy.AuthContext, rawDb interface{}) (uint, []byte, string) {
	db := rawDb.(GetAuthtokenEmailDb)

	var resJson map[string]interface{}

	if ac.GetLoggedIn() {
		return dozy.StatusIncorrectPermissions, []byte(""), ""
	}

	getHashStatus, hash, userId := db.GetUserHashIdByEmail(qs["email"][0])

	if getHashStatus == mongodb.ReadSucceeded {
		loginOk, needNewHash, newHash := ac.Login(userId, qs["password"][0], hash)

		if loginOk {
			resJson = map[string]interface{}{
				"succeeded": true,
				"authtoken": ac.GetAuthtoken(),
				"userId":    ac.GetUserId(),
			}

			if needNewHash {
				updateHashStatus := db.UpdateHashById(userId, newHash)

				if updateHashStatus != mongodb.WriteSucceeded {
					panic(updateHashStatus)
				}
			}
		} else {
			resJson = map[string]interface{}{
				"succeeded":              false,
				"failedDueToBadPassword": true,
				"failedDueToBadUsername": false,
			}
		}
	} else if getHashStatus == mongodb.ReadNotFound {
		resJson = map[string]interface{}{
			"succeeded":              false,
			"failedDueToBadPassword": false,
			"failedDueToBadUsername": true,
		}
	} else {
		panic(getHashStatus)
	}

	resJsonBytes, marshalErr := json.Marshal(resJson)
	if marshalErr != nil {
		panic(marshalErr)
	}
	return dozy.StatusGetOk, resJsonBytes, ""
}