Finish user creation
[blerg.git] / http_blerg.c
index f8513c5..6a6de82 100644 (file)
@@ -4,6 +4,7 @@
 #include <microhttpd.h>
 #include "database.h"
 #include "tags.h"
+#include "auth.h"
 
 #define URL_INFO_AUTHOR 0x1
 #define URL_INFO_RECORD 0x2
@@ -18,6 +19,7 @@ struct create_state {
 struct MHD_Response *response_404;
 struct MHD_Response *response_501;
 struct MHD_Response *response_JSON_Success;
+struct MHD_Response *response_JSON_Failure;
 
 void init_responses() {
 #define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
@@ -28,6 +30,9 @@ void init_responses() {
 
 #define JSON_SUCCESS "{status: \"success\"}"
        response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
+
+#define JSON_FAILURE "{status: \"failure\"}"
+       response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
 }
 
 int respond_404(struct MHD_Connection *connection) {
@@ -42,6 +47,10 @@ int respond_JSON_Success(struct MHD_Connection *connection) {
        return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
 }
 
+int respond_JSON_Failure(struct MHD_Connection *connection) {
+       return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure);
+}
+
 int parse_url_info(const char *url, char *author, uint64_t *record) {
        const char *c;
        int ret = 0;
@@ -199,14 +208,15 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
                        *upload_data_size = 0;
                        return MHD_YES;
                } else {
-                       printf("username: %s, password: %s\n", cs->username, cs->password);
-
                        if (cs->username[0] == 0 || cs->password[0] == 0)
-                               return MHD_NO; // TODO: Give a better response
+                               return respond_JSON_Failure(connection);
+
+                       if (blerg_exists(cs->username))
+                               return respond_JSON_Failure(connection);
 
                        struct blerg *b = blerg_open(cs->username);
                        blerg_close(b);
-                       /* auth_set_password(cs->username, cs->password); */
+                       auth_set_password(cs->username, cs->password);
 
                        MHD_destroy_post_processor(cs->pp);
                        free(cs);