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 = {
|
type ConnectionConfig = {
|
||||||
host: string,
|
host: string,
|
||||||
@ -30,7 +30,7 @@ export default class Database {
|
|||||||
return Database.instance;
|
return Database.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getConnection(): Promise<Connection> {
|
public async getConnection(): Promise<PoolConnection> {
|
||||||
return this.pool.getConnection();
|
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 { Hono, Context } from 'hono';
|
||||||
import { Connection } from 'mariadb';
|
import { PoolConnection } from 'mariadb';
|
||||||
import User from '@slovo/models/User';
|
import User from 'slovo/models/User';
|
||||||
import Database from '@slovo/infrastructure/database';
|
import Database from 'slovo/infrastructure/database';
|
||||||
|
|
||||||
const app = new Hono();
|
const app = new Hono();
|
||||||
|
|
||||||
// add routes and methods here
|
// add routes and methods here
|
||||||
app.post('/register', async (c: Context) => {
|
app.post('/register', async (c: Context) => {
|
||||||
// check if form (from the web app) or JSON (from a blank API call);
|
const user: User = await c.req.json<User>();
|
||||||
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 conn: PoolConnection = await Database.getInstance().getConnection();
|
||||||
const conn: Connection = await Database.getInstance().getConnection();
|
const query = `INSERT INTO users (username, name, password) VALUES (?, ?)`;
|
||||||
const query = `INSERT INTO users (username, password) VALUES (?, ?)`;
|
|
||||||
|
|
||||||
try {
|
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);
|
console.log(results);
|
||||||
|
|
||||||
|
await conn.release();
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
message: "Registration successful.",
|
message: "Registration successful.",
|
||||||
});
|
});
|
||||||
|
@ -2,9 +2,10 @@
|
|||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"jsx": "react-jsx",
|
"jsx": "react-jsx",
|
||||||
"jsxImportSource": "hono/jsx"
|
"jsxImportSource": "hono/jsx",
|
||||||
},
|
"baseUrl": "./src",
|
||||||
"paths": {
|
"paths": {
|
||||||
"@slovo/*": [ "./src/*" ]
|
"slovo/*": [ "*" ]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user