session state simplification
This commit is contained in:
@@ -306,14 +306,11 @@ export const authRouter = createTRPCRouter({
|
||||
}
|
||||
}
|
||||
|
||||
const isAdmin = userId === env.ADMIN_ID;
|
||||
|
||||
const clientIP = getClientIP(getH3Event(ctx));
|
||||
const userAgent = getUserAgent(getH3Event(ctx));
|
||||
await createAuthSession(
|
||||
getH3Event(ctx),
|
||||
userId,
|
||||
isAdmin,
|
||||
true, // OAuth defaults to remember
|
||||
clientIP,
|
||||
userAgent
|
||||
@@ -521,15 +518,12 @@ export const authRouter = createTRPCRouter({
|
||||
}
|
||||
}
|
||||
|
||||
const isAdmin = userId === env.ADMIN_ID;
|
||||
|
||||
// Create session with Vinxi (OAuth defaults to remember me)
|
||||
const clientIP = getClientIP(getH3Event(ctx));
|
||||
const userAgent = getUserAgent(getH3Event(ctx));
|
||||
await createAuthSession(
|
||||
getH3Event(ctx),
|
||||
userId,
|
||||
isAdmin,
|
||||
true, // OAuth defaults to remember
|
||||
clientIP,
|
||||
userAgent
|
||||
@@ -647,7 +641,6 @@ export const authRouter = createTRPCRouter({
|
||||
}
|
||||
|
||||
const userId = (res.rows[0] as unknown as User).id;
|
||||
const isAdmin = userId === env.ADMIN_ID;
|
||||
|
||||
const clientIP = getClientIP(getH3Event(ctx));
|
||||
const userAgent = getUserAgent(getH3Event(ctx));
|
||||
@@ -655,7 +648,6 @@ export const authRouter = createTRPCRouter({
|
||||
await createAuthSession(
|
||||
getH3Event(ctx),
|
||||
userId,
|
||||
isAdmin,
|
||||
rememberMe,
|
||||
clientIP,
|
||||
userAgent
|
||||
@@ -780,7 +772,6 @@ export const authRouter = createTRPCRouter({
|
||||
}
|
||||
|
||||
const userId = (res.rows[0] as unknown as User).id;
|
||||
const isAdmin = userId === env.ADMIN_ID;
|
||||
|
||||
// Use rememberMe from JWT if not provided in input, default to false
|
||||
const shouldRemember =
|
||||
@@ -791,7 +782,6 @@ export const authRouter = createTRPCRouter({
|
||||
await createAuthSession(
|
||||
getH3Event(ctx),
|
||||
userId,
|
||||
isAdmin,
|
||||
shouldRemember,
|
||||
clientIP,
|
||||
userAgent
|
||||
@@ -983,12 +973,10 @@ export const authRouter = createTRPCRouter({
|
||||
// Create session with client info
|
||||
const clientIP = getClientIP(getH3Event(ctx));
|
||||
const userAgent = getUserAgent(getH3Event(ctx));
|
||||
const isAdmin = userId === env.ADMIN_ID;
|
||||
|
||||
await createAuthSession(
|
||||
getH3Event(ctx),
|
||||
userId,
|
||||
isAdmin,
|
||||
true, // Always use persistent sessions
|
||||
clientIP,
|
||||
userAgent
|
||||
@@ -1150,14 +1138,11 @@ export const authRouter = createTRPCRouter({
|
||||
// Reset rate limits on successful login
|
||||
await resetLoginRateLimits(email, clientIP);
|
||||
|
||||
const isAdmin = user.id === env.ADMIN_ID;
|
||||
|
||||
// Create session with Vinxi
|
||||
const userAgent = getUserAgent(getH3Event(ctx));
|
||||
await createAuthSession(
|
||||
getH3Event(ctx),
|
||||
user.id,
|
||||
isAdmin,
|
||||
rememberMe ?? false, // Default to session cookie (expires on browser close)
|
||||
clientIP,
|
||||
userAgent
|
||||
|
||||
@@ -7,9 +7,9 @@ import { CACHE_CONFIG } from "~/config";
|
||||
|
||||
const BLOG_CACHE_TTL = CACHE_CONFIG.BLOG_CACHE_TTL_MS;
|
||||
|
||||
const getAllPostsData = async (privilegeLevel: string) => {
|
||||
const getAllPostsData = async (isAdmin: boolean) => {
|
||||
return withCacheAndStale(
|
||||
`blog-posts-${privilegeLevel}`,
|
||||
`blog-posts-${isAdmin ? "admin" : "public"}`,
|
||||
BLOG_CACHE_TTL,
|
||||
async () => {
|
||||
const conn = ConnectionFactory();
|
||||
@@ -34,7 +34,7 @@ const getAllPostsData = async (privilegeLevel: string) => {
|
||||
LEFT JOIN Comment c ON p.id = c.post_id
|
||||
`;
|
||||
|
||||
if (privilegeLevel !== "admin") {
|
||||
if (!isAdmin) {
|
||||
postsQuery += ` WHERE p.published = TRUE`;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ const getAllPostsData = async (privilegeLevel: string) => {
|
||||
SELECT t.value, t.post_id
|
||||
FROM Tag t
|
||||
JOIN Post p ON t.post_id = p.id
|
||||
${privilegeLevel !== "admin" ? "WHERE p.published = TRUE" : ""}
|
||||
${!isAdmin ? "WHERE p.published = TRUE" : ""}
|
||||
ORDER BY t.value ASC
|
||||
`;
|
||||
|
||||
@@ -64,21 +64,21 @@ const getAllPostsData = async (privilegeLevel: string) => {
|
||||
tagMap[key] = (tagMap[key] || 0) + 1;
|
||||
});
|
||||
|
||||
return { posts, tags, tagMap, privilegeLevel };
|
||||
return { posts, tags, tagMap, isAdmin };
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export const blogRouter = createTRPCRouter({
|
||||
getRecentPosts: publicProcedure.query(async ({ ctx }) => {
|
||||
const allPostsData = await getAllPostsData("public");
|
||||
const allPostsData = await getAllPostsData(false);
|
||||
|
||||
return allPostsData.posts.slice(0, 3);
|
||||
}),
|
||||
|
||||
getPosts: publicProcedure.query(async ({ ctx }) => {
|
||||
const privilegeLevel = ctx.privilegeLevel;
|
||||
return getAllPostsData(privilegeLevel);
|
||||
const isAdmin = ctx.isAdmin;
|
||||
return getAllPostsData(isAdmin);
|
||||
}),
|
||||
|
||||
incrementPostRead: publicProcedure
|
||||
|
||||
@@ -144,7 +144,7 @@ export const databaseRouter = createTRPCRouter({
|
||||
commentID: input.commentID,
|
||||
deletionType: input.deletionType,
|
||||
userId: ctx.userId,
|
||||
privilegeLevel: ctx.privilegeLevel
|
||||
isAdmin: ctx.isAdmin
|
||||
});
|
||||
|
||||
const commentQuery = await conn.execute({
|
||||
@@ -161,7 +161,7 @@ export const databaseRouter = createTRPCRouter({
|
||||
}
|
||||
|
||||
const isOwner = comment.commenter_id === ctx.userId;
|
||||
const isAdmin = ctx.privilegeLevel === "admin";
|
||||
const isAdmin = ctx.isAdmin;
|
||||
|
||||
console.log("[deleteComment] Authorization check:", {
|
||||
isOwner,
|
||||
|
||||
@@ -3,7 +3,7 @@ import { env } from "~/env/server";
|
||||
|
||||
export const infillRouter = createTRPCRouter({
|
||||
getConfig: publicProcedure.query(({ ctx }) => {
|
||||
if (ctx.privilegeLevel !== "admin") {
|
||||
if (!ctx.isAdmin) {
|
||||
return { endpoint: null, token: null };
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import { getAuthSession } from "~/server/session-helpers";
|
||||
export type Context = {
|
||||
event: APIEvent;
|
||||
userId: string | null;
|
||||
privilegeLevel: "anonymous" | "user" | "admin";
|
||||
isAdmin: boolean;
|
||||
};
|
||||
|
||||
async function createContextInner(event: APIEvent): Promise<Context> {
|
||||
@@ -16,11 +16,11 @@ async function createContextInner(event: APIEvent): Promise<Context> {
|
||||
const session = await getAuthSession(event.nativeEvent);
|
||||
|
||||
let userId: string | null = null;
|
||||
let privilegeLevel: "anonymous" | "user" | "admin" = "anonymous";
|
||||
let isAdmin = false;
|
||||
|
||||
if (session && session.userId) {
|
||||
userId = session.userId;
|
||||
privilegeLevel = session.isAdmin ? "admin" : "user";
|
||||
isAdmin = session.isAdmin;
|
||||
}
|
||||
|
||||
const req = event.nativeEvent.node?.req || event.nativeEvent;
|
||||
@@ -56,7 +56,7 @@ async function createContextInner(event: APIEvent): Promise<Context> {
|
||||
return {
|
||||
event,
|
||||
userId,
|
||||
privilegeLevel
|
||||
isAdmin
|
||||
};
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ export const createTRPCRouter = t.router;
|
||||
export const publicProcedure = t.procedure;
|
||||
|
||||
const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
|
||||
if (!ctx.userId || ctx.privilegeLevel === "anonymous") {
|
||||
if (!ctx.userId) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED", message: "Not authenticated" });
|
||||
}
|
||||
return next({
|
||||
@@ -82,7 +82,7 @@ const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
|
||||
});
|
||||
|
||||
const enforceUserIsAdmin = t.middleware(({ ctx, next }) => {
|
||||
if (ctx.privilegeLevel !== "admin") {
|
||||
if (!ctx.isAdmin) {
|
||||
throw new TRPCError({
|
||||
code: "FORBIDDEN",
|
||||
message: "Admin access required"
|
||||
|
||||
Reference in New Issue
Block a user