-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth-service.js
132 lines (116 loc) · 3.54 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*********************************************************************************
* WEB322 – Assignment 06
* I declare that this assignment is my own work in accordance with Seneca Academic Policy. No part
* of this assignment has been copied manually or electronically from any other source
* (including 3rd party web sites) or distributed to other students.
*
* Name: Lorenz Alvin Tubo Student ID: 1090934224 Date: 07/29/2023
*
* Cyclic Web App URL: https://easy-teal-elk-gear.cyclic.app/about
*
* GitHub Repository URL: https://github.com/YuhanPizza/web322-app
*
********************************************************************************/
//mongoose
const mongoose = require("mongoose");
//bcryptjs
const bcrypt = require('bcryptjs');
//user schema.
const userSchema = new mongoose.Schema({
userName: { type: String, unique: true },
password: String,
email: String,
loginHistory: [
{
dateTime: Date,
userAgent: String,
},
],
});
//user
let User;
//initalize
const initialize = () => {
return new Promise(function (resolve, reject) {
const connectionString =
process.env.MONGODB_STRING;
let db = mongoose.createConnection(connectionString);
db.on("error", (err) => {
reject(err); // reject the promise with the provided error
});
db.once("open", () => {
User = db.model("users", userSchema);
resolve();
});
});
};
// register user
const registerUser = (userData) => {
return new Promise(function (resolve, reject) {
if (userData.password !== userData.password2) {
reject("Passwords do not match");
} else {
bcrypt.hash(userData.password, 10)
.then((hash) => {
userData.password = hash;
const newUser = new User(userData);
return newUser.save(); // Return the promise
})
.then(() => {
resolve();
})
.catch((err) => {
if (err.code === 11000) {
reject("User Name already taken");
} else {
reject("There was an error creating the user: " + err);
}
});
}
});
};
// check user
const checkUser = (userData) => {
return new Promise(function (resolve, reject) {
User.findOne({ userName: userData.userName })
.then((user) => {
if (!user) {
reject("Unable to find user: " + userData.userName);
} else {
bcrypt.compare(userData.password, user.password)
.then((result) => {
if (result) {
const loginInfo = {
dateTime: new Date().toString(),
userAgent: userData.userAgent,
};
user.loginHistory.push(loginInfo);
// Keep only the last 10 logins.
if (user.loginHistory.length > 10) {
user.loginHistory = user.loginHistory.slice(-10);
}
return user.save(); // Return the promise
} else {
reject("Incorrect Password for user: " + userData.userName);
}
})
.then(() => {
resolve(user);
})
.catch((err) => {
reject("There was an error verifying the user: " + err);
});
}
})
.catch((err) => {
reject("Unable to find user: " + userData.userName);
});
});
};
module.exports = {
userSchema,
User,
initialize,
registerUser,
checkUser,
};