@igea/oac_backend 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@igea/oac_backend",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Backend service for the OAC project",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -3,5 +3,13 @@
3
3
  "url_register": "http://localhost:4001/register",
4
4
  "port_range": {
5
5
  "min": 5000, "max": 6000
6
+ },
7
+ "database": {
8
+ "host": "127.0.0.1",
9
+ "port": "5432",
10
+ "user": "postgres",
11
+ "password": "postgres",
12
+ "database": "oac",
13
+ "schema": "public"
6
14
  }
7
15
  }
@@ -3,5 +3,13 @@
3
3
  "url_register": "http://localhost:4001/register",
4
4
  "port_range": {
5
5
  "min": 5000, "max": 6000
6
+ },
7
+ "database": {
8
+ "host": "127.0.0.1",
9
+ "port": "5432",
10
+ "user": "postgres",
11
+ "password": "postgres",
12
+ "database": "oac",
13
+ "schema": "public"
6
14
  }
7
15
  }
@@ -0,0 +1,42 @@
1
+ const express = require('express');
2
+ const router = express.Router();
3
+ const Users = require('../models/users');
4
+
5
+ /**
6
+ curl -X POST \
7
+ -H "Content-Type: application/json" \
8
+ -d '{"type": "login","user":"igea","password":"@igea#"}' \
9
+ http://localhost:4000/backend/auth/authenticate
10
+ */
11
+ router.post('/authenticate', (req, res) => {
12
+ console.log("here authenticate")
13
+ let body = req.body
14
+ //let type = body.type
15
+ let user = body.user
16
+ let password = body.password
17
+ Users.fromAuthentication(user, password)
18
+ .then(response =>{
19
+ res.json({
20
+ success: true,
21
+ data: response,
22
+ message: null
23
+ });
24
+ }).catch(err => {
25
+ res.json({
26
+ success: false,
27
+ data: null,
28
+ message: `${ err }`
29
+ });
30
+ })
31
+ });
32
+
33
+ router.get('/echo', (req, res) => {
34
+ console.log("here ECHO")
35
+ res.json({
36
+ success: true,
37
+ data: "echo",
38
+ message: null
39
+ });
40
+ });
41
+
42
+ module.exports = router
package/src/index.js CHANGED
@@ -27,9 +27,13 @@ getPort.default({
27
27
  port: getPort.portNumbers(config.port_range.min, config.port_range.max) }
28
28
  ).then((port)=>{
29
29
  newPort = port
30
+
30
31
  const healthRouter = require('./controllers/health.js')
31
32
  app.use(`/${serviceName}/health`, healthRouter)
32
33
 
34
+ const authRouter = require('./controllers/auth.js')
35
+ app.use(`/${serviceName}/auth`, authRouter)
36
+
33
37
  app.listen(port, async () => {
34
38
  console.log(`${serviceName} listening on port ${port}`)
35
39
  await register()
@@ -0,0 +1,10 @@
1
+ const knex = require('knex');
2
+ const config = require('../config')
3
+
4
+ const db = knex({
5
+ client: 'pg',
6
+ connection: config.database,
7
+ pool: { min: 2, max: 10 }
8
+ });
9
+
10
+ module.exports = { db, schema: config.schema || 'public' };
@@ -0,0 +1,39 @@
1
+ const config = require('../config')
2
+ const {db, schema } = require('./db')
3
+ const table = `${schema}.users`
4
+
5
+ class Users {
6
+
7
+ static fromAuthentication(user, password){
8
+ console.log(user, password)
9
+ return new Promise(async (resolve, reject) => {
10
+ try{
11
+ let userFound = await db(table)
12
+ .where(function() {
13
+ this.whereRaw(
14
+ '(username = ? AND password = md5(?))',
15
+ [user, password]
16
+ );
17
+ })
18
+ .orWhere(function() {
19
+ this.whereRaw(
20
+ '(email = ? AND password = md5(?))',
21
+ [user, password]
22
+ );
23
+ })
24
+ .first();
25
+ if(userFound && userFound["is_active"]) {
26
+ delete userFound["password"]
27
+ resolve(userFound)
28
+ }else{
29
+ reject(new Error("Invalid credentials"))
30
+ }
31
+ }catch(e){
32
+ reject(e)
33
+ }
34
+ });
35
+ }
36
+
37
+ }
38
+
39
+ module.exports = Users