-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthRouter.js
119 lines (112 loc) · 3.9 KB
/
authRouter.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
const express = require("express")
const schemas = require("./schemas")
const mongoose = require("mongoose")
const jwt = require("jsonwebtoken")
const md5 = require("md5")
const AuthRouter = express.Router()
const authAPI = require("./dataRoutes/authAPI")
const {
response
} = require("express")
const MemberModel = mongoose.model("Member", schemas.memberSchema)
const MemberRequestModal = mongoose.model("MemberRequest", schemas.memberRequestSchema)
const secret = "knfsngslvmsomedofksmbhonf535wr2343363fsgdvgfhhedge644785";
AuthRouter.route("/login")
.post((req, res) => {
authAPI.Login(req.body, MemberModel, secret).then((value) => {
res.send(value)
})
})
AuthRouter.route("/login/verify")
.post((req, res) => {
authAPI.verifyLogin(req.body.token, secret, MemberModel).then((val) => res.send({
"status": val
}))
// if(req.body){
// const result = MemberModel.find({"username": req.body.username, "password": md5(req.body.password)})
// }
})
AuthRouter.route("/join")
.post((req, res) => {
const member = new MemberRequestModal({
"firstname": req.body.firstname,
"lastname": req.body.lastname,
"email": req.body.email,
"phone": req.body.phone,
"username": req.body.username,
"terms": req.body.terms
})
if (req.body && req.body.terms == true) {
//If Validated then status is true else false
authAPI.Join(member).then((val) => {
if (val.errors) {
res.status(200).json({
"status": false,
"message": "Validation Failed"
})
} else {
res.status(200).json({
"status": true,
"message": "Success"
})
}
}).catch((err) => {
res.status(200).json({
err,
"status": false,
"message": "Validation Failed"
});
})
} else {
res.status(200).send({
"status": false,
"message": "Please Accept The Terms"
});
}
})
//Accept OF Reject Join Request
AuthRouter.route("/join/requests")
.post((req, res) => {
const role = jwt.verify(req.body.token, secret, {}, (err, decoded) => {
if (err) {
res.status(400).send(err)
} else {
if (decoded.role == "admin") {
authAPI.getRequests(MemberRequestModal).then((val) => {
res.send(val)
});
}
}
})
})
AuthRouter.route("/join/request")
.post((req, res) => {
jwt.verify(req.body.token, secret, {}, (err, data) => {
if (err) {
console.log(err)
} else {
if (data.role == "admin") {
if (req.body.status == "A") {
authAPI.acceptRequest(req.body.id, MemberRequestModal).then((val) => {
res.send({
"status": true
})
}).catch((err) => {
return({
"status": true
})
})
} else {
authAPI.rejectRequest(req.body, MemberRequestModal).then((val) => {
res.status(200).send({
"status": val
})
}).catch(err => {
console.log(err)
})
}
}
}
})
})
module.exports = AuthRouter