user session works

This commit is contained in:
SimonFJ20 2025-03-12 15:58:13 +01:00
parent be00f1c965
commit 1b8ffa54a0
5 changed files with 43 additions and 29 deletions

View File

@ -1,22 +1,22 @@
CREATE TABLE IF NOT EXISTS users (
id INT PRIMARY KEY,
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL,
password_hash TEXT NOT NULL,
balance_dkk_cent INT NOT NULL
balance_dkk_cent INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS coords (
id INT PRIMARY KEY,
x INT NOT NULL,
y INT NOT NULL
id INTEGER PRIMARY KEY,
x INTEGER NOT NULL,
y INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS products (
id INT PRIMARY KEY,
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
price_dkk_cent INT NOT NULL,
price_dkk_cent INTEGER NOT NULL,
description TEXT NOT NULL,
coord INT,
barcode TEXT,
@ -25,31 +25,36 @@ CREATE TABLE IF NOT EXISTS products (
);
CREATE TABLE IF NOT EXISTS product_prices (
id INT PRIMARY KEY,
product INT NOT NULL,
price_dkk_cent INT NOT NULL,
id INTEGER PRIMARY KEY,
product INTEGER NOT NULL,
price_dkk_cent INTEGER NOT NULL,
FOREIGN KEY(product) REFERENCES products(id)
);
CREATE TABLE IF NOT EXISTS carts (
id INT PRIMARY KEY,
user INT NOT NULL,
id INTEGER PRIMARY KEY,
user INTEGER NOT NULL,
FOREIGN KEY(user) REFERENCES users(id)
);
CREATE TABLE IF NOT EXISTS cart_items (
id INT PRIMARY KEY,
cart INT NOT NULL,
amount INT NOT NULL,
id INTEGER PRIMARY KEY,
cart INTEGER NOT NULL,
amount INTEGER NOT NULL,
FOREIGN KEY(cart) REFERENCES carts(id)
);
INSERT OR REPLACE INTO users VALUES(1,'User','test@email.com','08ce0220f6d63d85c3ac313e308f4fca35ecfb850baa8ddb924cfab98137b6b18b4a8e027067cb98802757df1337246a0f3aa25c44c2b788517a871086419dcf',10000);
INSERT OR REPLACE INTO users (name, email, password_hash, balance_dkk_cent)
VALUES ('User','test@email.com','08ce0220f6d63d85c3ac313e308f4fca35ecfb850baa8ddb924cfab98137b6b18b4a8e027067cb98802757df1337246a0f3aa25c44c2b788517a871086419dcf',10000);
INSERT OR REPLACE INTO products (name, price_dkk_cent, description, coord, barcode)
VALUES ('Letmælk',1195,'Mælk fra ko',NULL,NULL);
INSERT OR REPLACE INTO products (name, price_dkk_cent, description, coord, barcode)
VALUES ('Smør',2000,'Smør fra mejeri',NULL,NULL);
INSERT OR REPLACE INTO products VALUES(1,'Letmælk',1195,'Mælk fra ko',NULL,NULL);
INSERT OR REPLACE INTO products VALUES(2,'Smør',2000,'Smør fra mejeri',NULL,NULL);

View File

@ -100,7 +100,7 @@ const Session* middleware_session(HttpCtx* ctx)
{
const Session* session = header_session(ctx);
if (!session) {
RESPOND_JSON(ctx, 200, "{\"ok\":false,\"msg\":\"unauthorized\"}");
RESPOND_JSON(ctx, 400, "{\"ok\":false,\"msg\":\"unauthorized\"}");
return NULL;
}
return session;

View File

@ -185,7 +185,8 @@ DbRes db_user_from_email(Db* db, User* user, const char* email)
" FROM users WHERE email = ?",
-1, &stmt, NULL);
if (prepare_res != SQLITE_OK) {
fprintf(stderr, "error: %s\n at %s:%d\n", sqlite3_errmsg(connection), __func__, __LINE__);
fprintf(stderr, "error: %s\n at %s:%d\n", sqlite3_errmsg(connection),
__func__, __LINE__);
res = DbRes_Error;
goto l0_return;
}
@ -197,7 +198,8 @@ DbRes db_user_from_email(Db* db, User* user, const char* email)
goto l0_return;
} else if (step_res != SQLITE_ROW) {
printf("step_res = %d, email = '%s'\n", step_res, email);
fprintf(stderr, "error: %s\n at %s:%d\n", sqlite3_errmsg(connection), __func__, __LINE__);
fprintf(stderr, "error: %s\n at %s:%d\n", sqlite3_errmsg(connection),
__func__, __LINE__);
res = DbRes_Error;
goto l0_return;
}
@ -217,7 +219,6 @@ l0_return:
return res;
}
DbRes db_product_all(Db* db, ProductVec* vec)
{
sqlite3* connection;

View File

@ -89,8 +89,6 @@ int http_server_listen(HttpServer* server)
return -1;
}
printf("accepted\n");
Client req = { .file = res, client_addr };
pthread_mutex_lock(&ctx->mutex);

View File

@ -29,11 +29,21 @@ Deno.test("test", async () => {
assertMatch(loginRes.token, /^[0-9a-zA-Z]+$/);
const token = loginRes.token;
const sessionUserRes = await axios.post(
`${url}/api/sessions/login`,
{ email, password },
{ responseType: "json" },
).then((res) => res.data);
const sessionUserRes = await axios.get(
`${url}/api/sessions/user`,
{ headers: { "Session-Token": token } },
)
.then((res) => res.data)
.catch((error) => error.response.data);
assertEquals(sessionUserRes.ok, true);
console.log(sessionUserRes.user);
const logoutRes = await axios.post(
`${url}/api/sessions/logout`,
{},
{ responseType: "json", headers: { "Session-Token": token } },
).then((res) => res.data);
assertEquals(logoutRes, { ok: true });
});