forked from benigmatic/cop-4331-16
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
authored and
root
committed
Jun 27, 2021
1 parent
6a24ff1
commit 01a5322
Showing
15 changed files
with
921 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
var token = require('./createJWT.js'); | ||
|
||
exports.setApp = function ( app, client ) | ||
{ | ||
|
||
app.post('/api/addcard', async (req, res, next) => | ||
{ | ||
// incoming: userId, color | ||
// outgoing: error | ||
|
||
const { userId, card, jwtToken } = req.body; | ||
|
||
try | ||
{ | ||
if( token.isExpired(jwtToken)) | ||
{ | ||
var r = {error:'The JWT is no longer valid', jwtToken: ''}; | ||
res.status(200).json(r); | ||
return; | ||
} | ||
} | ||
catch(e) | ||
{ | ||
console.log(e.message); | ||
} | ||
|
||
const newCard = {Card:card,UserId:userId}; | ||
var error = ''; | ||
|
||
try | ||
{ | ||
const db = client.db(); | ||
const result = db.collection('Cards').insertOne(newCard); | ||
} | ||
catch(e) | ||
{ | ||
error = e.toString(); | ||
} | ||
|
||
var refreshedToken = null; | ||
try | ||
{ | ||
refreshedToken = token.refresh(jwtToken).accessToken; | ||
} | ||
catch(e) | ||
{ | ||
console.log(e.message); | ||
} | ||
|
||
var ret = { error: error, jwtToken: refreshedToken }; | ||
|
||
res.status(200).json(ret); | ||
}); | ||
|
||
app.post('/api/login', async (req, res, next) => | ||
{ | ||
// incoming: login, password | ||
// outgoing: id, firstName, lastName, error | ||
|
||
var error = ''; | ||
|
||
const { login, password } = req.body; | ||
|
||
const db = client.db(); | ||
const results = await db.collection('Users').find({Login:login,Password:password}).toArray(); | ||
|
||
var id = -1; | ||
var fn = ''; | ||
var ln = ''; | ||
|
||
var ret; | ||
|
||
if( results.length > 0 ) | ||
{ | ||
id = results[0].UserId; | ||
fn = results[0].FirstName; | ||
ln = results[0].LastName; | ||
|
||
try | ||
{ | ||
const token = require("./createJWT.js"); | ||
ret = token.createToken( fn, ln, id ); | ||
} | ||
catch(e) | ||
{ | ||
ret = {error:e.message}; | ||
} | ||
} | ||
else | ||
{ | ||
ret = {error:"Login/Password incorrect"}; | ||
} | ||
|
||
res.status(200).json(ret); | ||
}); | ||
|
||
app.post('/api/searchcards', async (req, res, next) => | ||
{ | ||
// incoming: userId, search | ||
// outgoing: results[], error | ||
|
||
var error = ''; | ||
|
||
const { userId, search, jwtToken } = req.body; | ||
|
||
try | ||
{ | ||
if( token.isExpired(jwtToken)) | ||
{ | ||
var r = {error:'The JWT is no longer valid', jwtToken: ''}; | ||
res.status(200).json(r); | ||
return; | ||
} | ||
} | ||
catch(e) | ||
{ | ||
console.log(e.message); | ||
} | ||
|
||
var _search = search.trim(); | ||
|
||
const db = client.db(); | ||
const results = await db.collection('Cards').find({"Card":{$regex:_search+'.*', $options:'r'}}).toArray(); | ||
|
||
var _ret = []; | ||
for( var i=0; i<results.length; i++ ) | ||
{ | ||
_ret.push( results[i].Card ); | ||
} | ||
|
||
var refreshedToken = null; | ||
try | ||
{ | ||
refreshedToken = token.refresh(jwtToken).accessToken; | ||
} | ||
catch(e) | ||
{ | ||
console.log(e.message); | ||
} | ||
|
||
var ret = { results:_ret, error: error, jwtToken: refreshedToken }; | ||
|
||
res.status(200).json(ret); | ||
}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const jwt = require("jsonwebtoken"); | ||
require("dotenv").config(); | ||
|
||
exports.createToken = function ( fn, ln, id ) | ||
{ | ||
return _createToken( fn, ln, id ); | ||
} | ||
|
||
_createToken = function ( fn, ln, id ) | ||
{ | ||
try | ||
{ | ||
const expiration = new Date(); | ||
const user = {userId:id,firstName:fn,lastName:ln}; | ||
|
||
const accessToken = jwt.sign( user, process.env.ACCESS_TOKEN_SECRET); | ||
|
||
// In order to exoire with a value other than the default, use the | ||
// following | ||
/* | ||
const accessToken= jwt.sign(user,process.env.ACCESS_TOKEN_SECRET, | ||
{ expiresIn: '30m'} ); | ||
'24h' | ||
'365d' | ||
*/ | ||
|
||
var ret = {accessToken:accessToken}; | ||
} | ||
catch(e) | ||
{ | ||
var ret = {error:e.message}; | ||
} | ||
return ret; | ||
} | ||
|
||
exports.isExpired = function( token ) | ||
{ | ||
var isError = jwt.verify( token, process.env.ACCESS_TOKEN_SECRET, | ||
(err, verifiedJwt) => | ||
{ | ||
if( err ) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
}); | ||
|
||
return isError; | ||
|
||
} | ||
|
||
exports.refresh = function( token ) | ||
{ | ||
var ud = jwt.decode(token,{complete:true}); | ||
|
||
var userId = ud.payload.id; | ||
var firstName = ud.payload.firstName; | ||
var lastName = ud.payload.lastName; | ||
|
||
return _createToken( firstName, lastName, userId ); | ||
} |
Oops, something went wrong.