add: user registration API
This commit is contained in:
parent
cfd4fc3d21
commit
fba56106cc
@ -1,10 +1,12 @@
|
|||||||
import { type Client, Pool } from "https://deno.land/x/postgres@v0.19.3/mod.ts";
|
import { type Client, Pool } from "https://deno.land/x/postgres@v0.19.3/mod.ts";
|
||||||
import { postgresConfig } from "@core/db/pgConfig.ts";
|
import { postgresConfig, postgresConfigCred } from "@core/db/pgConfig.ts";
|
||||||
import { createMiddleware } from "hono/factory";
|
import { createMiddleware } from "hono/factory";
|
||||||
|
|
||||||
const pool = new Pool(postgresConfig, 4);
|
const pool = new Pool(postgresConfig, 4);
|
||||||
|
const poolCred = new Pool(postgresConfigCred, 2);
|
||||||
|
|
||||||
export const db = pool;
|
export const db = pool;
|
||||||
|
export const dbCred = poolCred;
|
||||||
|
|
||||||
export const dbMiddleware = createMiddleware(async (c, next) => {
|
export const dbMiddleware = createMiddleware(async (c, next) => {
|
||||||
const connection = await pool.connect();
|
const connection = await pool.connect();
|
||||||
@ -13,8 +15,16 @@ export const dbMiddleware = createMiddleware(async (c, next) => {
|
|||||||
connection.release();
|
connection.release();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const dbCredMiddleware = createMiddleware(async (c, next) => {
|
||||||
|
const connection = await poolCred.connect();
|
||||||
|
c.set("dbCred", connection);
|
||||||
|
await next();
|
||||||
|
connection.release();
|
||||||
|
})
|
||||||
|
|
||||||
declare module "hono" {
|
declare module "hono" {
|
||||||
interface ContextVariableMap {
|
interface ContextVariableMap {
|
||||||
db: Client;
|
db: Client;
|
||||||
|
dbCred: Client;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@cvsa/backend",
|
"name": "@cvsa/backend",
|
||||||
"imports": {
|
"imports": {
|
||||||
|
"@rabbit-company/argon2id": "jsr:@rabbit-company/argon2id@^2.1.0",
|
||||||
"hono": "jsr:@hono/hono@^4.7.5",
|
"hono": "jsr:@hono/hono@^4.7.5",
|
||||||
"zod": "npm:zod",
|
"zod": "npm:zod",
|
||||||
"yup": "npm:yup"
|
"yup": "npm:yup"
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import { dbMiddleware } from "./database.ts";
|
import { dbCredMiddleware, dbMiddleware } from "./database.ts";
|
||||||
import { rootHandler } from "./root.ts";
|
import { rootHandler } from "./root.ts";
|
||||||
import { getSnapshotsHanlder } from "./snapshots.ts";
|
import { getSnapshotsHanlder } from "./snapshots.ts";
|
||||||
|
import { registerHandler } from "./register.ts";
|
||||||
|
|
||||||
export const app = new Hono();
|
export const app = new Hono();
|
||||||
|
|
||||||
app.use('/video/*', dbMiddleware);
|
app.use('/video/*', dbMiddleware);
|
||||||
|
app.use('/user', dbCredMiddleware);
|
||||||
|
|
||||||
app.get("/", ...rootHandler);
|
app.get("/", ...rootHandler);
|
||||||
|
|
||||||
app.get('/video/:id/snapshots', ...getSnapshotsHanlder);
|
app.get('/video/:id/snapshots', ...getSnapshotsHanlder);
|
||||||
|
app.post('/user', ...registerHandler);
|
||||||
|
|
||||||
const fetch = app.fetch;
|
const fetch = app.fetch;
|
||||||
|
|
||||||
@ -17,4 +20,4 @@ export default {
|
|||||||
fetch,
|
fetch,
|
||||||
} satisfies Deno.ServeDefaultExport;
|
} satisfies Deno.ServeDefaultExport;
|
||||||
|
|
||||||
export const VERSION = "0.2.4";
|
export const VERSION = "0.3.0";
|
27
packages/backend/register.ts
Normal file
27
packages/backend/register.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { createHandlers } from "./utils.ts";
|
||||||
|
import Argon2id from "@rabbit-company/argon2id";
|
||||||
|
|
||||||
|
export const registerHandler = createHandlers(async (c) => {
|
||||||
|
try {
|
||||||
|
const client = c.get("dbCred");
|
||||||
|
const body = await c.req.json();
|
||||||
|
const username = body.username;
|
||||||
|
const password = body.password;
|
||||||
|
const hash = await Argon2id.hashEncoded(password);
|
||||||
|
const query = `
|
||||||
|
INSERT INTO users (username, password) VALUES ($1, $2)
|
||||||
|
`;
|
||||||
|
await client.queryObject(query, [username, hash]);
|
||||||
|
return c.json({
|
||||||
|
success: true,
|
||||||
|
message: "Registered",
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof SyntaxError) {
|
||||||
|
return c.json({ error: "Invalid JSON" }, 400);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return c.json({ error: (e as Error).message }, 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user