mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 22:09:16 +01:00
refactor(api): ♻️ Remove old redirect() and response() in favour of Hono's builtins
This commit is contained in:
parent
691716f7eb
commit
69d7d50239
20 changed files with 188 additions and 174 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { redirect } from "@/response";
|
||||
import type { Context } from "@hono/hono";
|
||||
import { setCookie } from "@hono/hono/cookie";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { eq, or } from "drizzle-orm";
|
||||
import { SignJWT } from "jose";
|
||||
|
|
@ -87,11 +88,11 @@ const route = createRoute({
|
|||
},
|
||||
});
|
||||
|
||||
const returnError = (query: object, error: string, description: string) => {
|
||||
const returnError = (context: Context, error: string, description: string) => {
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
// Add all data that is not undefined except email and password
|
||||
for (const [key, value] of Object.entries(query)) {
|
||||
for (const [key, value] of Object.entries(context.req.query())) {
|
||||
if (key !== "email" && key !== "password" && value !== undefined) {
|
||||
searchParams.append(key, value);
|
||||
}
|
||||
|
|
@ -100,11 +101,11 @@ const returnError = (query: object, error: string, description: string) => {
|
|||
searchParams.append("error", error);
|
||||
searchParams.append("error_description", description);
|
||||
|
||||
return redirect(
|
||||
return context.redirect(
|
||||
new URL(
|
||||
`${config.frontend.routes.login}?${searchParams.toString()}`,
|
||||
config.http.base_url,
|
||||
),
|
||||
).toString(),
|
||||
);
|
||||
};
|
||||
|
||||
|
|
@ -112,7 +113,7 @@ export default apiRoute((app) =>
|
|||
app.openapi(route, async (context) => {
|
||||
if (config.oidc.forced) {
|
||||
return returnError(
|
||||
context.req.query(),
|
||||
context,
|
||||
"invalid_request",
|
||||
"Logging in with a password is disabled by the administrator. Please use a valid OpenID Connect provider.",
|
||||
);
|
||||
|
|
@ -136,14 +137,14 @@ export default apiRoute((app) =>
|
|||
)
|
||||
) {
|
||||
return returnError(
|
||||
context.req.query(),
|
||||
context,
|
||||
"invalid_grant",
|
||||
"Invalid identifier or password",
|
||||
);
|
||||
}
|
||||
|
||||
if (user.data.passwordResetToken) {
|
||||
return redirect(
|
||||
return context.redirect(
|
||||
`${config.frontend.routes.password_reset}?${new URLSearchParams(
|
||||
{
|
||||
token: user.data.passwordResetToken ?? "",
|
||||
|
|
@ -198,14 +199,15 @@ export default apiRoute((app) =>
|
|||
}
|
||||
|
||||
// Redirect to OAuth authorize with JWT
|
||||
return redirect(
|
||||
setCookie(context, "jwt", jwt, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
sameSite: "Strict",
|
||||
path: "/",
|
||||
maxAge: 60 * 60,
|
||||
});
|
||||
return context.redirect(
|
||||
`${config.frontend.routes.consent}?${searchParams.toString()}`,
|
||||
302,
|
||||
{
|
||||
"Set-Cookie": `jwt=${jwt}; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age=${
|
||||
60 * 60
|
||||
}`,
|
||||
},
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -51,12 +51,11 @@ export default apiRoute((app) =>
|
|||
const { redirect_uri, client_id, code } = context.req.valid("query");
|
||||
|
||||
const redirectToLogin = (error: string) =>
|
||||
Response.redirect(
|
||||
context.redirect(
|
||||
`${config.frontend.routes.login}?${new URLSearchParams({
|
||||
...context.req.query,
|
||||
error: encodeURIComponent(error),
|
||||
}).toString()}`,
|
||||
302,
|
||||
);
|
||||
|
||||
const foundToken = await db
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { response } from "@/response";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { eq } from "drizzle-orm";
|
||||
import type { Context } from "hono";
|
||||
import { z } from "zod";
|
||||
import { Users } from "~/drizzle/schema";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
|
@ -50,21 +50,26 @@ const route = createRoute({
|
|||
},
|
||||
});
|
||||
|
||||
const returnError = (token: string, error: string, description: string) => {
|
||||
const returnError = (
|
||||
context: Context,
|
||||
token: string,
|
||||
error: string,
|
||||
description: string,
|
||||
) => {
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
searchParams.append("error", error);
|
||||
searchParams.append("error_description", description);
|
||||
searchParams.append("token", token);
|
||||
|
||||
return response(null, 302, {
|
||||
Location: new URL(
|
||||
return context.redirect(
|
||||
new URL(
|
||||
`${
|
||||
config.frontend.routes.password_reset
|
||||
}?${searchParams.toString()}`,
|
||||
config.http.base_url,
|
||||
).toString(),
|
||||
});
|
||||
);
|
||||
};
|
||||
|
||||
export default apiRoute((app) =>
|
||||
|
|
@ -74,7 +79,12 @@ export default apiRoute((app) =>
|
|||
const user = await User.fromSql(eq(Users.passwordResetToken, token));
|
||||
|
||||
if (!user) {
|
||||
return returnError(token, "invalid_token", "Invalid token");
|
||||
return returnError(
|
||||
context,
|
||||
token,
|
||||
"invalid_token",
|
||||
"Invalid token",
|
||||
);
|
||||
}
|
||||
|
||||
await user.update({
|
||||
|
|
@ -82,8 +92,8 @@ export default apiRoute((app) =>
|
|||
passwordResetToken: null,
|
||||
});
|
||||
|
||||
return response(null, 302, {
|
||||
Location: `${config.frontend.routes.password_reset}?success=true`,
|
||||
});
|
||||
return context.redirect(
|
||||
`${config.frontend.routes.password_reset}?success=true`,
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import {
|
|||
jsonOrForm,
|
||||
} from "@/api";
|
||||
import { mimeLookup } from "@/content_types";
|
||||
import { response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -110,7 +109,7 @@ export default apiRoute((app) =>
|
|||
|
||||
await db.delete(Emojis).where(eq(Emojis.id, id));
|
||||
|
||||
return response(null, 204);
|
||||
return context.newResponse(null, 204);
|
||||
}
|
||||
|
||||
case "PATCH": {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {
|
|||
handleZodError,
|
||||
idValidator,
|
||||
} from "@/api";
|
||||
import { response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { MediaManager } from "~/classes/media/media-manager";
|
||||
|
|
@ -71,7 +70,7 @@ export default apiRoute((app) =>
|
|||
if (attachment.data.url) {
|
||||
return context.json(attachment.toApi());
|
||||
}
|
||||
return response(null, 206);
|
||||
return context.newResponse(null, 206);
|
||||
}
|
||||
case "PUT": {
|
||||
const { description, thumbnail } =
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { response } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
|
@ -76,7 +75,7 @@ export default apiRoute((app) =>
|
|||
|
||||
await role.linkUser(user.id);
|
||||
|
||||
return response(null, 204);
|
||||
return context.newResponse(null, 204);
|
||||
}
|
||||
case "DELETE": {
|
||||
const userHighestRole = userRoles.reduce((prev, current) =>
|
||||
|
|
@ -96,7 +95,7 @@ export default apiRoute((app) =>
|
|||
|
||||
await role.unlinkUser(user.id);
|
||||
|
||||
return response(null, 204);
|
||||
return context.newResponse(null, 204);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { apiRoute, applyConfig, auth, handleZodError } from "@/api";
|
||||
import { proxyUrl, response } from "@/response";
|
||||
import { proxyUrl } from "@/response";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
|
|
@ -103,7 +103,7 @@ export default apiRoute((app) =>
|
|||
.delete(OpenIdAccounts)
|
||||
.where(eq(OpenIdAccounts.id, account.id));
|
||||
|
||||
return response(null, 204);
|
||||
return context.newResponse(null, 204);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue