buildsec2

master
lee 2025-05-30 00:24:53 +08:00
parent d8d77762d9
commit c1f10af49f
16 changed files with 37 additions and 46 deletions

View File

@ -28,7 +28,7 @@
"dist"
],
"scripts": {
"build": "bunchee --tsconfig tsconfig.build.json",
"build": "bunchee -watch --tsconfig tsconfig.build.json",
"dev": "tsx watch src/index.ts",
"dbm": "prisma migrate dev",
"dbp": "prisma db push",

View File

@ -1,5 +1,6 @@
/* eslint-disable no-var */
/* eslint-disable vars-on-top */
import type { Prisma } from "@prisma/client";
import { PrismaClient } from "@prisma/client";

View File

@ -34,8 +34,8 @@ export const authApi = app
.catch((error) => c.json(createErrorResult("服务器错误", error), 500));
})
.get("/profile", async (c) => {
console.log("profile");
const Authorization = c.req.header("authorization");
const token = Authorization?.split(" ")[1];
if (!token) return c.json({ result: false, data: null, code: 1 }, 200);
const users = jwt.decode(token) as any;
@ -52,7 +52,6 @@ export const authApi = app
updatedAt: true,
},
});
console.log("user-api", user);
if (isNil(user)) return c.json({ result: false, data: null, code: 3 }, 200);
return c.json({ result: true, data: user, code: 4 }, 200);

View File

@ -1,8 +1,9 @@
import type { User } from "@prisma/client";
import { isNil, omit } from "lodash";
import db from "@/libs/db/prismaClient";
import { hashPassword, verifyPassword } from "@/libs/password";
import { isNil, omit } from "lodash";
import type { createUserType } from "./type";

View File

@ -1,3 +1,4 @@
import { getCookie } from "hono/cookie";
import { cors } from "hono/cors";
import { authApi, categoryApi, postApi, tagApi } from ".";
@ -11,6 +12,11 @@ const routes = app
.route("/auth", authApi)
.route("/categories", categoryApi);
app.use("*", cors());
app.use("*", async (c, next) => {
const allCookies = getCookie(c);
console.log(allCookies, "cookies111");
await next();
});
type AppType = typeof routes;

View File

