way better auth middleware, enroll team server fn and pb fn
This commit is contained in:
@@ -1,28 +1,29 @@
|
||||
import { fetchSuperTokensAuth, setUserMetadata, superTokensFunctionMiddleware, superTokensRoleFunctionMiddleware } from "@/utils/supertokens";
|
||||
import { setUserMetadata, superTokensFunctionMiddleware } from "@/utils/supertokens";
|
||||
import { createServerFn } from "@tanstack/react-start";
|
||||
import { playerInputSchema, playerUpdateSchema } from "@/features/players/types";
|
||||
import { pbAdmin } from "@/lib/pocketbase/client";
|
||||
import { z } from "zod";
|
||||
import { logger } from ".";
|
||||
|
||||
export const fetchMe = createServerFn().handler(async () => {
|
||||
const data = await fetchSuperTokensAuth();
|
||||
if (!data || !data.userAuthId) return { user: undefined, roles: [], metadata: {} };
|
||||
export const fetchMe = createServerFn()
|
||||
.middleware([superTokensFunctionMiddleware])
|
||||
.handler(async ({ context }) => {
|
||||
if (!context || !context.userAuthId) return { user: undefined, roles: [], metadata: {} };
|
||||
|
||||
try {
|
||||
await pbAdmin.authPromise;
|
||||
const result = await pbAdmin.getPlayerByAuthId(data.userAuthId);
|
||||
logger.info('Fetched player', result);
|
||||
return {
|
||||
user: result || undefined,
|
||||
roles: data.roles,
|
||||
metadata: data.metadata
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Error fetching player:', error);
|
||||
return { user: undefined, roles: data.roles, metadata: data.metadata };
|
||||
}
|
||||
});
|
||||
try {
|
||||
await pbAdmin.authPromise;
|
||||
const result = await pbAdmin.getPlayerByAuthId(context.userAuthId);
|
||||
logger.info('Fetched player', result);
|
||||
return {
|
||||
user: result || undefined,
|
||||
roles: context.roles,
|
||||
metadata: context.metadata
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Error fetching player:', error);
|
||||
return { user: undefined, roles: context.roles, metadata: context.metadata };
|
||||
}
|
||||
});
|
||||
|
||||
export const getPlayer = createServerFn()
|
||||
.validator(z.string())
|
||||
|
||||
30
src/features/tournaments/hooks/use-enroll-team.ts
Normal file
30
src/features/tournaments/hooks/use-enroll-team.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { enrollTeam } from "@/features/tournaments/server";
|
||||
import toast from '@/lib/sonner';
|
||||
|
||||
const useEnrollTeam = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (data: { tournamentId: string, teamId: string }) => {
|
||||
return enrollTeam({ data });
|
||||
},
|
||||
onSuccess: (data, { tournamentId }) => {
|
||||
if (!data) {
|
||||
toast.error('There was an issue enrolling. Please try again later.');
|
||||
} else {
|
||||
queryClient.invalidateQueries({ queryKey: ['tournaments', 'detail', tournamentId] });
|
||||
toast.success('Team enrolled successfully!');
|
||||
}
|
||||
},
|
||||
onError: (error: any) => {
|
||||
if (error.message) {
|
||||
toast.error(error.message);
|
||||
} else {
|
||||
toast.error('An unexpected error occurred when trying to enroll the team. Please try again later.');
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export default useEnrollTeam;
|
||||
@@ -3,7 +3,7 @@ import { getTournament, listTournaments } from "./server";
|
||||
|
||||
const tournamentKeys = {
|
||||
list: ['tournaments', 'list'] as const,
|
||||
details: (id: string) => ['tournaments', 'details', id] as const,
|
||||
details: (id: string) => ['tournaments', 'details', id] as const
|
||||
};
|
||||
|
||||
export const tournamentQueries = {
|
||||
|
||||
@@ -41,3 +41,32 @@ export const getTournament = createServerFn()
|
||||
const tournament = await pbAdmin.getTournament(tournamentId);
|
||||
return tournament;
|
||||
});
|
||||
|
||||
export const enrollTeam = createServerFn()
|
||||
.validator(z.object({
|
||||
tournamentId: z.string(),
|
||||
teamId: z.string()
|
||||
}))
|
||||
.middleware([superTokensFunctionMiddleware])
|
||||
.handler(async ({ data: { tournamentId, teamId }, context }) => {
|
||||
try {
|
||||
const userId = context.userAuthId;
|
||||
const isAdmin = context.roles.includes("Admin");
|
||||
|
||||
const team = await pbAdmin.getTeam(teamId);
|
||||
if (!team) { throw new Error('Team not found'); }
|
||||
|
||||
const isPlayerOnTeam = team.players?.some(player => player.id === userId);
|
||||
|
||||
if (!isPlayerOnTeam && !isAdmin) {
|
||||
throw new Error('You do not have permission to enroll this team');
|
||||
}
|
||||
|
||||
logger.info('Enrolling team in tournament', { tournamentId, teamId, userId });
|
||||
const tournament = await pbAdmin.enrollTeam(tournamentId, teamId);
|
||||
return tournament;
|
||||
} catch (error) {
|
||||
logger.error('Error enrolling team', error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user