1- var User = require ( 'mongoose' ) . model ( 'User' ) ,
2- passport = require ( 'passport' ) ;
1+ import User from '../models/user.model' ;
32
4- var getErrorMessage = function ( err ) {
5- var message = '' ;
6-
7- if ( err . code ) {
8- switch ( err . code ) {
9- case 11000 :
10- case 11001 :
11- message = 'Username already exists' ;
12- break ;
13- default :
14- message = 'Something went wrong' ;
15- }
16- } else {
17- for ( var errName in err . errors ) {
18- if ( err . erros [ errName ] . message )
19- message = err . erros [ errName ] . message ;
20- }
21- }
22-
23- return message ;
3+ import HTTPStatus from 'http-status' ;
4+ import Joi from 'joi' ;
5+
6+ export const validation = {
7+ login : {
8+ body : {
9+ email : Joi . string ( )
10+ . email ( )
11+ . required ( ) ,
12+ password : Joi . string ( )
13+ . regex ( / ^ [ a - z A - Z 0 - 9 ] { 3 , 30 } $ / )
14+ . required ( ) ,
15+ } ,
16+ } ,
17+ create : {
18+ body : {
19+ email : Joi . string ( )
20+ . email ( )
21+ . required ( ) ,
22+ password : Joi . string ( )
23+ . min ( 6 )
24+ . regex ( / ^ (? = .* [ 0 - 9 ] ) (? = .* [ a - z A - Z ] ) ( [ a - z A - Z 0 - 9 ] + ) $ / )
25+ . required ( ) ,
26+ username : Joi . string ( )
27+ . min ( 3 )
28+ . max ( 20 )
29+ . required ( ) ,
30+ } ,
31+ } ,
2432} ;
2533
26- exports . signup = function ( req , res , next ) {
27- if ( ! req . user ) {
28- var user = new User ( req . body ) ;
29- var message = null ;
34+
35+ /**
36+ * @api {post } /users/login Login a user
37+ * @apiDescription Login a user
38+ * @apiName loginUser
39+ * @apiGroup User
40+ *
41+ * @apiParam (Body) {String} email User email.
42+ * @apiParam (Body) {String} password User password.
43+ *
44+ * @apiSuccess {Number} status Status of the Request.
45+ * @apiSuccess {String} _id User _id.
46+ * @apiSuccess {String} token Authentication token.
47+ *
48+ * @apiSuccessExample Success-Response:
49+ *
50+ * HTTP/1.1 200 OK
51+ *
52+ * {
53+ * _id: '123',
54+ * token: 'JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1OTBhMWI3ODAzMDI3N2NiNjQxM2JhZGUiLCJpYXQiOjE0OTM4MzQ2MTZ9.RSlMF6RRwAALZQRdfKrOZWnuHBk-mQNnRcCLJsc8zio',
55+ * }
56+ *
57+ * @apiErrorExample {json} Error
58+ * HTTP/1.1 400 Bad Request
59+ *
60+ * {
61+ * email: 'email is required'
62+ * }
63+ */
64+
65+ export async function login ( req , res , next ) {
66+ res . status ( HTTPStatus . OK ) . json ( req . user . toAuthJSON ( ) ) ;
67+
68+ return next ( ) ;
69+ }
3070
31- user . save ( function ( err ) {
32- if ( err ) {
33- var message = getErrorMessage ( err ) ;
34- res . json ( message ) ;
35- }
36- } )
71+ /**
72+ * @api {post } /users/signup Create a user
73+ * @apiDescription Create a user
74+ * @apiName createUser
75+ * @apiGroup User
76+ *
77+ * @apiParam (Body) {String} email User email.
78+ * @apiParam (Body) {String} password User password.
79+ * @apiParam (Body) {String} username User username.
80+ *
81+ * @apiSuccess {Number} status Status of the Request.
82+ * @apiSuccess {String} _id User _id.
83+ * @apiSuccess {String} token Authentication token.
84+ *
85+ * @apiSuccessExample Success-Response:
86+ *
87+ * HTTP/1.1 200 OK
88+ *
89+ * {
90+ * _id: '123',
91+ * token: 'JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1OTBhMWI3ODAzMDI3N2NiNjQxM2JhZGUiLCJpYXQiOjE0OTM4MzQ2MTZ9.RSlMF6RRwAALZQRdfKrOZWnuHBk-mQNnRcCLJsc8zio',
92+ * }
93+ *
94+ * @apiErrorExample {json} Error
95+ * HTTP/1.1 400 Bad Request
96+ *
97+ * {
98+ * email: 'email is required'
99+ * }
100+ */
101+ export async function create ( req , res , next ) {
102+ try {
103+ const user = await User . create ( req . body ) ;
104+ return res . status ( HTTPStatus . CREATED ) . json ( user . toAuthJSON ( ) ) ;
105+ } catch ( e ) {
106+ e . status = HTTPStatus . BAD_REQUEST ;
107+ return next ( e ) ;
37108 }
38- }
109+ }
0 commit comments