Update password functionality and error handling

This commit is contained in:
Douglas Barone 2023-12-15 16:02:47 -04:00
parent 68273802cc
commit 21b800f431
2 changed files with 31 additions and 13 deletions

View File

@ -37,7 +37,7 @@ export async function updatePassword({
username: string
password: string
newPassword: string
}) {
}): Promise<'SUCCESS' | 'FAIL'> {
try {
const userDN = await getUserDN(username)
await ldapClient.bind(userDN, password)
@ -58,9 +58,12 @@ export async function updatePassword({
})
})
])
return 'SUCCESS'
} catch (err) {
console.error(err)
} finally {
await ldapClient.unbind()
}
return 'FAIL'
}

View File

@ -1,29 +1,44 @@
import { initTRPC, TRPCError } from "@trpc/server";
import * as trpcExpress from "@trpc/server/adapters/express";
import { initTRPC, TRPCError } from '@trpc/server'
import * as trpcExpress from '@trpc/server/adapters/express'
import { z } from "zod";
import { z } from 'zod'
import { updatePassword } from './lib/updatePassword'
export const { procedure, router } = initTRPC.create();
export const { procedure, router } = initTRPC.create()
const { query, mutation, input } = procedure;
const { query, input } = procedure
export const appRouter = router({
hello: query(async () => {
return "Hello World!";
return 'Hello World!'
}),
updatePassword: input(
z.object({
username: z.string(),
password: z.string(),
newPassword: z.string().min(8),
newPassword: z.string().min(8)
})
).mutation(async () => {}),
});
).mutation(async ({ input }) => {
const { username, password, newPassword } = input
try {
await updatePassword({
username,
password,
newPassword
})
} catch (err: any) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: err.message
})
}
})
})
// export type definition of API
export type AppRouter = typeof appRouter;
export type AppRouter = typeof appRouter
export const trpcMiddleware = trpcExpress.createExpressMiddleware({
router: appRouter,
});
router: appRouter
})