1
1
import type { Request , Response } from 'express' ;
2
2
import { StatusCodes } from 'http-status-codes' ;
3
-
4
- import { loginService } from '@/services/auth' ;
3
+ import { loginService } from '@/services/auth/auth-logic' ;
5
4
import type { ILoginPayload } from '@/services/auth/types' ;
5
+ import { registerService } from '@/services/registration/register-logic' ;
6
+ import type { IRegisterPayload } from '@/services/registration/register-inputs' ;
6
7
7
8
export async function login ( req : Request , res : Response ) {
8
9
const { username, password } : Partial < ILoginPayload > = req . body ;
@@ -29,3 +30,33 @@ export async function logout(_req: Request, res: Response) {
29
30
. status ( StatusCodes . OK )
30
31
. json ( 'User has been logged out.' ) ;
31
32
}
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
+ }
0 commit comments