updated auth function and error handling