/
db.js
95 lines (76 loc) · 3.25 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// db.js created by Salam
// Schema/Models for mongodb records
module.exports = function (mongoose) {
$userSchema = new mongoose.Schema({
/* Schema for the user collection in mongodb
* Used to define what information is needed to add the user to the database collection
* @param loginId - A concatenation of region and summonername to create a unique per region logon
* @param summonerName - The users summoner name
* @param password - the users chosen password
* @param salt - the salt with which the password was hashed
* @param region - the user's region
*/
loginId: { type: String, index: true },
summonerName: String,
password: String,
salt: String,
region: String
});
$users = mongoose.model('User', $userSchema);
this.Users = $users
$sessionSchema = new mongoose.Schema({
/* Schema for the session data collection in mongodb
* Used to define what information is needed to check user sessions and keep users logged in
* @param loginId - A concatenation of region and summonername to create a unique per region logon
* @param sessionid - a random session id that is stored as a token cookie when the user logs in
* @param time - the time the token was set - Used to make sure sessions expire after 30 days of inactivity
*/
loginId: { type: [String], index: true },
sessionId: String,
time:{ type: Date, default: Date.now }
});
var $sessions = mongoose.model('Sessions', $sessionSchema);
this.Sessions = $sessions;
$profileSchema = new mongoose.Schema({
/* Schema for the users profile data
* Used to define what is needed for a document in this collection
* @param loginId - A concatenation of region and summonername to create a unique per region logon
* @param totalGames - the total number of games the user has played
* @param gamesWon - the total number of games won
* @param totalScore - the total culuilative score accrued over all played games
*/
loginId: { type: String, index: true },
totalGames: Number,
gamesWon: Number,
totalScore: Number
});
$profiles = mongoose.model('Profiles', $profileSchema);
this.Profiles = $profiles;
var $validateSession = function($token, $loginId, $callBack) {
/* class @validateSessions
* @param $token - The token provided by the client when they navigate to the index page
* @param $loginId - The loginId provided by the client
* @param $callback - A callback function to return errors back to the client
*
* desc - A function for validating user sessions with sessions stored in the sessions collection in the database
* if tokens and login Ids match session is upheld
*/
var $Date = new Date;
$sessions.findOne({loginId: $loginId, sessionId: $token}, 'loginId sessionId Date', function(err, session) {
if (err) {
$callBack({err: true, msg:'There was an error fetching your session.'});
} else {
if (typeof session === 'object' && session != null) {
if ((((new Date(session.time).getTime()) / 1000) + 2592000) < $Date.getTime() / 1000) {
$callBack({err: true, msg: 'Session exceeds 30 day timeout.'});
} else {
$callBack({err: false, msg: 'Success!'});
}
} else {
$callBack({err: true, msg: 'No session found.'});
}
}
});
}
this.validateSession = $validateSession;
}