4b47
4b47
TTCTheo's Typesafe Cult
Created by 4b47 on 2/9/2025 in #questions
Fetching data after Vercel deploy
Hey guys i got a question. I'm just learning to build fullstack app with next.js drizzle and neon. I configured it and everything works fine locally, but after vercel deploy, with same database url connection , it seems to be only fetching on the build time, and when i add records after that it doesnt fetch before redeploy. Why is that happening? I'm using newest version of next. And i just fetch on the page component with simple
const offerList: Offer[] = await database.select().from(offers);
const offerList: Offer[] = await database.select().from(offers);
Thanks for help!
4 replies
TTCTheo's Typesafe Cult
Created by 4b47 on 1/19/2023 in #questions
Login in with credentials with next auth
Hello! I'm trying to learn how to use next auth with credentials. So i got this next auth settings:
export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
type: "credentials",
credentials: {},
async authorize(credentials, req) {
const { email, password } = credentials as {
email: string;
password: string;
};
// perform you login logic
// find out user from db
const user = await prisma.user.findFirst({
where: {
email: email,
password: password,
},
});
if (!user) {
throw new Error("bad user");
}
return user;
},
}),
],
};

export default NextAuth(authOptions);
export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
type: "credentials",
credentials: {},
async authorize(credentials, req) {
const { email, password } = credentials as {
email: string;
password: string;
};
// perform you login logic
// find out user from db
const user = await prisma.user.findFirst({
where: {
email: email,
password: password,
},
});
if (!user) {
throw new Error("bad user");
}
return user;
},
}),
],
};

export default NextAuth(authOptions);
And i'm using this function to login
const handleSubmit = () => {
try {
signIn("credentials", {
email: emailValue,
password: passwordValue,
redirect: false,
});
} catch (error) {
console.log(error);
}
};
const handleSubmit = () => {
try {
signIn("credentials", {
email: emailValue,
password: passwordValue,
redirect: false,
});
} catch (error) {
console.log(error);
}
};
, signIn is imported from next-auth. And everything seems to be okay, but that authentication is not persisting. I mean, when i login and go to index file and again to login page, I'm again unauthenticated. Whats the problem?
7 replies
TTCTheo's Typesafe Cult
Created by 4b47 on 1/18/2023 in #questions
Error function trpc
Okay, so im kinda new to trpc stuff and not that familiar with that. I want to throw error on backend when Email is not unique. So when i write it like this in my router it works great, it throws that error:
registerUser: publicProcedure
.input(z.object({ email: z.string(), password: z.string() }))
.mutation(async ({ input, ctx }) => {
const user = await ctx.prisma.user.findUnique({
where: {
email: input.email,
},
});
if (user) {
throw new TRPCError({
code: "CONFLICT",
message: "Email is already used.",
});
}
const newlyCreatedUser = await ctx.prisma.user.create({
data: input,
});
return newlyCreatedUser;
}),
});
registerUser: publicProcedure
.input(z.object({ email: z.string(), password: z.string() }))
.mutation(async ({ input, ctx }) => {
const user = await ctx.prisma.user.findUnique({
where: {
email: input.email,
},
});
if (user) {
throw new TRPCError({
code: "CONFLICT",
message: "Email is already used.",
});
}
const newlyCreatedUser = await ctx.prisma.user.create({
data: input,
});
return newlyCreatedUser;
}),
});
but wanted to do it in more elegant way, and use util function like this
import { TRPCError } from "@trpc/server";

export const isUniqueEmail = async (ctx: any, passedEmail: string) => {
const email = await ctx.prisma.user.findUnique({
where: {
email: passedEmail,
},
});
if (email) {
throw new TRPCError({ code: "CONFLICT" });
}
};
import { TRPCError } from "@trpc/server";

export const isUniqueEmail = async (ctx: any, passedEmail: string) => {
const email = await ctx.prisma.user.findUnique({
where: {
email: passedEmail,
},
});
if (email) {
throw new TRPCError({ code: "CONFLICT" });
}
};
and
export const userRouter = createTRPCRouter({
registerUser: publicProcedure
.input(z.object({ email: z.string(), password: z.string() }))
.mutation(async ({ input, ctx }) => {
isUniqueEmail(ctx, input.email)
const newlyCreatedUser = await ctx.prisma.user.create({
data: input,
});
return newlyCreatedUser;
}),
});
export const userRouter = createTRPCRouter({
registerUser: publicProcedure
.input(z.object({ email: z.string(), password: z.string() }))
.mutation(async ({ input, ctx }) => {
isUniqueEmail(ctx, input.email)
const newlyCreatedUser = await ctx.prisma.user.create({
data: input,
});
return newlyCreatedUser;
}),
});
, but then i just got error i assume from that unique field in db and got 500, not my desired error which i throw in util function. What is wrong? also, dont really know how to type ctx in util function. Thanks
2 replies
TTCTheo's Typesafe Cult
Created by 4b47 on 1/13/2023 in #questions
Help with new trpc syntax
Hello! Can anyone help me rewrite this example piece of code to new trpc syntax with Public procedures, so I will know how to do it? Thanks!:)
export const trainingRouter = createRouter()
.mutation('createTraining', {
input: z.object({
trainingName: z.string(),
}),
async resolve({ input, ctx }) {
const newlyCreatedTraining = await ctx.prisma.training.create({
data: input,
})
return newlyCreatedTraining.id
},
})
export const trainingRouter = createRouter()
.mutation('createTraining', {
input: z.object({
trainingName: z.string(),
}),
async resolve({ input, ctx }) {
const newlyCreatedTraining = await ctx.prisma.training.create({
data: input,
})
return newlyCreatedTraining.id
},
})
2 replies