Feat: トークンを削除できる機能 / Fix: meエンドポイントでレスポンスヘッダーにapplication/jsonがない問題 / Chg: エンドポイントの関数名をL.jsを統一
This commit is contained in:
@@ -5,6 +5,7 @@ import { randomBytes } from "node:crypto";
|
||||
import { UserEntity } from "@/modules/entities/User";
|
||||
import { ErrorBase } from "@/errors";
|
||||
import Logger from "@/lib/logger";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
const logger = new Logger("Repo | Token.ts");
|
||||
|
||||
@@ -83,4 +84,12 @@ export class TokenRepository extends EntityRepository<TokenEntity> {
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
async deleteToken(token: TokenEntity, fastify?: FastifyInstance) {
|
||||
await this.nativeDelete(token);
|
||||
this.em.clear();
|
||||
|
||||
if (fastify)
|
||||
fastify.orm.em.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ import type { FastifyInstance } from "fastify";
|
||||
|
||||
export default async function Me(fastify: FastifyInstance) {
|
||||
fastify.post("/", async (req, res) => {
|
||||
res.header("Content-Type", "application/json");
|
||||
|
||||
if ("error" in req.token)
|
||||
return res.code(400).send(req.token);
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import SignUp from "./signup";
|
||||
import SignIn from "./signin";
|
||||
import PrimarySignUp from "./signup";
|
||||
import PrimarySignIn from "./signin";
|
||||
|
||||
export default async function Primary(fastify: FastifyInstance) {
|
||||
await fastify.register(SignUp, {
|
||||
await fastify.register(PrimarySignUp, {
|
||||
prefix: "/signup",
|
||||
});
|
||||
|
||||
await fastify.register(SignIn, {
|
||||
await fastify.register(PrimarySignIn, {
|
||||
prefix: "/signin",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import { UserEntity } from "@/modules/entities/User";
|
||||
import { UserRepository } from "@/modules/repositories/User";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
export default function SignIn(fastify: FastifyInstance) {
|
||||
export default function PrimarySignIn(fastify: FastifyInstance) {
|
||||
const logger = new Logger("Endpoint | primary/signin");
|
||||
|
||||
fastify.post("/", async (req, res) => {
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { FastifyInstance } from "fastify";
|
||||
import { UserRepository } from "@/modules/repositories/User";
|
||||
import { DatabaseError, InputError } from "@/errors";
|
||||
|
||||
export default function SignUp(fastify: FastifyInstance) {
|
||||
export default function PrimarySignUp(fastify: FastifyInstance) {
|
||||
const logger = new Logger("Endpoint | primary/signup");
|
||||
|
||||
fastify.post("/", async (req, res) => {
|
||||
|
||||
@@ -5,7 +5,7 @@ import { UserRepository } from "@/modules/repositories/User";
|
||||
import { DatabaseError, ErrorBase, InputError } from "@/errors";
|
||||
import { UniqueConstraintViolationException } from "@mikro-orm/core";
|
||||
|
||||
export default function CreateAdmin(fastify: FastifyInstance) {
|
||||
export default function SetupCreateAdmin(fastify: FastifyInstance) {
|
||||
const logger = new Logger("Endpoint | setup/create-admin");
|
||||
|
||||
fastify.post("/", async (req, res) => {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import CreateAdmin from "./create-admin";
|
||||
import Initialization from "./initialization";
|
||||
import SetupCreateAdmin from "./create-admin";
|
||||
import SetupInitialization from "./initialization";
|
||||
|
||||
export default async function Setup(fastify: FastifyInstance) {
|
||||
await fastify.register(Initialization, {
|
||||
await fastify.register(SetupInitialization, {
|
||||
prefix: "/initialization",
|
||||
});
|
||||
|
||||
await fastify.register(CreateAdmin, {
|
||||
await fastify.register(SetupCreateAdmin, {
|
||||
prefix: "/create-admin",
|
||||
});
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { ConfigEntity } from "@/modules/entities/Config";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import z from "zod/v3";
|
||||
|
||||
export default function Initialization(fastify: FastifyInstance) {
|
||||
export default function SetupInitialization(fastify: FastifyInstance) {
|
||||
const logger = new Logger("Endpoint | setup/initialization");
|
||||
|
||||
fastify.post("/", async (req, res) => {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { DatabaseError } from "@/errors";
|
||||
import Logger from "@/lib/logger";
|
||||
import { TokenEntity } from "@/modules/entities/Token";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
export default function TokenDelete(fastify: FastifyInstance) {
|
||||
const logger = new Logger("Endpoint | token/delete");
|
||||
|
||||
fastify.post("/", async (req, res) => {
|
||||
res.header("Content-Type", "application/json");
|
||||
|
||||
if ("error" in req.token)
|
||||
return res.code(400).send(req.token);
|
||||
|
||||
try {
|
||||
await fastify.orm.em.getRepository(TokenEntity).deleteToken(req.token, fastify);
|
||||
|
||||
return res.send({
|
||||
success: true,
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error("Database Error: Server initialization failed:", err);
|
||||
|
||||
return res.code(500).send(DatabaseError());
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import TokenDelete from "./delete";
|
||||
|
||||
export default async function Token(fastify: FastifyInstance) {
|
||||
await fastify.register(TokenDelete, {
|
||||
prefix: "/delete",
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user