feat(auth): signupm, forgot passowrd, reset password all added
This commit is contained in:
38
app/src/internal/auth/routes/resetPassword.ts
Normal file
38
app/src/internal/auth/routes/resetPassword.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Router, type Request, type Response } from "express";
|
||||
|
||||
import { authClient } from "../../../pkg/auth/auth-client.js";
|
||||
import z from "zod";
|
||||
import { auth } from "../../../pkg/auth/auth.js";
|
||||
|
||||
const resetSchema = z.object({
|
||||
email: z.string(),
|
||||
});
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
try {
|
||||
const validated = resetSchema.parse(req.body);
|
||||
|
||||
const data = await auth.api.requestPasswordReset({
|
||||
body: {
|
||||
email: validated.email,
|
||||
redirectTo: `${process.env.BETTER_AUTH_URL}/user/resetpassword`,
|
||||
},
|
||||
});
|
||||
return res.json(data);
|
||||
} catch (err) {
|
||||
if (err instanceof z.ZodError) {
|
||||
const flattened = z.flattenError(err);
|
||||
return res.status(400).json({
|
||||
error: "Validation failed",
|
||||
details: flattened,
|
||||
});
|
||||
}
|
||||
|
||||
console.log(err);
|
||||
return res.status(400).json({ error: err });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -2,10 +2,12 @@ import type { Express, Request, Response } from "express";
|
||||
import me from "./me.js";
|
||||
import register from "./register.js";
|
||||
import userRoles from "./userroles.js";
|
||||
import resetPassword from "./resetPassword.js";
|
||||
import { requireAuth } from "../../../pkg/middleware/authMiddleware.js";
|
||||
|
||||
export const setupAuthRoutes = (app: Express, basePath: string) => {
|
||||
app.use(basePath + "/api/user/me", requireAuth(), me);
|
||||
app.use(basePath + "/api/user/resetpassword", resetPassword);
|
||||
app.use(basePath + "/api/user/register", register);
|
||||
app.use(basePath + "/api/user/roles", requireAuth(), userRoles);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user