Maj
Maj2y ago

First time learning trpc really good tool. help with setting cookies on front end.

Hello how do you use tRPC to make a cookie and send it to the front end. or is there any other method to make some auth stuff with the next auth.
2 Replies
Maj
Maj2y ago
export const userRouter = createRouter().mutation("login", {
input: z.object({
email: z.string(),
password: z.string(),
}),

async resolve({ ctx , input}) {
// @ts-ignore

const user = await ctx.prisma.user.findUniqueOrThrow({
where: {
email: input.email,
}
})
if (!user) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'User not found'
})
}

const isMatch = await bcrypt.compare(input.password, user.password);
if (!isMatch) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'Incorrect Credentials'
})
}
return {
user: user,
message: "Login Successful"
};

}
})
export const userRouter = createRouter().mutation("login", {
input: z.object({
email: z.string(),
password: z.string(),
}),

async resolve({ ctx , input}) {
// @ts-ignore

const user = await ctx.prisma.user.findUniqueOrThrow({
where: {
email: input.email,
}
})
if (!user) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'User not found'
})
}

const isMatch = await bcrypt.compare(input.password, user.password);
if (!isMatch) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'Incorrect Credentials'
})
}
return {
user: user,
message: "Login Successful"
};

}
})
this is my login router which i want to use to login through and i want to return a cookie and send some data back to the client.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View