-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenHandler.js
72 lines (64 loc) · 2.28 KB
/
tokenHandler.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
let jwt = require('jsonwebtoken');
let crypto = require('crypto');// crypto library for hashing password
class TokenHandler {
constructor(secret, lifetime, mongoCollection) {
this.jwtKey = secret;
this.jwtExpirySeconds = lifetime;
this.mongo = mongoCollection;
}
createToken(obj, callback) {
jwt.sign(obj, this.jwtKey, {
algorithm: 'HS256',
expiresIn: this.jwtExpirySeconds
}, (err, token) => {
if (err) {
callback(false);
} else {
callback(token);
}
});
}
signIn(name, password, callback) {//for hashing password we use crypto functions and
let userObj = { //specify sha256 as hash algorithm
"name": name,
"password": crypto.createHash("sha256").update(password).digest('hex')
};
this.mongo.find(userObj).toArray((err, res) => {
if (err || !res[0]) {
callback(false);
} else {
this.createToken(userObj, callback);//if user in Clients exists
}
});
}
registerUser(name, password, callback) { //for hashing password we use crypto functions and
let userObj = { //specify sha256 as hash algorithm
"name": name,
"password": crypto.createHash("sha256").update(password).digest('hex')
};
this.mongo.find({"name": name}).toArray((err, res) => {//check if user with such name already exists or not
if (err || res[0]) { //if there is an error or i find user with such name
callback(false);
} else {
this.mongo.insertOne(userObj, (err, res) => {
if (err) {
callback(false)
} else {
this.createToken(userObj, callback);
}
})
}
})
}
getUserInfoByToken(token, callback) {
try {
let userObj = jwt.verify(token, this.jwtKey);
callback(userObj);
} catch (e) {
if (e instanceof jwt.JsonWebTokenError) {
callback(false);
}
}
}
}
module.exports.TokenHandler = TokenHandler;