-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth-service.js
86 lines (79 loc) · 2.57 KB
/
auth-service.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
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
const bcrypt = require('bcryptjs');
var userSchema = new Schema({
"userName": String,
"password": String,
"email": String,
"loginHistory": [ {
"dateTime": Date,
"userAgent": String,
}]
});
let User;
function initialize(){
let db = mongoose.createConnection(`mongodb+srv://hashcode-ankit:[email protected]/?retryWrites=true&w=majority`);
return new Promise((resolve,reject)=>{
db.on('error', (err)=>{
reject(Error("Db not Connected"));
});
db.once('open', ()=>{
User = db.model("users", userSchema);
resolve("db1 success!");
});
})
}
function registerUser(userData) {
return new Promise((resolve,reject)=>{
if(userData.password != userData.password2){
reject("Password do not match");
}
bcrypt.hash(userData.password, 10).then((hash)=>{
userData.password=hash;
let newUser= new User(userData);
newUser.save((err)=>{
if(err){
reject(Error("User Name Already Taken "))
}
else if(err){
reject("There was an error creating the user:"+err);
}
else{
resolve();
}
})
})
.catch(err=>{
console.log(err); // Show any errors that occurred during the process
});
})
}
function checkUser(userData){
return new Promise((resolve,reject)=>{
User.find({userName: userData.userName})
.exec().then((data)=>{
if(data.length==0){
reject("Unable to Find user"+userData.userName)
}
bcrypt.compare(userData.password, data[0].password).then((result) => {
if(!result){
reject("Incorrect Password for user : "+ userData.userName);
}
else{
data[0].loginHistory.push({dateTime: (new Date()).toString(), userAgent: userData.userAgent})
User.updateOne(
{userName: data[0].userName},
{$set : { loginHistory : data[0].loginHistory}}
).exec().then(()=>{
resolve(data[0])
}).catch(function(error){
reject("There was an error verifying the user : "+err)
});
}
})
}).catch(function(error){
reject("unable to find the user : "+ userData.userName +error)
});
})
}
module.exports={initialize,registerUser,checkUser }