File tree 5 files changed +74
-2
lines changed
5 files changed +74
-2
lines changed Original file line number Diff line number Diff line change
1
+ const { response, request } = require ( 'express' ) ;
2
+ const { log } = require ( '../helpers/log' ) ;
3
+
4
+ const login = async ( req = request , res = response ) => {
5
+ try {
6
+ const { username = '' , password = '' } = req . body ;
7
+ if ( ! username || ! password ) {
8
+ throw new Error ( '[ERROR] Username and Password required' ) ;
9
+ }
10
+
11
+ const token = '' ;
12
+ res . json ( {
13
+ message : '[SUCCESS] - LOGIN SUCCESS' ,
14
+ token
15
+ } ) ;
16
+
17
+ } catch ( error ) {
18
+ log ( error ) ;
19
+ res . status ( 500 ) . json ( {
20
+ message : '[ERROR] - POST LOGIN ERROR' ,
21
+ error
22
+ } ) ;
23
+ }
24
+ }
25
+
26
+ module . exports = {
27
+ login
28
+ }
Original file line number Diff line number Diff line change
1
+
2
+ const parse_jwt = function ( token = '' ) {
3
+ let url_encrypted = token . split ( '.' ) [ 0 ] ;
4
+ b64 = url_encrypted . replace ( '-' , '+' ) . replace ( '_' , '/' ) ;
5
+ return JSON . parse ( window . atob ( b64 ) ) ;
6
+ }
7
+
8
+ module . exports = {
9
+ parse_jwt
10
+ }
Original file line number Diff line number Diff line change
1
+ const { Schema, model } = require ( 'mongoose' ) ;
2
+
3
+ const login_schema = new Schema ( {
4
+ username : {
5
+ type : String ,
6
+ required : [ true , 'field <email> is required' ]
7
+ } ,
8
+ password : {
9
+ type : String ,
10
+ required : [ true , 'field <password> is required' ]
11
+ }
12
+ } )
13
+
14
+ login_schema . methods . toJSON = function ( ) {
15
+ const { password, __v, ...user_loged } = this . toObject ( ) ;
16
+ return user_loged ;
17
+ }
18
+
19
+ module . exports = model ( 'Login' , login_schema ) ;
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ class Server {
14
14
// declare routes
15
15
this . elements_routes_path = `${ ROOT_PATH_API } /elements` ;
16
16
this . user_routes_path = `${ ROOT_PATH_API } /user` ;
17
+ this . auth_path = `${ ROOT_PATH_API } /auth` ;
17
18
// conneccion db
18
19
this . _db_connect ( ) ;
19
20
// middelwares
@@ -32,8 +33,9 @@ class Server {
32
33
}
33
34
34
35
routes = ( ) => {
35
- this . app . use ( `${ this . elements_routes_path } ` , require ( '../routes/elements.routes' ) )
36
- this . app . use ( `${ this . user_routes_path } ` , require ( '../routes/user.routes' ) )
36
+ this . app . use ( `${ this . elements_routes_path } ` , require ( '../routes/elements.routes' ) ) ;
37
+ this . app . use ( `${ this . user_routes_path } ` , require ( '../routes/user.routes' ) ) ;
38
+ this . app . use ( `${ this . auth_path } ` , require ( '../routes/auth' ) ) ;
37
39
this . app . get ( '*' , ( req , res ) => { res . status ( 404 ) . send ( '404 | Not Found' ) } ) ;
38
40
}
39
41
Original file line number Diff line number Diff line change
1
+ const { Router } = require ( 'express' ) ;
2
+ const { check } = require ( 'express-validator' ) ;
3
+ const { login } = require ( '../controller/auth.controller' ) ;
4
+
5
+ const auth_router = new Router ( ) ;
6
+
7
+ auth_router . post ( '/login' , [
8
+ check ( 'username' , 'Invalid e-mail' ) . isEmail ( ) ,
9
+ check ( 'password' , 'password is required' ) . not ( ) . isEmpty ( ) ,
10
+ check ( 'password' , 'must have at least 4 characters' ) . isLength ( { min : 8 } ) ,
11
+ ] , login ) ;
12
+
13
+ module . exports = auth_router ;
You can’t perform that action at this time.
0 commit comments