@ -7,7 +7,7 @@ export default async function TagPage() {
const res = await fetchApi((c) => c.api.tags.$get());
if (!res.ok) return <div>Failed to load tags</div>;
const tags = await res.json();
console.log(tags);
return (
<div>
<ScrollArea>

View File

@ -45,13 +45,11 @@ export function LoginForm({
const { token } = await loginApi(data);
setAccessToken(token);
const user = await getUser();
console.log(user);
if (user) {
setauth(user);
}
router.push("/");
console.log(token);
} catch (err) {
console.log(err);

View File

@ -40,8 +40,6 @@ export function RegisterForm({
setAccessToken(token);
const user = await getUser();
if (user) {
console.log("user", user);
setauth(user);
}
};

View File

@ -10,7 +10,6 @@ export const userFetchApi = async (data: createUserType) => {
if (!result.ok) {
throw new Error("Failed to create user");
} else {
console.log("success");
const token = await result.json();
return token;
}
@ -31,6 +30,6 @@ export const getUser = async () => {
const result = await fetchApi(async (c) => c.api.auth.profile.$get());
const user = await result.json();
console.log(user, "getUser");
return user.result ? user.data : null;
};

View File

@ -33,7 +33,6 @@ export const CategoryListComponent = ({ items }: { items: CategoryList }) => {
parent: "",
},
});
console.log(items);
const categoryHandler = async (data: CreateCategoryParams) => {
if (data.parent === "/") {

View File

@ -1,9 +1,10 @@
import { app } from "@repo/api";
export const GET = async (req: Request) => app.fetch(req);
export const POST = async (req: Request) => app.fetch(req);
export const PUT = async (req: Request) => app.fetch(req);
export const PATCH = async (req: Request) => app.fetch(req);
export const DELETE = async (req: Request) => app.fetch(req);
export const OPTIONS = async (req: Request) => app.fetch(req);
export const HEAD = async (req: Request) => app.fetch(req);
const refetch = async (req: Request) => {
return app.fetch(req);
};
export const GET = refetch;
export const POST = refetch;
export const PUT = refetch;
export const PATCH = refetch;
export const DELETE = refetch;

View File

@ -1,12 +1,12 @@
"use server";
import { createClient } from "@repo/api";
import { isNil } from "lodash";
import { cookies } from "next/headers";
import { getCookie } from "./cookies";
import { ACCESS_TOKEN_COOKIE_NAME } from "./token";
export const client = createClient({
const client = createClient({
headers: async () => {
const token = await getCookie(ACCESS_TOKEN_COOKIE_NAME);
const token = (await cookies()).get("auth_token")?.value;
return !isNil(token) && token.length > 0
? { Authorization: `Bearer ${token}` }
: { Authorization: "" };

View File

@ -21,8 +21,10 @@ export const getCookie = async (
) => {
if (typeof window === "undefined") {
const { cookies } = await import("next/headers");
return (await cookies()).get(name)?.value;
const token = (await cookies()).get(name)?.value;
return token;
}
return getClientCookie(name, option);
};
export const hasCookie = async (

View File

@ -1,15 +0,0 @@
type EventNames = "API:UN_AUTH" | "API:VALIDATE_ERROR";
type EventListener = (...args: any[]) => void;
class EventEmitter {
private listeners: Record<EventNames, Set<EventListener>> = {
"API:UN_AUTH": new Set(),
"API:VALIDATE_ERROR": new Set(),
};
on(EventNames: EventNames, listener: EventListener) {
this.listeners[EventNames].add(listener);
}
emit(EventNames: EventNames, ...args: any[]) {
this.listeners[EventNames].forEach((listener) => listener(...args));
}
}
export default new EventEmitter();

View File

@ -1,10 +1,10 @@
"use client";
import type { SerializeOptions as CookieSerializeOptions } from "cookie";
import { setCookie } from "cookies-next";
import jwt from "jsonwebtoken";
import { isNil, omit } from "lodash";
import { setCookie } from "./cookies";
type AccessTokenCookieOptions = Pick<
CookieSerializeOptions,
| "domain"
@ -47,7 +47,7 @@ const getAccessTokenOptions = (token: string): AccessTokenCookieOptions => {
name: ACCESS_TOKEN_COOKIE_NAME,
value: token,
maxAge,
secure: process.env.NODE_ENV === "production",
secure: false,
sameSite: "lax",
path: "/",
};
@ -57,9 +57,9 @@ const getAccessTokenOptions = (token: string): AccessTokenCookieOptions => {
* cookiesaccess token
* @param token
*/
const setAccessToken = async (token: string) => {
const setAccessToken = (token: string) => {
const options = getAccessTokenOptions(token);
await setCookie(
setCookie(
options.name,
token,
omit(getAccessTokenOptions(token), ["name", "value"]),
@ -72,7 +72,6 @@ const setAccessToken = async (token: string) => {
*/
const getAccessTokenFromHeader = (req: any): string | null => {
const authHeader = req.headers.get?.("authorization");
console.log(authHeader, "req.headers.get");
if (authHeader?.startsWith("Bearer ")) {
return authHeader.substring(7);
}

3
web.http 100644
View File

@ -0,0 +1,3 @@
GET http://0.0.0.0:3001/api/auth/profile
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjBlNWQyYTQ4LWM0ODAtNDRhNS1hMjJiLTJlMzIwMWQ0N2U1ZSIsInVzZXJuYW1lIjoiYWRtaW4iLCJyb2xlIjoiVVNFUiIsImV4cCI6MjE4MDUyODQwMiwiaWF0IjoxNzQ4NTI4NDAyfQ.Y0KWjDnQ1FN30jTPg0VD7pFdqj1yB0ZfK99qOZP0o8Y