diff --git a/examples/auth.c b/examples/auth.c
index 89a5599107e4784e07e9867b25667e7dd33aedd2..a5ffca420af0b23702fa745db8f08b676dd51cba 100644
--- a/examples/auth.c
+++ b/examples/auth.c
@@ -2,38 +2,25 @@
 #include <string.h>
 #include "mongoose.h"
 
-static int index_html(struct mg_connection *conn) {
-  mg_send_header(conn, "Content-Type", "text/html");
-  mg_printf_data(conn, "%s",
-                 "This link is password-protected: <a href=/secret>link</a>");
-  return 1;
-}
-
-static int secret_html(struct mg_connection *conn) {
-  static const char *passwords_file = "my_passwords.txt";
-  FILE *fp = fopen(passwords_file, "r");
+static int auth_handler(struct mg_connection *conn) {
+  int result = 0; // Not authorized
+  FILE *fp;
 
   // To populate passwords file, do
   // mongoose -A my_passwords.txt mydomain.com admin admin
-
-  if (mg_authorize_digest(conn, fp)) {
-    mg_printf_data(conn, "%s", "Hi, here is a secret message!");
-  } else {
-    mg_send_digest_auth_request(conn);
-  }
-
-  if (fp != NULL) {
+  if ((fp = fopen("my_passwords.txt", "r")) != NULL) {
+    result = mg_authorize_digest(conn, fp);
     fclose(fp);
   }
 
-  return 1;
+  return result;
 }
 
 int main(void) {
   struct mg_server *server = mg_create_server(NULL);
   mg_set_option(server, "listening_port", "8080");
-  mg_add_uri_handler(server, "/", index_html);
-  mg_add_uri_handler(server, "/secret", secret_html);
+  mg_set_option(server, "document_root", ".");
+  mg_set_auth_handler(server, auth_handler);
 
   printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
   for (;;) {
diff --git a/mongoose.c b/mongoose.c
index bbfa99ab4a1aa27de0a34b6ae7d78ebd0cb399b8..0ba8b935514cc61465c55aa4c832b1ce60903398 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -273,6 +273,7 @@ struct mg_server {
   struct ll active_connections;
   struct ll uri_handlers;
   mg_handler_t error_handler;
+  mg_handler_t auth_handler;
   char *config_options[NUM_OPTIONS];
   void *server_data;
 #ifdef MONGOOSE_USE_SSL
@@ -3358,6 +3359,13 @@ static void open_local_endpoint(struct connection *conn) {
   const char *dir_lst = conn->server->config_options[ENABLE_DIRECTORY_LISTING];
 #endif
 
+  // Call auth handler
+  if (conn->server->auth_handler != NULL &&
+      conn->server->auth_handler(&conn->mg_conn) == 0) {
+    mg_send_digest_auth_request(&conn->mg_conn);
+    return;
+  }
+
   // Call URI handler if one is registered for this URI
   conn->endpoint.uh = find_uri_handler(conn->server, conn->mg_conn.uri);
   if (conn->endpoint.uh != NULL) {
@@ -4117,6 +4125,10 @@ void mg_set_http_error_handler(struct mg_server *server, mg_handler_t handler) {
   server->error_handler = handler;
 }
 
+void mg_set_auth_handler(struct mg_server *server, mg_handler_t handler) {
+  server->auth_handler = handler;
+}
+
 void mg_set_listening_socket(struct mg_server *server, int sock) {
   if (server->listening_sock != INVALID_SOCKET) {
     closesocket(server->listening_sock);
diff --git a/mongoose.h b/mongoose.h
index 7121f998821f031e29f0269170a90f2a9ceea47a..85b6056e76686f1c2e327daee6d0b956433839d5 100644
--- a/mongoose.h
+++ b/mongoose.h
@@ -65,6 +65,7 @@ const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
 unsigned int mg_poll_server(struct mg_server *, int milliseconds);
 void mg_add_uri_handler(struct mg_server *, const char *uri, mg_handler_t);
 void mg_set_http_error_handler(struct mg_server *, mg_handler_t);
+void mg_set_auth_handler(struct mg_server *, mg_handler_t);
 const char **mg_get_valid_option_names(void);
 const char *mg_get_option(const struct mg_server *server, const char *name);
 void mg_set_listening_socket(struct mg_server *, int sock);
diff --git a/unit_test.c b/unit_test.c
index 68162caca605bfa7d807eed02215683843721aa1..171265f8e18577946ef5d82c1abfcdc13e99a2a2 100644
--- a/unit_test.c
+++ b/unit_test.c
@@ -475,7 +475,6 @@ static int cb4(struct mg_connection *conn) {
 }
 
 static int cb3(struct mg_connection *conn) {
-  printf("cb3: %d\n", conn->status_code);
   fflush(stdout);
   sprintf((char *) conn->connection_param, "%d", conn->status_code);
   return 1;