mirror of
https://github.com/ajnart/homarr.git
synced 2026-02-07 07:09:21 +01:00
✨ Add procedure for registration tokens management
This commit is contained in:
@@ -15,6 +15,7 @@ import { rssRouter } from './routers/rss';
|
||||
import { usenetRouter } from './routers/usenet/router';
|
||||
import { userRouter } from './routers/user';
|
||||
import { weatherRouter } from './routers/weather';
|
||||
import { inviteRouter } from './routers/registrationTokens';
|
||||
|
||||
/**
|
||||
* This is the primary router for your server.
|
||||
@@ -37,6 +38,7 @@ export const rootRouter = createTRPCRouter({
|
||||
usenet: usenetRouter,
|
||||
calendar: calendarRouter,
|
||||
weather: weatherRouter,
|
||||
registrationTokens: inviteRouter
|
||||
});
|
||||
|
||||
// export type definition of API
|
||||
|
||||
52
src/server/api/routers/registrationTokens.ts
Normal file
52
src/server/api/routers/registrationTokens.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import dayjs from 'dayjs';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { createTRPCRouter, publicProcedure } from '../trpc';
|
||||
import { randomBytes } from 'crypto';
|
||||
|
||||
export const inviteRouter = createTRPCRouter({
|
||||
getAllInvites: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
limit: z.number().min(1).max(100).nullish(),
|
||||
cursor: z.string().nullish(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const limit = input.limit ?? 50;
|
||||
const cursor = input.cursor;
|
||||
const registrationTokens = await ctx.prisma.registrationToken.findMany({
|
||||
take: limit + 1, // get an extra item at the end which we'll use as next cursor
|
||||
cursor: cursor ? { id: cursor } : undefined,
|
||||
});
|
||||
|
||||
let nextCursor: typeof cursor | undefined = undefined;
|
||||
if (registrationTokens.length > limit) {
|
||||
const nextItem = registrationTokens.pop();
|
||||
nextCursor = nextItem!.id;
|
||||
}
|
||||
|
||||
return {
|
||||
registrationTokens: registrationTokens.map((token) => ({
|
||||
id: token.id,
|
||||
expires: token.expires,
|
||||
})),
|
||||
nextCursor,
|
||||
};
|
||||
}),
|
||||
createRegistrationToken: publicProcedure.input(
|
||||
z.object({
|
||||
expiration: z
|
||||
.date()
|
||||
.min(dayjs().add(5, 'minutes').toDate())
|
||||
.max(dayjs().add(6, 'months').toDate()),
|
||||
})
|
||||
).mutation(async ({ ctx, input }) => {
|
||||
await ctx.prisma.registrationToken.create({
|
||||
data: {
|
||||
expires: input.expiration,
|
||||
token: randomBytes(20).toString('hex'),
|
||||
}
|
||||
});
|
||||
}),
|
||||
});
|
||||
Reference in New Issue
Block a user