Simplify API authentication check
Fix memory leaks on apiKey functions, close database connections