Add the register user endpoint, add a middleware for converting form data to JSON

This commit is contained in:
Dave Smith-Hayes 2024-02-16 21:13:13 -05:00
parent 2258b651de
commit 480ccfa811
4 changed files with 34 additions and 20 deletions

View File

@ -1,4 +1,4 @@
import { Connection, Pool, createPool } from 'mariadb';
import { PoolConnection, Pool, createPool } from 'mariadb';
type ConnectionConfig = {
host: string,
@ -30,7 +30,7 @@ export default class Database {
return Database.instance;
}
public async getConnection(): Promise<Connection> {
public async getConnection(): Promise<PoolConnection> {
return this.pool.getConnection();
}
}

View File

@ -0,0 +1,13 @@
import { Context } from 'hono';
export default async function parsedForm(c: Context, next) {
if (c.req.method == 'POST') {
if (c.req.header('Content-Type') == 'application/json') {
next();
} else {
}
}
}

View File

@ -1,28 +1,28 @@
import { Hono, Context } from 'hono';
import { Connection } from 'mariadb';
import User from '@slovo/models/User';
import Database from '@slovo/infrastructure/database';
import { PoolConnection } from 'mariadb';
import User from 'slovo/models/User';
import Database from 'slovo/infrastructure/database';
const app = new Hono();
// add routes and methods here
app.post('/register', async (c: Context) => {
// check if form (from the web app) or JSON (from a blank API call);
let user: User;
if (c.req.header('Content-Type') == 'application/json') {
user = await c.req.json<User>();
} else {
user = await c.req.formData<User>();
}
const user: User = await c.req.json<User>();
const conn: Connection = await Database.getInstance().getConnection();
const query = `INSERT INTO users (username, password) VALUES (?, ?)`;
const conn: PoolConnection = await Database.getInstance().getConnection();
const query = `INSERT INTO users (username, name, password) VALUES (?, ?)`;
try {
const results = await conn.query(query, [ user.username, user.password ]);
const results = await conn.query(query, [
user.username,
user.name,
user.password
]);
console.log(results);
await conn.release();
return c.json({
message: "Registration successful.",
});

View File

@ -2,9 +2,10 @@
"compilerOptions": {
"strict": true,
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
},
"paths": {
"@slovo/*": [ "./src/*" ]
"jsxImportSource": "hono/jsx",
"baseUrl": "./src",
"paths": {
"slovo/*": [ "*" ]
}
}
}