added database
This commit is contained in:
1 parent
c4ea7a4293
commit
4f67cf11bd
30 files changed
+601
-267
No files matched your search
+31
-30
@@ -1,36 +1,37 @@
|
||||
#include "user.h"
|
||||
#include "jwt.h"
|
||||
#include "user.h"
|
||||
#include "utils.h"
|
||||
|
||||
void get_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
request_with_token *requestdata = check_body(hm);
|
||||
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
|
||||
if (!valid) {
|
||||
mg_http_reply(c, 403, "", "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("invalid token"));
|
||||
return;
|
||||
}
|
||||
const User *user = new_user("username", "", "mail@example.com", "root", true);
|
||||
if (user == NULL) {
|
||||
mg_http_reply(c, 404, "", "{%m:%m}", MG_ESC("error"), MG_ESC("user not found"));
|
||||
return;
|
||||
}
|
||||
char responsedata[2048] = {0};
|
||||
snprintf(
|
||||
responsedata,
|
||||
sizeof(responsedata),
|
||||
"{\"id\": %d, \"username\": \"%s\", \"email\": \"%s\", \"group\": \"%s\", \"is_superuser\": %s}",
|
||||
user->id,
|
||||
user->username,
|
||||
user->email,
|
||||
user->group,
|
||||
user->is_superuser ? "true" : "false"
|
||||
);
|
||||
request_with_token *requestdata = check_body(hm);
|
||||
if (!jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET)) {
|
||||
free_request_with_token(requestdata);
|
||||
mg_http_reply(c, 403, "", "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("invalid token"));
|
||||
return;
|
||||
}
|
||||
cJSON *payload = jwt_get_payload(requestdata->token);
|
||||
char *username =
|
||||
strdup(cJSON_GetObjectItem(payload, "username")->valuestring);
|
||||
User *user = db_get_user_by_username(username);
|
||||
if (!user) {
|
||||
free_request_with_token(requestdata);
|
||||
cJSON_Delete(payload);
|
||||
free(username);
|
||||
mg_http_reply(c, 404, "", "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("user not found"));
|
||||
return;
|
||||
}
|
||||
char responsedata[2048] = {0};
|
||||
snprintf(responsedata, sizeof(responsedata),
|
||||
"{\"id\": %d, \"username\": \"%s\", \"email\": \"%s\", \"group\": "
|
||||
"\"%s\", \"is_superuser\": %s}",
|
||||
user->id, user->username, user->email, user->group,
|
||||
user->is_superuser ? "true" : "false");
|
||||
|
||||
mg_http_reply(
|
||||
c,
|
||||
200,
|
||||
"Content-Type: application/json\r\n",
|
||||
responsedata
|
||||
);
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n", responsedata);
|
||||
free_request_with_token(requestdata);
|
||||
free(username);
|
||||
free_user(user);
|
||||
cJSON_Delete(payload);
|
||||
}
|
||||
Reference in new issue
Block a user