-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusercontroller.js
153 lines (116 loc) · 3.68 KB
/
usercontroller.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
145
146
147
148
149
150
151
152
153
const mongoose = require("mongoose")
const bcrypt = require("bcrypt")
const jwt = require("jsonwebtoken")
const secret = process.env.secret
const User = require("../models/userModel")
const registerUser = async (req, res) => {
//1. check wheather the user is already existed or not
//2.if user is new user then create it
//3.if user is already existed then send message like that user already existed
const { name, password, email } = req.body
try {
if (!name || !password || !email) {
return res.status(400).json({
status: "one or more fields are empty",
})
}
const existedUser = await User.findOne({ email })
if (existedUser) {
return res.status(400).json({
error: "user is already existed"
})
}
// encryping the users password before saving
bcrypt.hash(password, 10, async (err, hash) => {
if (err) {
return res.status(500).json({
status: "Failed",
error: err.message
})
}
const newuser = await User.create({
name: name,
email: email,
password: hash
})
return res.status(201).json({
status: "Success",
message: "user created successfully",
user: newuser
})
})
}
catch (e) {
res.json({
status: "error",
message: e.message
})
}
}
const loginUser = async (req, res) => {
const { email, password } = req.body
try {
//1. we check wheather user having the account with the given email or not
const existeduser = await User.findOne({ email: email })
if(!existeduser){
return res.status(409).json({
status:"Failed",
message:"There is no account with this email"
})
}
//2. if user already there , check the given password
bcrypt.compare(password,existeduser.password, (err,result) =>{
if(err){
return res.json({
status:"Failed",
message:err.message
})
}
if(result){ // the result is true when both passwords are matched, other wise result is false
const token =jwt.sign({_id:existeduser._id},secret)
return res.status(200).json({
status:"Successful",
message:"Login success",
token : token
})
}
else{
return res.status(401).json({
status:"Failed",
message:"Invalid Credentials"
})
}
})
}
catch (err) {
res.json({
status:"Failed",
error: err.message
})
}
}
//get all users
const getAllusers = async(req,res)=>{
console.log(req.search)
try{
const username = req.query.search
let user; // getting all users except current user
console.log(req.user._id)
if(username){
user = await User.find({$and:[
{$or:[{name:{$regex:username, $options:"i"}},{
email:{$regex:username, $options:"i"}
}
]}
,{_id:{$ne:req.user._id}}]}).select("-password")
}
res.send(user)
}
catch(err){
res.status(500).json({
status:"Failed",
error:err.message
})
}
}
module.exports = { registerUser, loginUser, getAllusers}