@igea/oac_backend 1.0.8 → 1.0.9

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.8",
3
+ "version": "1.0.9",
4
4
  "description": "Backend service for the OAC project",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -18,7 +18,9 @@
18
18
  },
19
19
  "homepage": "https://github.com/chpigea/oac_backend#readme",
20
20
  "dependencies": {
21
+ "@igea/oac_jwt_helpers": "1.0.6",
21
22
  "axios": "1.10.0",
23
+ "cookie-parser": "1.4.7",
22
24
  "express": "5.1.0",
23
25
  "get-port": "7.1.0",
24
26
  "knex": "3.1.0",
@@ -11,5 +11,6 @@
11
11
  "password": "postgres",
12
12
  "database": "oac",
13
13
  "schema": "public"
14
- }
14
+ },
15
+ "jwt_secret": "@igea#"
15
16
  }
@@ -11,5 +11,6 @@
11
11
  "password": "postgres",
12
12
  "database": "oac",
13
13
  "schema": "public"
14
- }
14
+ },
15
+ "jwt_secret": "@igea#"
15
16
  }
@@ -2,41 +2,72 @@ const express = require('express');
2
2
  const router = express.Router();
3
3
  const Users = require('../models/users');
4
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
- });
5
+ module.exports = function(jwtLib){
6
+ /**
7
+ curl -X POST \
8
+ -H "Content-Type: application/json" \
9
+ -d '{"type": "login","user":"igea","password":"@igea#"}' \
10
+ http://localhost:4000/backend/auth/authenticate
11
+ */
32
12
 
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
- });
13
+ const manageUser = function(res, user){
14
+ let token = jwtLib.createToken(user)
15
+ res.cookie('token', token, {
16
+ httpOnly: true,
17
+ secure: false,
18
+ sameSite: 'lax'
19
+ })
20
+ res.json({
21
+ success: true,
22
+ data: user,
23
+ message: null
24
+ });
25
+ }
26
+
27
+ router.post('/authenticate', (req, res) => {
28
+ console.log("here authenticate")
29
+ let body = req.body
30
+ let type = body.type
31
+
32
+ if(type.toLowerCase()=="login"){
33
+ let user = body.user
34
+ let password = body.password
35
+ Users.fromAuthentication(user, password)
36
+ .then(response => {
37
+ response["is_anonymous"] = false
38
+ manageUser(res, response)
39
+ }).catch(err => {
40
+ res.json({
41
+ success: false,
42
+ data: null,
43
+ message: `${ err }`
44
+ });
45
+ })
46
+ }else{
47
+ manageUser(res, {
48
+ id: -1,
49
+ name: 'anonymous',
50
+ surname: 'anonymous',
51
+ username: 'anonymous',
52
+ email: 'anonymous@igea-soluzioni.it',
53
+ mobile: null,
54
+ is_admin: false,
55
+ is_active: true,
56
+ is_anonymous: true
57
+ })
58
+ }
59
+ });
60
+
61
+ router.get('/echo', (req, res) => {
62
+ console.log("here ECHO")
63
+ res.json({
64
+ success: true,
65
+ data: "echo",
66
+ message: null
67
+ });
68
+ });
69
+
70
+ return router
71
+
72
+ }
41
73
 
42
- module.exports = router
package/src/index.js CHANGED
@@ -2,10 +2,23 @@ const config = require('./config.js');
2
2
  const express = require("express");
3
3
  const axios = require("axios");
4
4
  const getPort = require('get-port');
5
+ const cookieParser = require('cookie-parser');
5
6
  const serviceName = "backend"
7
+ const jwtLibFactory = require('@igea/oac_jwt_helpers')
8
+ const jwtLib = jwtLibFactory({
9
+ secret: process.env.JWT_SECRET || config.jwt_secret,
10
+ excludePaths: [
11
+ `/${serviceName}/auth/authenticate`,
12
+ `/${serviceName}/auth/echo`,
13
+ `/${serviceName}/health`
14
+ ],
15
+ signOptions: { expiresIn: '1h' }
16
+ });
6
17
 
7
18
  const app = express();
19
+ app.use(cookieParser());
8
20
  app.use(express.json());
21
+ app.use(jwtLib.middleware);
9
22
 
10
23
  let newPort = null
11
24
 
@@ -31,7 +44,7 @@ getPort.default({
31
44
  const healthRouter = require('./controllers/health.js')
32
45
  app.use(`/${serviceName}/health`, healthRouter)
33
46
 
34
- const authRouter = require('./controllers/auth.js')
47
+ const authRouter = require('./controllers/auth.js')(jwtLib)
35
48
  app.use(`/${serviceName}/auth`, authRouter)
36
49
 
37
50
  app.listen(port, async () => {