Add the register user endpoint, add a middleware for converting form data to JSON
This commit is contained in:
parent
2258b651de
commit
480ccfa811
@ -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();
|
||||
}
|
||||
}
|
||||
|
13
server/src/middleware/form-to-json.ts
Normal file
13
server/src/middleware/form-to-json.ts
Normal 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 {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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.",
|
||||
});
|
||||
|
@ -2,9 +2,10 @@
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"jsx": "react-jsx",
|
||||
"jsxImportSource": "hono/jsx"
|
||||
},
|
||||
"jsxImportSource": "hono/jsx",
|
||||
"baseUrl": "./src",
|
||||
"paths": {
|
||||
"@slovo/*": [ "./src/*" ]
|
||||
"slovo/*": [ "*" ]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user