♻️ Use synchronous id generation (#645)

This commit is contained in:
Luke Vella 2023-04-03 18:46:25 +01:00 committed by GitHub
parent 27bbe6f947
commit ffa7007184
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 12 additions and 27 deletions

View file

@ -1,13 +0,0 @@
import { customAlphabet } from "nanoid/async";
export const nanoid = customAlphabet(
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
12,
);
export const randomid = customAlphabet(
"0123456789abcdefghijklmnopqrstuvwxyz",
12,
);
export const generateOtp = customAlphabet("0123456789", 6);

View file

@ -6,7 +6,7 @@ type UserSessionData = { id: string; isGuest: boolean };
declare module "iron-session" { declare module "iron-session" {
export interface IronSessionData { export interface IronSessionData {
user: UserSessionData; user?: UserSessionData;
} }
} }

View file

@ -17,7 +17,7 @@ export async function createContext(
isGuest: true, isGuest: true,
}; };
opts.req.session.user = user; opts.req.session.user = user;
await opts.req.session.save(); opts.req.session.save();
} }
return { user, session: opts.req.session }; return { user, session: opts.req.session };
} }

View file

@ -53,8 +53,8 @@ export const polls = router({
) )
.mutation( .mutation(
async ({ ctx, input }): Promise<{ id: string; urlId: string }> => { async ({ ctx, input }): Promise<{ id: string; urlId: string }> => {
const adminUrlId = await nanoid(); const adminUrlId = nanoid();
const participantUrlId = await nanoid(); const participantUrlId = nanoid();
let email = input.user?.email; let email = input.user?.email;
let name = input.user?.name; let name = input.user?.name;
@ -83,7 +83,7 @@ export const polls = router({
title: true, title: true,
}, },
data: { data: {
id: await nanoid(), id: nanoid(),
title: input.title, title: input.title,
timeZone: input.timeZone, timeZone: input.timeZone,
location: input.location, location: input.location,

View file

@ -33,14 +33,12 @@ export const comments = router({
}), }),
) )
.mutation(async ({ ctx, input: { pollId, authorName, content } }) => { .mutation(async ({ ctx, input: { pollId, authorName, content } }) => {
const user = ctx.session.user;
const newComment = await prisma.comment.create({ const newComment = await prisma.comment.create({
data: { data: {
content, content,
pollId, pollId,
authorName, authorName,
userId: user.id, userId: ctx.user.id,
}, },
select: { select: {
id: true, id: true,

View file

@ -27,7 +27,7 @@ const optionValues = ["2022-12-14", "2022-12-15", "2022-12-16", "2022-12-17"];
export const demo = router({ export const demo = router({
create: possiblyPublicProcedure.mutation(async () => { create: possiblyPublicProcedure.mutation(async () => {
const adminUrlId = await nanoid(); const adminUrlId = nanoid();
const demoUser = { name: "John Example", email: "noreply@rallly.co" }; const demoUser = { name: "John Example", email: "noreply@rallly.co" };
const options: Array<{ start: Date; id: string }> = []; const options: Array<{ start: Date; id: string }> = [];
@ -72,13 +72,13 @@ export const demo = router({
await prisma.poll.create({ await prisma.poll.create({
data: { data: {
id: await nanoid(), id: nanoid(),
title: "Lunch Meeting", title: "Lunch Meeting",
location: "Starbucks, 901 New York Avenue", location: "Starbucks, 901 New York Avenue",
description: `Hey everyone, please choose the dates when you are available to meet for our monthly get together. Looking forward to see you all!`, description: `Hey everyone, please choose the dates when you are available to meet for our monthly get together. Looking forward to see you all!`,
demo: true, demo: true,
adminUrlId, adminUrlId,
participantUrlId: await nanoid(), participantUrlId: nanoid(),
user: { user: {
connectOrCreate: { connectOrCreate: {
where: { where: {

View file

@ -61,7 +61,7 @@ export const participants = router({
}), }),
) )
.mutation(async ({ ctx, input: { pollId, votes, name, email } }) => { .mutation(async ({ ctx, input: { pollId, votes, name, email } }) => {
const user = ctx.session.user; const { user } = ctx;
const poll = await prisma.poll.findUnique({ const poll = await prisma.poll.findUnique({
where: { id: pollId }, where: { id: pollId },

View file

@ -17,7 +17,7 @@ export const publicProcedure = t.procedure;
export const middleware = t.middleware; export const middleware = t.middleware;
const checkAuthIfRequired = middleware(async ({ ctx, next }) => { const checkAuthIfRequired = middleware(async ({ ctx, next }) => {
if (process.env.AUTH_REQUIRED === "true" && ctx.session.user.isGuest) { if (process.env.AUTH_REQUIRED === "true" && ctx.user.isGuest) {
throw new TRPCError({ code: "UNAUTHORIZED", message: "Login is required" }); throw new TRPCError({ code: "UNAUTHORIZED", message: "Login is required" });
} }
return next(); return next();

View file

@ -1,4 +1,4 @@
import { customAlphabet } from "nanoid/async"; import { customAlphabet } from "nanoid";
export const nanoid = customAlphabet( export const nanoid = customAlphabet(
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",