-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauth.js
144 lines (138 loc) · 3.86 KB
/
auth.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
133
134
135
136
137
138
139
140
141
142
143
144
const express = require("express");
const jsonwebtoken = require("jsonwebtoken");
const bycryptjs = require("bcryptjs");
const User = require("./Models/UserModel");
const config = require("./config");
const verifyToken = require("./verifyToken");
const authtokens = require("./authTokens");
const UserModel = require("./Models/UserModel");
const app = express.Router();
app.post("/register", (req, res, next) => {
const { username, password } = req.body;
if (username && password) {
bycryptjs.hash(password, 8).then((value) => {
User.create({
username: username,
password: value,
role: "admin",
})
.then((doc) => {
const token = jsonwebtoken.sign({ id: doc._id }, config.secret, {
expiresIn: 3600,
});
const decoded = jsonwebtoken.decode(token);
authtokens[token] = {
id: doc._id,
type: doc.role,
};
res
.status(200)
.json({
auth: true,
token: token,
status: "loggedIn",
uid: doc._id,
role: doc.role,
exp: decoded.exp,
});
})
.catch((er) => {
res
.status(200)
.json({ auth: false, status: er.message, token: null });
});
});
} else {
res
.status(500)
.json({ auth: false, status: "Unable to find username or password" });
}
});
app.post("/login", (req, res, next) => {
const { username, password } = req.body;
console.log(req.body)
if (username && password) {
User.findOne({ username: username }).then((doc) => {
if (doc !== null) {
const isValid = bycryptjs.compareSync(password, doc.password);
if (isValid) {
const token = jsonwebtoken.sign({ id: doc._id }, config.secret, {
expiresIn: 3600, //86400
});
const decoded = jsonwebtoken.decode(token);
authtokens[token] = {
id: doc._id,
type: doc.role,
};
res
.status(200)
.json({
auth: true,
token: token,
status: "loggedIn",
uid: doc._id,
role: doc.role,
exp: decoded.exp,
});
} else {
res
.status(200)
.json({
auth: false,
token: null,
status: "Incorrect Password",
uid: doc._id,
});
}
} else {
res
.status(200)
.json({
auth: false,
token: null,
status: "Incorrect Username Or Password",
uid: null,
});
}
});
} else {
res
.status(500)
.json({ auth: false, status: "Unable to find username or password" });
}
});
app.post("/passwordchange", verifyToken, (req, res, next) => {
const { userId, password, newPassword } = req.body;
console.log(req.body);
UserModel.findById(userId).then((doc) => {
const isValid = bycryptjs.compareSync(password, doc.password);
if (isValid) {
bycryptjs.hash(newPassword, 8).then((hashedPass) => {
UserModel.updateOne(
{ _id: doc._id },
{
username: doc.username,
password: hashedPass,
role: doc.role,
}
).then(() => {
res.status(200).json({ message: "Password Changed Successfully" });
});
});
} else {
res
.status(200)
.json({
auth: false,
token: null,
status: "Incorrect Password",
uid: doc._id,
});
}
});
});
app.get("/logout", verifyToken, function (req, res) {
delete authtokens[req.headers["x-access-token"]];
res.status(200).send({ auth: false, token: null, status: "Logged-Out" });
});
module.exports = app;