You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

42 lines
1.2 KiB

import { getCurrentUser } from "../../service/auth";
const SESSION_COOKIE_NAME = "pp_session";
const UNAUTHORIZED_MESSAGE = "未登录或会话已失效";
export default defineWrappedResponseHandler(async (event) => {
try {
const sessionId = getCookie(event, SESSION_COOKIE_NAME);
if (!sessionId) {
deleteCookie(event, SESSION_COOKIE_NAME, {
path: "/",
});
throw createError({
statusCode: 401,
statusMessage: UNAUTHORIZED_MESSAGE,
});
}
const user = await getCurrentUser(sessionId);
if (!user) {
deleteCookie(event, SESSION_COOKIE_NAME, {
path: "/",
});
throw createError({
statusCode: 401,
statusMessage: UNAUTHORIZED_MESSAGE,
});
}
return R.success({
user,
});
} catch (err) {
if (err && typeof err === "object" && "statusCode" in err) {
throw err;
}
throw createError({
statusCode: 500,
statusMessage: "服务器繁忙,请稍后重试",
});
}
});