1 | /*!
|
2 |
|
3 | ----------------------------------------------------------------------------
|
4 | | qewd: Quick and Easy Web Development |
|
5 | | |
|
6 | | Copyright (c) 2017-18 M/Gateway Developments Ltd, |
|
7 | | Redhill, Surrey UK. |
|
8 | | All rights reserved. |
|
9 | | |
|
10 | | http://www.mgateway.com |
|
11 | | Email: rtweed@mgateway.com |
|
12 | | |
|
13 | | |
|
14 | | Licensed under the Apache License, Version 2.0 (the "License"); |
|
15 | | you may not use this file except in compliance with the License. |
|
16 | | You may obtain a copy of the License at |
|
17 | | |
|
18 | | http://www.apache.org/licenses/LICENSE-2.0 |
|
19 | | |
|
20 | | Unless required by applicable law or agreed to in writing, software |
|
21 | | distributed under the License is distributed on an "AS IS" BASIS, |
|
22 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
23 | | See the License for the specific language governing permissions and |
|
24 | | limitations under the License. |
|
25 | ----------------------------------------------------------------------------
|
26 |
|
27 | 19 November 2018
|
28 |
|
29 | */
|
30 |
|
31 | module.exports = function(req) {
|
32 | var uid = req.session.uid;
|
33 | if (typeof uid === 'undefined') return;
|
34 | //console.log('uid = ' + uid);
|
35 | var qewdSession = this.sessions.byToken(uid);
|
36 | if (!qewdSession) {
|
37 | // New JWT - create a new QEWD Session for it
|
38 |
|
39 | //console.log('**** application: ' + req.application + '; timeout: ' + req.session.timeout);
|
40 | qewdSession= this.sessions.create(req.application, req.session.timeout);
|
41 | var token = qewdSession.token;
|
42 | //console.log('token = ' + token);
|
43 |
|
44 | // swap QEWD Session token with JWT uid value
|
45 | var sessionDocName = this.userDefined.config.sessionDocumentName;
|
46 | var sessionGlo = this.db.use(sessionDocName);
|
47 | var sessionRec = sessionGlo.$(['session', qewdSession.id]);
|
48 | var sessionIndex = sessionGlo.$('sessionsByToken');
|
49 | sessionRec.$(['ewd-session', 'token']).value = uid;
|
50 | sessionIndex.$(uid).value = qewdSession.id;
|
51 | sessionIndex.$(token).delete();
|
52 | }
|
53 | else {
|
54 | console.log('QEWD Session ' + qewdSession.id + ' exists for uid ' + uid);
|
55 | }
|
56 | return qewdSession;
|
57 | };
|