Create Authentication Controllers
Create src/controllers/auth.controller.js.
The controller connects the authentication routes to the authentication service.
Registration should return HTTP 201.
Login should return HTTP 200.
src/controllers/auth.controller.js
import * as authService from '../services/auth.service.js';
import { registerUser } from '../services/user.service.js';
export async function register(req, res, next) {
try {
const user = await registerUser(req.body);
res.status(201).json({
success: true,
message: 'Registration successful',
data: user
});
} catch (error) {
next(error);
}
}
export async function login(req, res, next) {
try {
const result = await authService.loginUser(
req.body.email,
req.body.password
);
res.status(200).json({
success: true,
message: 'Login successful',
data: result
});
} catch (error) {
next(error);
}
}Creates registration and login controllers that delegate authentication operations to service functions.
Key Takeaways
Controllers coordinate authentication requests and responses.
Registration returns 201.
Login returns an access token after successful verification.