Added refresh tokens
This commit is contained in:
@@ -1,10 +1,22 @@
|
||||
let User = require('../schema/user.model');
|
||||
let Session = require('../schema/session.model');
|
||||
const bcrypt = require('bcrypt');
|
||||
|
||||
// checkSession(userId, sessionId) checks if the sessionId is valid for the user
|
||||
const checkSession = (userId, sessionId, f) => {
|
||||
Session.find({ userId: userId, sessionId: sessionId }, (err, res) => {
|
||||
if (res) {
|
||||
if (res && res.type == 0) {
|
||||
f(true);
|
||||
return;
|
||||
}
|
||||
f(false);
|
||||
});
|
||||
}
|
||||
|
||||
// checkRefresh(userId, sessionId) checks if the refresh token is valid for the user
|
||||
const checkRefresh = (userId, sessionId, f) => {
|
||||
Session.find({ userId: userId, sessionId: sessionId }, (err, res) => {
|
||||
if (res && res.type == 1) {
|
||||
f(true);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const config = require('../config');
|
||||
let Session = require('../schema/session.model');
|
||||
const maxSessionLength = config.maxSessionLength;
|
||||
const maxRefreshLength = config.maxRefreshLength;
|
||||
|
||||
// purgeSessions() purge sessions that have existed for longer than maxSessionLength
|
||||
const purgeSessions = () => {
|
||||
@@ -9,7 +10,12 @@ const purgeSessions = () => {
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
let timeDifference = new Date().getTime() - arr[i].date;
|
||||
let dayDifference = timeDifference / (1000 * 3600 * 24);
|
||||
if (dayDifference > maxSessionLength) {
|
||||
if (arr[i].type == 0 && dayDifference > maxSessionLength) {
|
||||
arr[i].delete().catch(e => {
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
if (arr[i].type == 1 && dayDifference > maxRefreshLength) {
|
||||
arr[i].delete().catch(e => {
|
||||
console.log(e);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user