Skip to content

Commit

Permalink
feat(register): add simple register
Browse files Browse the repository at this point in the history
  • Loading branch information
Famozzy committed Nov 12, 2024
1 parent 34543bd commit f87de68
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
import { Request, Response } from "express";
import { registerSchema } from "../schemas/auth.schema";
import { auth } from "../config";
import { z } from "zod";
import type { Controller } from "../types";

export const helloAuth = (_req: Request, res: Response) => {
res.json({ message: "hello auth" });
};

type RegisterSchema = z.infer<typeof registerSchema>;
export const register: Controller<RegisterSchema> = async (req, res) => {
try {
const { name: displayName, email, password } = req.body;

const user = await auth.createUser({ displayName, email, password });

res.status(201).json({
status: "success",
message: "register success",
data: { userId: user.uid },
});
} catch (error) {
if (error instanceof z.ZodError) {
res.status(400).json({
status: "fail",
message: "validation error",
errors: error.message,
});
}

if (error instanceof Error) {
res.status(400).json({ status: "fail", message: error.message });
}
}
};
8 changes: 8 additions & 0 deletions src/routes/v1/auth.route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { Router } from "express";
import * as authController from "@controllers/auth.controller";
import { validator } from "../../middleware/validation.middleware";
import { registerSchema } from "../../schemas/auth.schema";

// /api/v1/auth
const authRouter = Router();

authRouter.get("/", authController.helloAuth);

authRouter.post(
"/register",
validator(registerSchema),
authController.register,
);

export default authRouter;
9 changes: 9 additions & 0 deletions src/schemas/auth.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from "zod";

export const registerSchema = z.object({
body: z.object({
name: z.string().min(3, "Name must be at least 3 characters"),
email: z.string().email("Invalid email address"),
password: z.string().min(8, "Password must be at least 8 characters"),
}),
});

0 comments on commit f87de68

Please sign in to comment.