Skip to content

Commit 3d47cf8

Browse files
committed
refactor register controller and service into auth
1 parent 5ef9ca8 commit 3d47cf8

File tree

4 files changed

+34
-37
lines changed

4 files changed

+34
-37
lines changed

backend/user/src/controllers/auth/index.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { Request, Response } from 'express';
22
import { StatusCodes } from 'http-status-codes';
3-
4-
import { loginService } from '@/services/auth';
3+
import { loginService } from '@/services/auth/auth-logic';
54
import type { ILoginPayload } from '@/services/auth/types';
5+
import { registerService } from '@/services/registration/register-logic';
6+
import type { IRegisterPayload } from '@/services/registration/register-inputs';
67

78
export async function login(req: Request, res: Response) {
89
const { username, password }: Partial<ILoginPayload> = req.body;
@@ -29,3 +30,33 @@ export async function logout(_req: Request, res: Response) {
2930
.status(StatusCodes.OK)
3031
.json('User has been logged out.');
3132
}
33+
34+
export async function register(req: Request, res: Response) {
35+
//Extract the registration data from the request body
36+
const { email, username, password, firstName, lastName }: Partial<IRegisterPayload> = req.body;
37+
38+
//Validate input
39+
if (!username || !password || !email || !firstName || !lastName) {
40+
return res.status(StatusCodes.UNPROCESSABLE_ENTITY).json('Malformed Request');
41+
}
42+
43+
//Call the registration service
44+
const { code, data, error } = await registerService({
45+
email,
46+
username,
47+
firstName,
48+
lastName,
49+
password,
50+
});
51+
52+
//Handle errors
53+
if (error || code !== StatusCodes.CREATED || !data) {
54+
const sanitizedErr = error?.message ?? 'An error occurred during registration.';
55+
return res.status(code).json(sanitizedErr);
56+
}
57+
58+
return res.status(StatusCodes.CREATED).json({
59+
message: 'User registered successfully',
60+
user: data.user, // Return user data if needed
61+
});
62+
}

backend/user/src/controllers/registration/registerController.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

backend/user/src/routes/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from 'express';
22

33
import { login, logout } from '@/controllers/auth';
44
import { limiter } from '@/lib/ratelimit';
5-
import { register } from '@/controllers/registration/registerController';
5+
import { register } from '@/controllers/auth';
66

77
const router = express.Router();
88

File renamed without changes.

0 commit comments

Comments
 (0)