buildsec2
parent
d8d77762d9
commit
c1f10af49f
|
@ -28,7 +28,7 @@
|
||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "bunchee --tsconfig tsconfig.build.json",
|
"build": "bunchee -watch --tsconfig tsconfig.build.json",
|
||||||
"dev": "tsx watch src/index.ts",
|
"dev": "tsx watch src/index.ts",
|
||||||
"dbm": "prisma migrate dev",
|
"dbm": "prisma migrate dev",
|
||||||
"dbp": "prisma db push",
|
"dbp": "prisma db push",
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/* eslint-disable no-var */
|
/* eslint-disable no-var */
|
||||||
/* eslint-disable vars-on-top */
|
/* eslint-disable vars-on-top */
|
||||||
|
|
||||||
import type { Prisma } from "@prisma/client";
|
import type { Prisma } from "@prisma/client";
|
||||||
|
|
||||||
import { PrismaClient } from "@prisma/client";
|
import { PrismaClient } from "@prisma/client";
|
||||||
|
|
|
@ -34,8 +34,8 @@ export const authApi = app
|
||||||
.catch((error) => c.json(createErrorResult("服务器错误", error), 500));
|
.catch((error) => c.json(createErrorResult("服务器错误", error), 500));
|
||||||
})
|
})
|
||||||
.get("/profile", async (c) => {
|
.get("/profile", async (c) => {
|
||||||
console.log("profile");
|
|
||||||
const Authorization = c.req.header("authorization");
|
const Authorization = c.req.header("authorization");
|
||||||
|
|
||||||
const token = Authorization?.split(" ")[1];
|
const token = Authorization?.split(" ")[1];
|
||||||
if (!token) return c.json({ result: false, data: null, code: 1 }, 200);
|
if (!token) return c.json({ result: false, data: null, code: 1 }, 200);
|
||||||
const users = jwt.decode(token) as any;
|
const users = jwt.decode(token) as any;
|
||||||
|
@ -52,7 +52,6 @@ export const authApi = app
|
||||||
updatedAt: true,
|
updatedAt: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
console.log("user-api", user);
|
|
||||||
|
|
||||||
if (isNil(user)) return c.json({ result: false, data: null, code: 3 }, 200);
|
if (isNil(user)) return c.json({ result: false, data: null, code: 3 }, 200);
|
||||||
return c.json({ result: true, data: user, code: 4 }, 200);
|
return c.json({ result: true, data: user, code: 4 }, 200);
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import type { User } from "@prisma/client";
|
import type { User } from "@prisma/client";
|
||||||
|
|
||||||
|
import { isNil, omit } from "lodash";
|
||||||
|
|
||||||
import db from "@/libs/db/prismaClient";
|
import db from "@/libs/db/prismaClient";
|
||||||
import { hashPassword, verifyPassword } from "@/libs/password";
|
import { hashPassword, verifyPassword } from "@/libs/password";
|
||||||
import { isNil, omit } from "lodash";
|
|
||||||
|
|
||||||
import type { createUserType } from "./type";
|
import type { createUserType } from "./type";
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { getCookie } from "hono/cookie";
|
||||||
import { cors } from "hono/cors";
|
import { cors } from "hono/cors";
|
||||||
|
|
||||||
import { authApi, categoryApi, postApi, tagApi } from ".";
|
import { authApi, categoryApi, postApi, tagApi } from ".";
|
||||||
|
@ -11,6 +12,11 @@ const routes = app
|
||||||
.route("/auth", authApi)
|
.route("/auth", authApi)
|
||||||
.route("/categories", categoryApi);
|
.route("/categories", categoryApi);
|
||||||
app.use("*", cors());
|
app.use("*", cors());
|
||||||
|
app.use("*", async (c, next) => {
|
||||||
|
const allCookies = getCookie(c);
|
||||||
|
console.log(allCookies, "cookies111");
|
||||||
|
await next();
|
||||||
|
});
|
||||||
|
|
||||||
type AppType = typeof routes;
|
type AppType = typeof routes;
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ export default async function TagPage() {
|
||||||
const res = await fetchApi((c) => c.api.tags.$get());
|
const res = await fetchApi((c) => c.api.tags.$get());
|
||||||
if (!res.ok) return <div>Failed to load tags</div>;
|
if (!res.ok) return <div>Failed to load tags</div>;
|
||||||
const tags = await res.json();
|
const tags = await res.json();
|
||||||
console.log(tags);
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<ScrollArea>
|
<ScrollArea>
|
||||||
|
|
|
@ -45,13 +45,11 @@ export function LoginForm({
|
||||||
const { token } = await loginApi(data);
|
const { token } = await loginApi(data);
|
||||||
setAccessToken(token);
|
setAccessToken(token);
|
||||||
const user = await getUser();
|
const user = await getUser();
|
||||||
console.log(user);
|
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
setauth(user);
|
setauth(user);
|
||||||
}
|
}
|
||||||
router.push("/");
|
router.push("/");
|
||||||
console.log(token);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
|
||||||
|
|
|
@ -40,8 +40,6 @@ export function RegisterForm({
|
||||||
setAccessToken(token);
|
setAccessToken(token);
|
||||||
const user = await getUser();
|
const user = await getUser();
|
||||||
if (user) {
|
if (user) {
|
||||||
console.log("user", user);
|
|
||||||
|
|
||||||
setauth(user);
|
setauth(user);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,7 +10,6 @@ export const userFetchApi = async (data: createUserType) => {
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
throw new Error("Failed to create user");
|
throw new Error("Failed to create user");
|
||||||
} else {
|
} else {
|
||||||
console.log("success");
|
|
||||||
const token = await result.json();
|
const token = await result.json();
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
@ -31,6 +30,6 @@ export const getUser = async () => {
|
||||||
const result = await fetchApi(async (c) => c.api.auth.profile.$get());
|
const result = await fetchApi(async (c) => c.api.auth.profile.$get());
|
||||||
|
|
||||||
const user = await result.json();
|
const user = await result.json();
|
||||||
console.log(user, "getUser");
|
|
||||||
return user.result ? user.data : null;
|
return user.result ? user.data : null;
|
||||||
};
|
};
|
||||||
|
|
|
@ -33,7 +33,6 @@ export const CategoryListComponent = ({ items }: { items: CategoryList }) => {
|
||||||
parent: "",
|
parent: "",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
console.log(items);
|
|
||||||
|
|
||||||
const categoryHandler = async (data: CreateCategoryParams) => {
|
const categoryHandler = async (data: CreateCategoryParams) => {
|
||||||
if (data.parent === "/") {
|
if (data.parent === "/") {
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import { app } from "@repo/api";
|
import { app } from "@repo/api";
|
||||||
|
|
||||||
export const GET = async (req: Request) => app.fetch(req);
|
const refetch = async (req: Request) => {
|
||||||
export const POST = async (req: Request) => app.fetch(req);
|
return app.fetch(req);
|
||||||
export const PUT = async (req: Request) => app.fetch(req);
|
};
|
||||||
export const PATCH = async (req: Request) => app.fetch(req);
|
export const GET = refetch;
|
||||||
export const DELETE = async (req: Request) => app.fetch(req);
|
export const POST = refetch;
|
||||||
export const OPTIONS = async (req: Request) => app.fetch(req);
|
export const PUT = refetch;
|
||||||
export const HEAD = async (req: Request) => app.fetch(req);
|
export const PATCH = refetch;
|
||||||
|
export const DELETE = refetch;
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
|
"use server";
|
||||||
import { createClient } from "@repo/api";
|
import { createClient } from "@repo/api";
|
||||||
import { isNil } from "lodash";
|
import { isNil } from "lodash";
|
||||||
|
import { cookies } from "next/headers";
|
||||||
|
|
||||||
import { getCookie } from "./cookies";
|
const client = createClient({
|
||||||
import { ACCESS_TOKEN_COOKIE_NAME } from "./token";
|
|
||||||
|
|
||||||
export const client = createClient({
|
|
||||||
headers: async () => {
|
headers: async () => {
|
||||||
const token = await getCookie(ACCESS_TOKEN_COOKIE_NAME);
|
const token = (await cookies()).get("auth_token")?.value;
|
||||||
|
|
||||||
return !isNil(token) && token.length > 0
|
return !isNil(token) && token.length > 0
|
||||||
? { Authorization: `Bearer ${token}` }
|
? { Authorization: `Bearer ${token}` }
|
||||||
: { Authorization: "" };
|
: { Authorization: "" };
|
||||||
|
|
|
@ -21,8 +21,10 @@ export const getCookie = async (
|
||||||
) => {
|
) => {
|
||||||
if (typeof window === "undefined") {
|
if (typeof window === "undefined") {
|
||||||
const { cookies } = await import("next/headers");
|
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);
|
return getClientCookie(name, option);
|
||||||
};
|
};
|
||||||
export const hasCookie = async (
|
export const hasCookie = async (
|
||||||
|
|
|
@ -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();
|
|
|
@ -1,10 +1,10 @@
|
||||||
|
"use client";
|
||||||
import type { SerializeOptions as CookieSerializeOptions } from "cookie";
|
import type { SerializeOptions as CookieSerializeOptions } from "cookie";
|
||||||
|
|
||||||
|
import { setCookie } from "cookies-next";
|
||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import { isNil, omit } from "lodash";
|
import { isNil, omit } from "lodash";
|
||||||
|
|
||||||
import { setCookie } from "./cookies";
|
|
||||||
|
|
||||||
type AccessTokenCookieOptions = Pick<
|
type AccessTokenCookieOptions = Pick<
|
||||||
CookieSerializeOptions,
|
CookieSerializeOptions,
|
||||||
| "domain"
|
| "domain"
|
||||||
|
@ -47,7 +47,7 @@ const getAccessTokenOptions = (token: string): AccessTokenCookieOptions => {
|
||||||
name: ACCESS_TOKEN_COOKIE_NAME,
|
name: ACCESS_TOKEN_COOKIE_NAME,
|
||||||
value: token,
|
value: token,
|
||||||
maxAge,
|
maxAge,
|
||||||
secure: process.env.NODE_ENV === "production",
|
secure: false,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
path: "/",
|
path: "/",
|
||||||
};
|
};
|
||||||
|
@ -57,9 +57,9 @@ const getAccessTokenOptions = (token: string): AccessTokenCookieOptions => {
|
||||||
* 在cookies设置access token
|
* 在cookies设置access token
|
||||||
* @param token
|
* @param token
|
||||||
*/
|
*/
|
||||||
const setAccessToken = async (token: string) => {
|
const setAccessToken = (token: string) => {
|
||||||
const options = getAccessTokenOptions(token);
|
const options = getAccessTokenOptions(token);
|
||||||
await setCookie(
|
setCookie(
|
||||||
options.name,
|
options.name,
|
||||||
token,
|
token,
|
||||||
omit(getAccessTokenOptions(token), ["name", "value"]),
|
omit(getAccessTokenOptions(token), ["name", "value"]),
|
||||||
|
@ -72,7 +72,6 @@ const setAccessToken = async (token: string) => {
|
||||||
*/
|
*/
|
||||||
const getAccessTokenFromHeader = (req: any): string | null => {
|
const getAccessTokenFromHeader = (req: any): string | null => {
|
||||||
const authHeader = req.headers.get?.("authorization");
|
const authHeader = req.headers.get?.("authorization");
|
||||||
console.log(authHeader, "req.headers.get");
|
|
||||||
if (authHeader?.startsWith("Bearer ")) {
|
if (authHeader?.startsWith("Bearer ")) {
|
||||||
return authHeader.substring(7);
|
return authHeader.substring(7);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
|
||||||
|
GET http://0.0.0.0:3001/api/auth/profile
|
||||||
|
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjBlNWQyYTQ4LWM0ODAtNDRhNS1hMjJiLTJlMzIwMWQ0N2U1ZSIsInVzZXJuYW1lIjoiYWRtaW4iLCJyb2xlIjoiVVNFUiIsImV4cCI6MjE4MDUyODQwMiwiaWF0IjoxNzQ4NTI4NDAyfQ.Y0KWjDnQ1FN30jTPg0VD7pFdqj1yB0ZfK99qOZP0o8Y
|
Loading…
Reference in New Issue