// This query is called in multiple places but will only run once and return data to all of them
const getRedirectOrUserQuery = query(async () => {
"use server";
const session = await getSession();
const userId = session.data.userId;
if (userId === undefined) throw redirect("/login");
const user = await db.user.findUnique({ where: { id: userId } });
if (!user) throw redirect("/register");
return { id: user.id, username: user.username };
}, "get-redirect-or-user");
// Same logic, but as a server function instead of a query
const getRedirectOrUser = async () => {
"use server";
const session = await getSession();
const userId = session.data.userId;
if (userId === undefined) throw redirect("/login");
const user = await db.user.findUnique({ where: { id: userId } });
if (!user) throw redirect("/register");
return { id: user.id, username: user.username };
};
// Main query used in createAsync
export const getUser = query(async () => {
"use server";
const user = await getRedirectOrUserQuery();
return user;
}, "user");
// This query is called in multiple places but will only run once and return data to all of them
const getRedirectOrUserQuery = query(async () => {
"use server";
const session = await getSession();
const userId = session.data.userId;
if (userId === undefined) throw redirect("/login");
const user = await db.user.findUnique({ where: { id: userId } });
if (!user) throw redirect("/register");
return { id: user.id, username: user.username };
}, "get-redirect-or-user");
// Same logic, but as a server function instead of a query
const getRedirectOrUser = async () => {
"use server";
const session = await getSession();
const userId = session.data.userId;
if (userId === undefined) throw redirect("/login");
const user = await db.user.findUnique({ where: { id: userId } });
if (!user) throw redirect("/register");
return { id: user.id, username: user.username };
};
// Main query used in createAsync
export const getUser = query(async () => {
"use server";
const user = await getRedirectOrUserQuery();
return user;
}, "user");