File tree Expand file tree Collapse file tree 4 files changed +82
-1
lines changed Expand file tree Collapse file tree 4 files changed +82
-1
lines changed Original file line number Diff line number Diff line change 1+ import { Request , Response } from "express" ;
2+ import userRepository from "../../repositories/user.repository" ;
3+ import User from "../../models/user" ;
4+ import { validationResult } from "express-validator" ;
5+
6+ export const createUser = async ( req : Request , res : Response ) => {
7+ const errors = validationResult ( req ) ;
8+
9+ if ( ! errors . isEmpty ( ) ) {
10+ return res . status ( 400 ) . json ( {
11+ message : 'Invalid data provided for registration' ,
12+ errors : errors . array ( )
13+ } ) ;
14+ }
15+
16+ try {
17+ const user = req . body ;
18+ const newUser = await userRepository . save ( new User ( {
19+ ...user , role : 'user'
20+ } ) ) ;
21+
22+ return res . status ( 200 ) . json ( {
23+ user : newUser ,
24+ } ) ;
25+
26+ } catch ( error : any ) {
27+ console . log ( error ) ;
28+ res . status ( 400 ) . json ( {
29+ message : error . message . toString ( ) ,
30+ } ) ;
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ import { Request } from "express" ;
2+ import { checkSchema } from "express-validator" ;
3+ import userRepository from "../../repositories/user.repository" ;
4+
5+ const validator = checkSchema ( {
6+ firstname : { notEmpty : { errorMessage : "Firstname is required" } } ,
7+ lastname : { notEmpty : { errorMessage : "Lastname is required" } } ,
8+ email : {
9+ notEmpty : {
10+ errorMessage : "Email is required" ,
11+ bail : true
12+ } ,
13+ isEmail : {
14+ errorMessage : "Email address is not valid" ,
15+ bail : true
16+ } ,
17+ custom : {
18+ options : async ( value : string ) => {
19+ const emailExist = await userRepository . existByEmail ( value ) ;
20+ if ( emailExist ) throw new Error ( ) ;
21+ } ,
22+ errorMessage : "Email is already used"
23+ }
24+ } ,
25+ password : {
26+ isLength : {
27+ options : { min : 6 } ,
28+ } ,
29+ errorMessage : "Password should have at least 6 characters"
30+ } ,
31+ phone : {
32+ isMobilePhone : {
33+ options : [ 'any' , { strictMode : true } ] ,
34+ } ,
35+ }
36+ } , [ 'body' ] ) ;
37+
38+ export default validator ;
Original file line number Diff line number Diff line change 1+ import { Router } from "express" ;
2+ import { createUser } from "../controllers/users.controller" ;
3+ import registerValidator from "../middlewars/register.validator" ;
4+
5+ const router = Router ( ) ;
6+
7+ router . post ( '/register' , registerValidator , createUser ) ;
8+
9+ export default router ;
Original file line number Diff line number Diff line change 11import express , { Request , Response } from 'express' ;
2- import tasksRoutes from './api/routes/tasks' ;
2+ import tasksRoutes from './api/routes/tasks.routes' ;
3+ import authRoutes from './api/routes/auth.routes' ;
34
45const app = express ( ) ;
56
67app . use ( express . json ( ) ) ;
78app . use ( '/tasks' , tasksRoutes ) ;
9+ app . use ( '/auth' , authRoutes ) ;
810
911app . get ( '/' , ( req : Request , res : Response ) => {
1012 res . send ( 'Hello, TypeScript Express!' ) ;
You can’t perform that action at this time.
0 commit comments